Why I love Scala: Episode 1

Who needs if-else chains?

And now I bring you a new series in which I show you all the little ways in which Scala can make your life a happier life.

Episode 1: I wanted to render a set of spinning number wheels, like the wheels of a slot machine. Each wheel spins for a different length of time, has a different time-offset in the animation loop, and is positioned slightly differently.

I could have done this with a giant chain of if-else statements. Instead I set up some data in an infinite stream, zipped it up, and mapped over it.

Et voilà.

Lucky Seven!

It's clean. It's concise. And more importantly it was fast to write. Beautiful.

Note: if you're curious and don't recognize it, the render loop belongs in a LibGDX app.