10.19. Splitting Sequences into Subsets (groupBy, partition, etc.)

Problem

You want to partition a sequence into two or more different sequences (subsets) based on an algorithm or location you define.

Solution

Use the groupBy, partition, span, or splitAt methods to partition a sequence into subsequences. The sliding and unzip methods can also be used to split sequences into subsequences, though sliding can generate many subsequences, and unzip primarily works on a sequence of Tuple2 elements.

The groupBy, partition, and span methods let you split a sequence into subsets according to a function, whereas splitAt lets you split a collection into two sequences by providing an index number, as shown in these examples:

scala> val x = List(15, 10, 5, 8, 20, 12)
x: List[Int] = List(15, 10, 5, 8, 20, 12)

scala> val y = x.groupBy(_ > 10)
y: Map[Boolean,List[Int]] =
   Map(false -> List(10, 5, 8), true -> List(15, 20, 12))

scala> val y = x.partition(_ > 10)
y: (List[Int], List[Int]) = (List(15, 20, 12), List(10, 5, 8))

scala> val y = x.span(_ < 20)
y: (List[Int], List[Int]) = (List(15, 10, 5, 8), List(20, 12))

scala> val y = x.splitAt(2)
y: (List[Int], List[Int]) = (List(15, 10), List(5, 8, 20, 12))

The groupBy method partitions the collection into a Map of subcollections based on your function. The true map contains the elements for which your predicate returned true, and the false map contains the elements that returned false.

The partition, span, and splitAt methods create a Tuple2 of sequences that ...

Get Scala Cookbook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.