Title: Break A Sequence Into Groups
Slug: break_a_sequence_into_groups
Summary: Break A Sequence Into Groups 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 [15]:
// 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


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

View Groups


In [17]:
// View group that is evens
evensOdds(true)


Out[17]:
List(42, 28, 38, 58, 458, 584, 878)

In [18]:
// View group that is odds
evensOdds(false)


Out[18]:
List(25, 63, 23, 2569, 25, 25)