Iterators

slidingO

The circular equivalent of sliding.

Example

Seq(0, 1, 2).slidingO(2) // Iterator(Seq(0, 1), Seq(1, 2), Seq(2, 0))```

Compared to standard

In the same example the standard version behaves differently, does not pass the “sliding window” on the last and first elements.

Seq(0, 1, 2).sliding(2) // Iterator(Seq(0, 1), Seq(1, 2))```

rotations

All possible rotations.

Note

Starting from itself and moving one rotation step to the left.

Example

Seq(0, 1, 2).rotations // Iterator(Seq(0, 1, 2), Seq(1, 2, 0), Seq(2, 0, 1))

On empty seq

Just itself on empty Seq.

Seq.empty.rotations // Iterator(Seq())

reversions

2 sequences, the sequence itself and reversed.

Example

Seq(0, 1, 2).reversions // Iterator(Seq(0, 1, 2), Seq(2, 1, 0))

On empty seq

Just itself on empty Seq.

Seq.empty.reversions // Iterator(Seq())

reflections

2 sequences, the sequence itself and reflected at the start.

Example

Seq(0, 1, 2).reflections // Iterator(Seq(0, 1, 2), Seq(0, 2, 1))

On empty seq

Just itself on empty Seq.

Seq.empty.reflections // Iterator(Seq())

rotationsAndReflections

All possible rotations and reflections.

Note

Starting from itself and moving one rotation step to the right, then reversing and doing the same.

Example

Seq(0, 1, 2).rotationsAndReflections // Iterator(Seq(0, 1, 2), Seq(1, 2, 0), Seq(2, 0, 1), Seq(0, 2, 1), Seq(2, 1, 0), Seq(1, 0, 2))

On empty seq

Just itself on empty Seq.

Seq.empty.rotationsAndReflections // Iterator(Seq())

groupedO

The circular equivalent of grouped โ€” partitions the ring into ceil(n / size) non-overlapping fixed-size blocks.

Unlike standard grouped, the final block wraps across the seam between the last and first elements, so every block has exactly size elements.

Example

"ABCDE".groupedO(2).toList.map(_.mkString) // List("AB", "CD", "EA")

Without wrap

When size divides the ring length, no wrap is needed.

"ABCDEF".groupedO(2).toList.map(_.mkString) // List("AB", "CD", "EF")

Block larger than the ring

When size > n, a single block wraps around multiple times.

"AB".groupedO(5).toList.map(_.mkString) // List("ABABA")

On empty seq

Seq.empty.groupedO(2).toList // Nil

zipWithIndexO

The circular equivalent of zipWithIndex, pairing each element with its original (circular) index rather than its position in the iterator. Accepts an optional starting circular index.

Example

Seq('a', 'b', 'c').zipWithIndexO(1).toList // List(('b', 1), ('c', 2), ('a', 0))

Default

Starting at index 0 produces the same output as zipWithIndex.

Seq('a', 'b', 'c').zipWithIndexO().toList // List(('a', 0), ('b', 1), ('c', 2))
The source code for this page can be found here.