Title: Filter A Sequence
Slug: filter_a_sequence
Summary: Filter A Sequence Using Scala.
Date: 2017-01-03 12:00
Category: Scala
Tags: Basics
Authors: Chris Albon

If you want to learn more, check out Scala Cookbook and Programming in Scala.

Create An Array Sequence


In [20]:
// Create an array that contains arrays with first and last names
val ages = Array(42,25,28,38,58,63,23,458,2569,584,25,25,878)

Elements Less Than 100


In [21]:
ages.filter(_ < 100)


Out[21]:
Array(42, 25, 28, 38, 58, 63, 23, 25, 25)

Elements Greater Than 100


In [22]:
ages.filter(_ >= 100)


Out[22]:
Array(458, 2569, 584, 878)

Elements That Are Even


In [23]:
ages.filter(_ % 2 == 0)


Out[23]:
Array(42, 28, 38, 58, 458, 584, 878)