Indexing

applyO

The circular equivalent of apply.

Note

Given the definition of circular sequence, it returns an element for any possible integer.

Example

Seq(0, 1, 2).applyO(3) // 0

Compared to standard

In the same example the standard version behaves differently, does not return an element, it throws.

Seq(0, 1, 2).apply(3) // IndexOutOfBoundsException

Not a total function

It does not return a value for an empty sequence.

Seq.empty.applyO(0) // ArithmeticException

liftO

The circular equivalent of lift: like applyO, but total. Since any integer is a valid circular index of a non-empty sequence, it returns None only when the sequence is empty.

Example

Seq(0, 1, 2).liftO(3)   // Some(0)
Seq.empty[Int].liftO(0) // None

indexOfO

The circular equivalent of indexOf: finds the index of the first element equal to a given value, searching circularly from a circular index (default 0) and wrapping past the end.

Example

Seq(0, 1, 2).indexOfO(0, 1) // 0 โ€” found by wrapping, indexOf(0, 1) would be -1

indexFrom

Converts a circular index into a standard index.

Example

Seq(0, 1, 2).indexFrom(30001) // 1
The source code for this page can be found here.