Title: Flatten Sequence Of Sequences
Slug: flatten_sequence_of_sequences
Summary: Flatten Sequence Of Sequences 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 [1]:
// Create an array that contains arrays with first and last names
val fullNames = Array(
    Array("Jason", "Miller"),
    Array("Jason", "Miller"), // Duplicate
    Array("Sally", "Fields"),
    Array("Betty", "Johnson")
)

Flatten The Sequence


In [2]:
// Flatten the sequence
fullNames.flatten


Out[2]:
Array(Jason, Miller, Jason, Miller, Sally, Fields, Betty, Johnson)

Flatten The Sequence And Only Keep Unique Values


In [3]:
// Flatten the sequence and remove any duplicates
fullNames.flatten.distinct


Out[3]:
Array(Jason, Miller, Sally, Fields, Betty, Johnson)