v Break A Sequence Into Groups - Scala

Break A Sequence Into Groups

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

Create An Array Sequence

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

Group Array By Anonymous Function

// If an element is even, return True, if not, return False
val isEven = ages.groupBy(_ % 2 == 0)

View Groups

// View group that is evens
evensOdds(true)
List(42, 28, 38, 58, 458, 584, 878)
// View group that is odds
evensOdds(false)
List(25, 63, 23, 2569, 25, 25)