Scala Snippet: How to filter a list in Scala

In Scala, filtering and processing collections is easy and elegant. There are many filtermethods available, but the most used will probably the basic filter method.

Here’s a code example of some filtering on my (ex)camera collection.
The filter method will not only work on Lists, but on any Scala collection.

Running this example will give you the following result:

original article

Scala Snippet: multiline statements in the Scala REPL console

One of the coolest things a standard Scala install will give you, is the Scala interpreter.
Technically speaking, this is not an interpreter. In the background, each statement is quickly compiled into bytecode and executed on the jvm.
Therefore, most people refer to it as the Scala REPL: Read-Evaluate-Print-Loop.

You can access it by starting a command shell on your system and typing in ‘scala‘.
Do make sure your either run it from the place where scala is installed or have scala on your environment PATH.

By using the repl, you can quickly experiment and test out different statements. Once you press ENTER it will evaluate the statement and display the result.

Frequently you want to execute multi-line statements and luckily the repl has a solution for that.

Simply type in :paste and the repl will accept multiline statements.
To exit this mode and evaluate your code, simply type CTRL+D.

Example:

original article

Scala Snippet: Object, Companion Object and Static Methods

If you are a Java developer moving to Scala, one notable difference in terminology that can cause confusion is the term ‘object’.
In Java an object is always an instance of a class, created by calling a constructor.

Object

In Scala an object is used for defining a single instance of a class with the features you want.

In practice this means you will use it:

  • to keep utility/helper methods and constants in one place
  • to have a single immutable instance to share across a system
  • to implement the singleton design pattern to coordinate actions across a system

An object can have the same features as a class. You can extend other classes or traits. The only notable difference is that an object cannot have constructor parameters.

Companion Object

In Java there might be occassions where you want to use static methods.

In Scala you define these methods inside a ‘companion object’, which has to be defined in the same source file as the class.
The class and object can access each others private methods and variables, but you have to do it as you would call a out of scope static method in Java.

Checkout this example in the repl:

As you can see, I’ve used the “static” method from the companion object to update the shuttercount when calling the makePicture method on my canon Camera.

Finally, a singleton object that does not share the same name with a companion class is called a standalone object.

original article

Scala Snippet: Case Class vs plain ordinary Class

In Scala there exist the construct of a ‘case class’. According to Martin Odersky this supports you to write a “regular, non-encapsulated data structure”. It always seems to be associated with pattern matching.

So when to use a case class and when to use a ‘plain’ class?

I found this nice explanation stating:

“Case classes can be seen as plain and immutable data-holding objects that should exclusively depend on their constructor arguments.

This functional concept allows us to

  • use a compact initialisation syntax (Node(1, Leaf(2), None)))
  • decompose them using pattern matching
  • have equality comparisons implicitly defined

In combination with inheritance, case classes are used to mimic algebraic datatypes.

If an object performs stateful computations on the inside or exhibits other kinds of complex behaviour, it should be an ordinary class.

Defining a case class gives you a lot of boilerplate code for free:

  • Getters are generated for the constructor parameters. Setters are only generated when the parameters are declared as var. They are val by default.
  • A nice toString method is generated.
  • An equals and hashCode methods are generated.
  • A copy method is generated to clone an object.
  • An apply method is generated, removing the need to use the new keyword when creating a new instance of the class.
  • An unapply method is generated.

Test it out by starting the Scala repl in a terminal:

If you type in the name of your object with a ‘.’ and press tab, the repl will display some of the available methods that are generated.

Final note
Alvin Alexander writes that case classes are primarily intended to create “immutable records” that you can easily use in pattern matching expressions.
So while you can define the constructor parameters as var, they are intended to be val.

original article