Comments on: Learning Scala – first impressions http://blog.alexcollins.org/2009/09/03/learning-scala-first-impressions/ Musings of technology, sport, life et al Sat, 30 Apr 2011 17:25:18 +0000 hourly 1 http://wordpress.org/?v=3.0.5 By: atc http://blog.alexcollins.org/2009/09/03/learning-scala-first-impressions/comment-page-1/#comment-3892 atc Wed, 09 Sep 2009 13:26:13 +0000 http://blog.beplacid.net/?p=74#comment-3892 James, makes sense completely :) Knew there'd be a good reason, I just hadn't gotten that far yet. Thanks for posting. James, makes sense completely :)

Knew there’d be a good reason, I just hadn’t gotten that far yet. Thanks for posting.

]]>
By: James Iry http://blog.alexcollins.org/2009/09/03/learning-scala-first-impressions/comment-page-1/#comment-3891 James Iry Fri, 04 Sep 2009 16:03:05 +0000 http://blog.beplacid.net/?p=74#comment-3891 In Scala functions are objects and objects can be functions. Isn't a map, at least in part, a function from key to value? Isn't an array, at least in part, a function from integer to value? Here's something to contemplate def english(n: Int) = n match { case 0 => "zero" case 1 => "one" case 2 => "two" } val spanish = Map( 0 -> "cero", 1 -> "uno", 2 -> "dos" ) val french = Array("zero", "un", "deux") def translate(n : Int, language : Int => String) = language(n) println(translate(0, english)) println(translate(1, spanish)) println(translate(2, french)) In Scala functions are objects and objects can be functions.

Isn’t a map, at least in part, a function from key to value? Isn’t an array, at least in part, a function from integer to value?

Here’s something to contemplate

def english(n: Int) = n match {
case 0 => “zero”
case 1 => “one”
case 2 => “two”
}

val spanish = Map(
0 -> “cero”,
1 -> “uno”,
2 -> “dos”
)

val french = Array(“zero”, “un”, “deux”)

def translate(n : Int, language : Int => String) = language(n)

println(translate(0, english))
println(translate(1, spanish))
println(translate(2, french))

]]>