I was recently working on Project Euler Problem 47, which requires the solver to find consecutive integers that each have a given number of distinct prime factors.

This gave me a reason to dive into the Scala collection methods, where I discovered sliding

Groups elements in fixed size blocks by passing a “sliding window” over them

Stream.from(1).take(5).sliding(3).toList

// List(Stream(1, 2, 3), Stream(2, 3, 4), Stream(3, 4, 5))

Sliding provide a great way to do “introspection” on a collection, giving the programmer the ability to compare an element to its neighbors.

Turns out that sliding has 2 signatures, one which takes only the window size, and another which takes window size and also step size.

Stream.from(1).take(5).sliding(3, 2).toList

// List(Stream(1, 2, 3), Stream(3, 4, 5))

Here’s how I ended up solving PE47: Euler47.scala