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 26

Accepts Any Args

Scala supports both multiple-parameter and multiple-parameter list (i.e., curried) definitions of functions. This gives you quite a bit of freedom when defining functions, but also means that some rework can be required if you decide to change a function's "parameter style."

In the following example, a method that starts out with a single parameter list with two parameters is refactored to use curried parameters. Both forms of the method are then invoked, unchanged, before and after refactoring.

What is the result of executing the following code?

  def prependIfLong(candidate: Any, elems: Any*): Seq[Any] = {
    if (candidate.toString.length > 1)
      candidate +: elems
    else
      elems
  }
  println(prependIfLong("I""love" ...

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