O'Reilly logo

Scala Puzzlers by Nermin Šerifovic, Andrew Phillips

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Puzzler 35

A Listful of Dollars

A convenient feature that allows Scala to cater to different coding styles is that you can often substitute curly braces for parentheses:

  scala> (1 to 3).foreach(r => 
           print("%.5f ".format(math.Pi * r * r)))
  3.14159 12.56637 28.27433
    scala> (1 to 3) foreach { r => 
           print("%.5f ".format(math.Pi * r * r)) }
  3.14159 12.56637 28.27433

Scala also provides type aliases, which allows you to give more convenient names to nontrivial types and domain-specific names to general types:

  // letters -> terms and the pages on which they appear
  type BookIndex = Map[Char, Map[String, Seq[Int]]]
    type Fahrenheit = Int
  type Celsius = Int
  // compare with 'def toFahrenheit(celsius: Int): Int'
  def toFahrenheit(c:  ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required