Filed under scala

Using Streams in Scala

Currently there is an interesting discussion happening at the Vienna Scala Enthusiasts google group about using streams in Scala. One short puzzler for you: val s = None #:: Stream.continually(Some(“A”)) val l = None :: Some(“A”) :: Some(“A”) :: Nil s.take(3) == l What does the Scala REPL output for the last expression? a) true … Continue reading

Thinking of Option as List

At the beginning it felt strange to me to use map and flatMap on an Option. But it makes a lot of sense because an Option is just a List which can be empty or just contains one element. The Option class (Scaladoc) even provides a method to convert an Option to a List: scala> … Continue reading

New ScalaTest with IntelliJ IDEA

For existing code one can easily create a ScalaTest with Cmd+Shift+T. But for starting with the test the Scala plugin provides no template. I created following template for myself based on the Scala Class template: #if ((${PACKAGE_NAME} && ${PACKAGE_NAME} != “”))package ${PACKAGE_NAME} #end #parse(“File”) import org.scalatest.FunSuite class ${NAME}Suite extends FunSuite { }

Using minBy and maxBy of Scala Collections API

The Scaladoc of minBy and maxBy which were added in Scala 2.9 is quite rudimentary: def minBy[B](f: (A) ⇒ B)(implicit cmp: Ordering[B]): A def maxBy[B](f: (A) ⇒ B)(implicit cmp: Ordering[B]): A I tried to understand these higher order functions and come up with a use case which makes sense: scala> case class Person(name: String, age: Int) defined class … Continue reading