So, it’s more than two years since I started to use Groovy.
At the beginning I solved couple Project Euler problems and moved to scripting. I still love Groovy, but couple months ago I started to look at Scala. What can I say about it?
IT IS AWESOME
Until I started using it, I was thinking that dynamic typing is just a hidden cost of all the features I get with Groovy. Errors at run-time were painful, but I quickly learned the patterns to follow when developing dynamically typed code.
With Scala I found out that static typing can be used together with all those higher order functions, functional programming etc. And it makes me much more comfortable.
I approached Scala the usual way. I solved first problem from Project Euler. It worked fine, but I didn’t like the style. There was too much Java in my Scala code and I knew it can look better, more functional. And the only functional language I had been using so far was Haskell.
So, I took a Haskell tutorial from and started to look for similarities in Scala. Rewriting Project Euler’s solutions in functional style was a lot of fun and training for my brain. Now it’s more natural.
Since I posted solution for the Problem 024 in Groovy. Here’s one in Scala:
object Problem024 extends App {
val nums = (0 until 10).toList.permutations
val result = nums drop(999999) next() map(_.toString) reduce(_+_)
Console.println(result)
}
It’s a simple bruteforce. What is beautiful about it? The List.permutations method returns Iterator. In Groovy similar method returns a Set which makes it almost useless. There’s an alternative method in Groovy Collection.eachPermutation, but it takes a closure as a parameter. The third option is using PermutationGenerator which returns an Iterator, which I didn’t know of two years ago.
What I see is that Scala’s type system is really well thought. If there are or were any glitches, they were quickly fixed (from small version to small version). It’s not four two years development cycle like in case of Java.
So far, I’ve solved more than 40 problems in Project Euler. You can see sources at https://github.com/bibix/euler. And many of them are one-liners thanks to Scala.
And you can always follow me on Twitter
