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 36

Size It Up

One of the goals of the Scala collections is to make common operations, such as concatenating two collections, concise and easy to read. To that end, the collection types support a number of operators, such as ++ and +:, allowing you to write expressions "naturally" and avoid method names such as concat or prepend.

The following code example uses one of these operations to determine how many items are in a young Scala enthusiast's lunchbox. In order to ensure an at least moderately healthy diet, we sneak an apple into the lunchbox as well.

Our Scala enthusiast has the peculiar habit of never taking more than one of each item, so the code starts out by assuming that lunchboxes are sets. Realizing that this is something of ...

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