Spray is a library written on top of Akka and Scala that allows to quickly create REST interfaces. It is becoming more and more popular in the Scala community because it is easy to use and performant thanks to this asynchronous, actor-based model. This article describes how to efficiently exploit Spray to create a simple… Continue reading How to build a REST api with Spray and Akka
Category: Tutorial
How to use Case Classes in Scala
Scala has adopted many concepts from other functional programming languages: higher-order functions from Haskell, actors model from Erlang, futures from Clojure, etc. However, Scala has also introduced new tools in the functional programming world: case classes is one of them. Case classes are a special type of classes, as the compiler automatically adds some useful… Continue reading How to use Case Classes in Scala
Stateful objects: use them to lie!
Sometimes we want to hide/protect our variables — the classic concept of encapsulation in object oriented programming. We’d like to write a Scala class to manage someone’s age. In particular, we’d like to: - lie on our age if we are not teens anymore (you never know!) - put some validation to avoid negative age values Our… Continue reading Stateful objects: use them to lie!
Shared package code? Fear no more!
Sometimes code is common to all the classes of a specific package (e.g.: conversions between objects, custom logs, shared operations, etc). Our lovely Java doesn’t seem to have a convenient way of declaring code as part of a package, rather than just a class….but FEAR NO MORE! Let’s see how we can this in Scala using package objects.… Continue reading Shared package code? Fear no more!
Traits are better than Legos!
One of the most powerful (and cool) features in Scala is that you can use traits as stackable modifications. Let’s see how it works with a simple tutorial 🙂 Our goal is to create a list of ordered colours, let’s call it ColourMixer. First we create a class to represent what a basic (empty) colour mixer is: Let's create our Legos… Continue reading Traits are better than Legos!