Artikel mit Tag scala

myndian.de

Artikel mit Tag scala

Verwandte Tags

object orientation performance mixins jade adom java composition rpg optimization traits groovy grails c# dynamic typing windows enttäuschung book mocks tests ibatis sql maps tutorial test spring framework unittests nice eclipse fun literatur disco satire internet explorer video projekt apple safari internet netscape familie firefox web wissenschaft werbung bus & bahn montag tussis framework web development mvc sql antipattern strindtemplate jdbc velocity jsp web-applikationen model driven software development groocy ms sql server configuration database fehlerbehandlung strong typing language open source

Samstag, 7. Februar 2009

Toying around ... with Scala

After a long time here is a post about Scala again. I played around with composition of objects an classes from traits.


This post is also a reaction to Thomas Biskups post Toying around ..., where he played around with Qi4j to perhaps find a better architecture for his rogue like rpg “JADE”. We sometimes talking about this and I once mentioned Scalas traits.


So here is a little example of object and class composition with traits in Scala from the domain of role play games. I implemented some simple traits which I used to compose a class and a (singelton) object.


package de.myndian.scala.rpg

// Something that has a “name”
trait Named
{
  def name : String
}

// Something that has “power”
trait Powered
{
  def power : Int
}

// A namend thing that can be hitted 
trait Hitable
{
  this: Named =>
  
  var hitpoints : Int
  
  def hit(points : Int)
  {
    hitpoints -= points
    print(name + “ was hitted with ” + points + “ points. ”) 
    if(hitpoints <= 0)
      println(name + " is dead.")
    else
      println(name + " has " + hitpoints + " hitpoints remaining.")
  }
}

// A named thing that can use its power to hit a hittable
trait Fighter
{
  this : Named with Powered =>
  
  val rnd = new Random(System.currentTimeMillis)
  
  def hit(target : Named with Hitable)
  {
    println(name + “ hits ” + target.name)
    target.hit(rnd.nextInt(power))
  }
}

class Door(val name : String, var hitpoints : Int) 
    extends Named
    with Hitable


// In this fight the dwarf “kills” a door 
object DemoFight
    extends Application
{
  var aDoor = new Door(“a door”, 10)

  object TheDwarf
      extends Named
      with Hitable
      with Fighter
      with Powered
  {
    val name = “the dwarf”
    val power = 10
    var hitpoints = 20
  }

  while(aDoor.hitpoints > 0)
    TheDwarf.hit(aDoor)
}

Some sample output:

the dwarf hits a door
a door was hitted with 3 points. a door has 7 hitpoints remaining.
the dwarf hits a door
a door was hitted with 7 points. a door is dead.

So Thomas, what about “SADE”? ;-)

Geschrieben von Jörg in Softwaretechnik um 15:41 | Kommentare (0) | Trackbacks (0)
Tags für diesen Artikel: adom, composition, jade, mixins, object orientation, rpg, scala, traits

Mittwoch, 31. Oktober 2007

last language war language trolling

Sorry, I could not get around. I had to post this. :-D

(via Ted Newards Blog Ride)
Geschrieben von Jörg in Softwaretechnik um 16:51 | Kommentare (0) | Trackbacks (0)
Tags für diesen Artikel: c#, fun, java, scala

Samstag, 26. November 2005

Scala makes things "just work"

This is our response to Anonymous generic methods making things “just work” using Scala instead of C#. Others had coded solutions in Java or Groovy.

Here’s our code in Scala. Have a look, how simple things can be:

object FindPersonsDemo with Application
{
  case class Person(firstName: String, 
                    lastName: String,
                    age: Int);

  val persons = List(
      Person(“Cathi”, “Gero”, 35),
      Person(“Ted”, “Neward”, 35),
      Person(“Stephanie”, “Gero”, 12),
      Person(“Michael”, “Neward”, 12));
 
  val newards = for (val p <- persons;
                     p.lastName == “Neward”) yield p;
   
  Console.println(newards);
}

The solution in groovy is also very short and nice to read. I was impressed. We had a look at Groovy for some time, because it seems to be a very cool scripting language, with Java roots which makes it very interesting. So we tried to start using it for tests. We tried. With little success. After asking some questions in the user mailing list we got an solution, which did not satisfy us.

Many groovy examples I have read were confusing. There are so much tricks and things you have to know to understand them. A nice example is the map creation syntax, which starts a great discussion in the mailing list. It seems to be very nice:
def map = [name:“Wallace”, likes:“cheese”]
But you can only use Strings as identifier. I had not seen this until some one points this out on the mailing list. This is very ugly, I think.

Let’s get back to the example. I think Scala is much faster and in contrast to a scripting language and it is type save. Yes, it’s really type save, even if you don’t see some type informations in this short piece of code.

I think we will have a look at Groovy again in the future, but Scala seems to be the language of choice at the moment. (beside the standard languages like Java, C#, ...) There’s nother Nice one, but it does not seem to be stable enough for use in real projects.

The first real post in our new blog, by the way. :-)
Geschrieben von Jörg in Softwaretechnik um 20:48 | Kommentar (1) | Trackbacks (0)
Tags für diesen Artikel: c#, groovy, java, nice, scala
(Seite 1 von 1, insgesamt 3 Einträge)

Suche

Inhalt

Startseite
Galerien
Impressum

Kategorien

  • XML Alltag
  • XML Internet
  • XML Musik
  • XML Politik
  • XML Softwaretechnik
  • XML Sonstiges
  • XML Visuelles

Alle Kategorien

Archive

Juli 2010
Juni 2010
Mai 2010
Das Neueste ...
Älteres ...

Blog abonnieren

XML RSS 2.0 feed
ATOM/XML ATOM 1.0 feed
XML RSS 2.0 Kommentare

Login

Verwaltung des Blogs

Login

Aktuelle Einträge

Netzwerkkultur verändert die Gesellschaft
Dienstag, 17. November

Absolute and relative date and time
Sonntag, 18. Oktober

Oren Lavie - Her Morning Elegance
Dienstag, 6. Oktober

Twitter & Blogroll
Samstag, 8. August

Read It Later: Round-Trip-Integration mit Firefox und Google-Reader
Montag, 3. August

Blogroll

* Jörg bei Twitter
* Jens bei Twitter
* Nils bei Twitter

* Beetlebum
* a life less ordinary?
* Martin Fowler's Bliki
* Springify
* BILDblog
* Plazeboalarm
* LawBlog
* ADOM Blog
* Being busy
* Dr. Gero Presser

Links

* Heise
* The Scala Programming Language
Nils' Fotos bei fotocommunity.de
Jogi auf Qype
Get Firefox!
Use OpenOffice.org

Heise News

* Alcatel-Lucent bleibt in den roten Zahlen

* IBM kauft Kompressionsspezialisten

* Speicherhersteller erwarten Preissteigerung

* Britischer Datenschützer findet in Googles WLAN-Datensammlung keine persönliche Daten

* Österreich wegen verpasster Einführung der Vorratsdatenspeicherung verurteilt

kostenloser Counter