Chunk Sequence In Equal Sized 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)

Chunk Array Into Groups Of Two Elements

// Slide over sequence, create a list of two elements, then take two steps
ages.sliding(2,2).toArray
Array(List(42, 25), List(28, 38), List(58, 63), List(23, 458), List(2569, 584), List(25, 25), List(878))

Chunk Array Into Groups Of Two Elements, With Overlap

// Slide over sequence, create a list of two elements, then take one step
ages.sliding(2,1).toArray
Array(List(42, 25), List(25, 28), List(28, 38), List(38, 58), List(58, 63), List(63, 23), List(23, 458), List(458, 2569), List(2569, 584), List(584, 25), List(25, 25), List(25, 878))