So every year I learn a new language. It’s fun, insightful and leaves me feelin’ a fresh for new challenges in my new found religion.
This time round I’ve been learning Common LISP and now Scala. Being a Java programmer professionally it makes sense to try the latter. I love the idea of it being its own language – for scripting as well as fully-fledged development – that can run inside the JVM (or if you’re evil, the .NET CLR).
First impressions are that it’s exciting to learn. I love its simplicity, and its productivity. One thing I do hate: the syntax of array indexes. I can’t for the life of me think why it’s a good idea. I like the [] syntax because it makes it explicit as to what you’re working with. Doing array(1) looks like a method call, not great. Sure I’ll find out soon enough as to why it’s done that way.
I wrote the following today whilst waiting for my Grilled Mackerel to finish soaking :)
#!/bin/sh
exec scala $0 $@
!#
args.foreach(arg => for (c <- arg) println(c))
2 Comments
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))
James, makes sense completely :)
Knew there’d be a good reason, I just hadn’t gotten that far yet. Thanks for posting.
Post a Comment