In a previous SKB, we met the map
method that the standard library offers. There are plenty more, but let's focus on the filter
method in this Scala Knowledge Bit.
The filter
method allows you to keep the elements of the list that return true for the test (Also called a lambda function).
For instance, for list.filter(a => a > 2)
, the list
will be filtered down to only keep the elements that are > 2
(greater than 2). If the list was List(1,2,3,4)
then the filter method would return a new list: List(3,4)
.