Back in university I had few classes dedicated to Scala programming language. Sometimes suddenly things come back to us.

If you never heard about Scala, here is my very short introduction.

Scala was created as bridge between object oriented and functional languages so that it can run on Java Virtual Machine (JVM) and be fully interoperable with Java classes. As Martin Odersky, creator of language, said “I wanted to show that the two paradigms can be unified and that something new and powerful could result from that combination.”

Scala doesn’t force to write in functional style, however it does have all standard features one would expect from functional programming language. The more you prefer OOP, the more likely that your Scala code will look like Java code. If someone starts with functional languages he or she might get impression that Scala isn’t functional language at all. Personally, if I wanted to learn functional programming I would rather go for Haskell which will “force you to eat the whole functional programming burrito”, as Bruce A. Tate, author of “7 languages in 7 weeks” says.

Scala allows for very laconic syntaxes. Sometimes it is way to much of different syntaxes, sometimes syntax is quaint, for example operator “::=” for adding elements to the list, but once you played enough with language you will be able to write shorter pieces of code than what it would take in most conventional languages.

Here is how you could calculate total sum of string lengths in a list:

val listOfStrings = List("a", "ab", "abc", "abcd")
val totLength = (0 /: listOfStrings) { (length, item) => length + item.length() }
println(totLength)

I also wrote generation of permutations in in lexicographic order:

object PermutationGenerator extends App {

  var perm = Array[Char]('a', 'b', 'c', 'd')
  printPerm(1, perm)

  for (i <- 2 to factorial(perm.length)) {
    perm = nextPermutation(perm)
    printPerm(i, perm)
  }

  def factorial(n: Int): Int = n match {
    case 0 => 1
    case x if x > 0 => n * factorial(n - 1)
  }

  def printPerm(i: Int, a: Array[Char]) {
    print(i + ": ")
    a.foreach(c => print(c))
    println()
  }

  def swap(a: Array[Char], i: Int, j: Int) {
    val tmp = a(i)
    a(i) = a(j)
    a(j) = tmp
  }

  def nextPermutation(a: Array[Char]): Array[Char] = {

    val k = (a zip a.tail) lastIndexWhere { case (x, y) => x < y }

    val l = a lastIndexWhere { c => a(k) < c }

    swap(a, l, k)

    return a.take(k + 1) ++ a.takeRight(a.length - k - 1).reverse
  }
}

Code above isn’t functional or imperative completely but rather some combination. Scala is different from other languages, because it allows for such flexibility. There are languages which make easy things hard and hard thing easy. In Scala it is up to you. For example, to avoid swap method (mutable operations) I could have wrote this “obvious” statement:

return (a.take(k) :+ a(l)) ++ ((a.takeRight(a.length - k - 1).take(l - k - 1) :+ a(k)) ++ a.takeRight(a.length - l - 1)).reverse

Few days ago I also wrote one application to continuously traverse web site and find there some specific links and then send e-mail with complete list of them. But to be honest I gave up debugging it and finding problems, even Scala has more or less accepted integration in eclipse IDE. App was rewritten in C#. My cheating was supposed to win T-shirt to my wife. Surprisingly the site went down next day. My guess is I was not the only one smart-ass to do traversing of the site.

I only took few baby steps, but from what I know now I feel deep sympathy to this language.

Links:

How and Why Twitter Uses Scala

Scala official introduction

Have you every programmed in Scala? How was it?

I imagine that it was fun, but would like to hear from you.

If you haven't subsribed yet, you can subsribe below: