windowing a collection in scala
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
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.
Here’s how I ended up solving PE47: Euler47.scala