Scala fits pretty well with list manipulations. Indeed, it facilitates their usage by defining a lot of methods that enable a lot of behaviors, such as filtering elements or grouping them based on an aggregation value. There are tons of such methods, and actually, if we need something to do something with a sequence, it should already be defined at http://www.scala-lang.org/api/current/index.html#scala.collection.Seq.
In the coming sections, we'll cover the most useful sequences when building Scala templates. First of all, let me just point to the fact that in Scala, when we think List
we mean Seq
.
The foreach
method provides a way to iterate over a sequence and apply a given function to each item. In object-oriented programming, we can think of it as a visitor pattern on a flat list.
The result of foreach
is Unit
, which is the Scala version of void
in Java.
The following screenshot shows how to use it:
As we can see, the Java code is less elegant...