Nanyx

sequence

Helpers for sequence processing

The sequence module provides operations on seq(a), which is always finite. Potentially infinite sequences have the type gen(a) and their own attached definitions; Gen.take on a gen yields a seq, which is how an infinite generator is brought into the finite world where fold and toList are safe to call. Because the two are different types with their own attached definitions, xs \take(10) resolves to whichever applies to xs.

map

Sequence.map: (seq(a), (a -> b)) -> seq(b)
numbers \Sequence.map { * 2 }

filter

Sequence.filter: (seq(a), (a -> bool)) -> seq(a)
numbers \Sequence.filter { > 0 }

take

Sequence.take: (seq(a), int) -> seq(a)
numbers \Sequence.take(10)

The equivalent on a potentially infinite generator is attached to gen instead, and is the usual way to make a generator finite:

Gen.take: (gen(a), int) -> seq(a)
fibonacci \Gen.take(10)  -- fibonacci: gen(int), result: seq(int)

skip

Sequence.skip: (seq(a), int) -> seq(a)
items \Sequence.skip(5)

fold

Sequence.fold: (seq(a), b, ((b, a) -> b)) -> b
numbers \Sequence.fold(0) { + }

toList

Sequence.toList: seq(a) -> list(a)

Materializes a sequence into a strict list.

numbers \Sequence.take(3) \Sequence.toList
-- [first, second, third]