Scala filter example

The “filter” function loops over a List, Stream, etc, and removes entries that don’t match the desired criteria:

List(1, 3, 4, None).filter(x => x.equals(3))
res9: List[Any] = List(3)

You can also use the underscore syntax to simplify this. The following is obviously the same as the first example:

List(1, 3, 4, None).filter(_ => _.equals(3))
res10: List[Any] = List(3)

But, it allows you to simplify to this very concise version:

List(1, 3, 4, None).filter(_.equals(3))
res11: List[Any] = List(3)