Metacircular thoughts

February 9, 2007

Beautiful code in Scala

Filed under: Scala — metacircular @ 9:32 pm

The most beautiful code I have seen in Smalltalk has come from the combination of higher-order functions and the Smalltalk-style method invocation syntax scala allows (but does not force you to use). Consider the following:


scala> val data = List range (0,11)
data: scala.List[scala.Int] = List(0,1,2,3,4,5,6,7,8,9,10)

scala> def even(n: int) = (n%2 == 0)
even: (scala.Int)scala.Boolean

scala> data filter even
line12: scala.List[scala.Int] = List(0,2,4,6,8,10)

scala> import Console._
scala> (data filter even) foreach println
0
2
4
6
8
10
line13: scala.Unit = ()

Not bad at all, IMO.

1 Comment »

  1. beautiful

    Comment by lancelot — February 21, 2007 @ 10:59 pm | Reply


RSS feed for comments on this post. TrackBack URI

Leave a comment

Blog at WordPress.com.