# sequence¶

`meth @(Value: any): sequence`

Returns an infinite sequence that repeatedly produces

`Value`

. Should be used with`:limit`

or paired with a finite sequence in`zip`

,`weave`

, etc.list(@1 limit 10) :> [1, 1, 1, 1, 1, 1, 1, 1, 1, 1]

`meth (Initial: any) @ (Fn: function): sequence`

Returns a sequence that produces

`Initial`

,`Fn(Initial)`

,`Fn(Fn(Initial))`

, ... stopping when`Fn(Last)`

returns`nil`

.list(1 @ (_ + 1) limit 10) :> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

`type chained < function, sequence`

A chained function or sequence, consisting of a base function or sequence and any number of additional functions or filters.

When used as a function or sequence, the base is used to produce an initial result, then the additional functions are applied in turn to the result.

Filters do not affect the result but will shortcut a function call or skip an iteration if

`nil`

is returned. I.e. filters remove values from a sequence that fail a condition without affecting the values that pass.`fun chained(Base: any, Fn₁, : function, ...): chained`

Returns a new chained function or sequence with base

`Base`

and additional functions or filters`Fn₁, ..., Fnₙ`

.let F := chained(fun(X) X + 1, fun(X) X ^ 2) F(10) :> 121

`meth (Arg₁: chained)[...]`

*TBD*`meth ^(Function: function): sequence`

Returns a sequence that generates the result of calling

`Function()`

at each iteration until`nil`

is returned.let L := [1, 2, 3, 4] list(^fun L:pull) :> [4, 3, 2, 1]

`meth (Function: function):repeat: sequence`

Returns a sequence that generates the result of calling

`Function()`

at each iteration until`nil`

is returned.Deprecated since version 2.9.0: Use

`^`

instead.let L := [1, 2, 3, 4] list(L:pull(_):repeat) :> [4, 3, 2, 1]

`type grouped < sequence`

*TBD*`meth (Arg₁: integer::interval) & (Arg₂: integer::interval)`

*TBD*`meth (Arg₁: integer::range) & (Arg₂: integer::range)`

*TBD*`type iterator`

An iterator.

`meth (Iterator: iterator):key: any`

Returns the current key produced by

`Iterator`

.`meth (Iterator: iterator):next: iterator | nil`

Advances

`Iterator`

, returning`nil`

if it is finished.`meth (Iterator: iterator):value: any`

Returns the current value produced by

`Iterator`

.`meth (Buffer: string::buffer):append(Value: real::range)`

Appends a representation of

`Value`

to`Buffer`

.`type sequence`

The base type for any sequence value.

`fun all(Sequence: sequence): some | nil`

Returns

`nil`

if`nil`

is produced by`Sequence`

. Otherwise returns`some`

. If`Sequence`

is empty, then`some`

is returned.all([1, 2, 3, 4]) :> some all([1, 2, nil, 4]) :> nil all([]) :> some

`fun batch(Sequence: sequence, Size: integer, Shift?: integer, Function: function): sequence`

Returns a new sequence that calls

`Function`

with each batch of`Size`

values produced by`Sequence`

and produces the results. If a`Shift`

is provided then`Size - Shift`

values of each batch come from the previous batch.list(batch(1 .. 20, 4, tuple)) :> [(1, 2, 3, 4), (5, 6, 7, 8), (9, 10, 11, 12), (13, 14, 15, 16), (17, 18, 19, 20)] list(batch(1 .. 20, 4, 2, tuple)) :> [(1, 2, 3, 4), (3, 4, 5, 6), (5, 6, 7, 8), (7, 8, 9, 10), (9, 10, 11, 12), (11, 12, 13, 14), (13, 14, 15, 16), (15, 16, 17, 18), (17, 18, 19, 20)]

`fun count(Sequence: sequence): integer`

Returns the count of the values produced by

`Sequence`

. For some types of sequences (e.g.`list`

,`map`

, etc), the count is simply retrieved. For all other types, the sequence is iterated and the total number of values counted.count([1, 2, 3, 4]) :> 4 count(1 .. 10 ->? (2 | _)) :> 5

`fun count2(Sequence: sequence): map`

Returns a map of the values produced by

`Sequence`

with associated counts.count2("banana") :> {"b" is 1, "a" is 3, "n" is 2}

`fun distill(Initial?: any, Sequence: sequence, Fn: function): any | nil`

Returns a sequence that produces

`Initial`

,`Fn(Initial, V₁)`

,`Fn(Fn(Initial, V₁), V₂)`

, ... . If`Initial`

is omitted, the first value produced by`Sequence`

is used.list(distill(1 .. 10, +)) :> [1, 3, 6, 10, 15, 21, 28, 36, 45, 55] list(distill(20, 1 .. 10, +)) :> [21, 23, 26, 30, 35, 41, 48, 56, 65, 75]

`fun fold(Sequence: sequence): sequence`

Returns a new sequence that treats alternating values produced by

`Sequence`

as keys and values respectively.map(fold(1 .. 10)) :> {1 is 2, 3 is 4, 5 is 6, 7 is 8, 9 is 10}

`fun grid(Sequence₁, : sequence, ..., Function: any): sequence`

Returns a new sequence that produces

`Function(V₁, V₂, ..., Vₙ)`

for all possible combinations of`V₁, ..., Vₙ`

, where`Vᵢ`

are the values produced by`Sequenceᵢ`

.list(grid(1 .. 3, "cake", [true, false], tuple)) :> [(1, c, true), (1, c, false), (1, a, true), (1, a, false), (1, k, true), (1, k, false), (1, e, true), (1, e, false), (2, c, true), (2, c, false), (2, a, true), (2, a, false), (2, k, true), (2, k, false), (2, e, true), (2, e, false), (3, c, true), (3, c, false), (3, a, true), (3, a, false), (3, k, true), (3, k, false), (3, e, true), (3, e, false)] list(grid(1 .. 3, "cake", *)) :> ["c", "a", "k", "e", "cc", "aa", "kk", "ee", "ccc", "aaa", "kkk", "eee"]

`fun iterate(Sequence: sequence): iterator | nil`

Create an iterator for

`Sequence`

. Returns`nil`

is`Sequence`

is empty.`fun key(Sequence: sequence)`

Returns a new sequence which produces the keys of

`Sequence`

.list(key({"A" is 1, "B" is 2, "C" is 3})) :> ["A", "B", "C"]

`fun max(Sequence: sequence): any | nil`

Returns the largest value (using

`:max`

) produced by`Sequence`

.max([1, 5, 2, 10, 6]) :> 10

`fun max2(Sequence: sequence): tuple | nil`

Returns a tuple with the key and value of the largest value (using

`>`

) produced by`Sequence`

. Returns`nil`

if`Sequence`

is empty.max2([1, 5, 2, 10, 6]) :> (4, 10)

`fun min(Sequence: sequence): any | nil`

Returns the smallest value (using

`:min`

) produced by`Sequence`

.min([1, 5, 2, 10, 6]) :> 1

`fun min2(Sequence: sequence): tuple | nil`

Returns a tuple with the key and value of the smallest value (using

`<`

) produced by`Sequence`

. Returns`nil`

if`Sequence`

is empty.min2([1, 5, 2, 10, 6]) :> (1, 1)

`fun pair(Sequence₁: sequence, Sequence₂: sequence): sequence`

Returns a new sequence that produces the values from

`Sequence₁`

as keys and the values from`Sequence₂`

as values.`fun prod(Sequence: sequence): any | nil`

Returns the product of the values (using

`*`

) produced by`Sequence`

.prod([1, 5, 2, 10, 6]) :> 600

`fun reduce(Initial?: any, Sequence: sequence, Fn: function): any | nil`

Returns

`Fn(Fn( ... Fn(Initial, V₁), V₂) ..., Vₙ)`

where`Vᵢ`

are the values produced by`Sequence`

. If`Initial`

is omitted, the first value produced by`Sequence`

is used.reduce(1 .. 10, +) :> 55 reduce([], 1 .. 10, :put) :> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]

`fun reduce2(Initial?: any, Sequence: sequence, Fn: function): any | nil`

Returns

`Fn(Fn( ... Fn(Initial, K₁, V₁), K₂, V₂) ..., Kₙ, Vₙ)`

where`Kᵢ`

and`Vᵢ`

are the keys and values produced by`Sequence`

. If`Initial`

is omitted, a tuple with the first key and value produced by`Sequence`

is used.reduce2([], "cake", fun(L, K, V) L:put((K, V))) :> [(1, c), (2, a), (3, k), (4, e)]

`fun some(Sequence: sequence): any | nil`

Returns the first value produced by

`Sequence`

that is not`nil`

.some([nil, nil, "X", nil]) :> "X" some([nil, nil, nil, nil]) :> nil

`fun sum(Sequence: sequence): any | nil`

Returns the sum of the values (using

`+`

) produced by`Sequence`

.sum([1, 5, 2, 10, 6]) :> 24

`fun swap(Sequence: sequence)`

Returns a new sequence which swaps the keys and values produced by

`Sequence`

.map(swap("cake")) :> {"c" is 1, "a" is 2, "k" is 3, "e" is 4}

`fun unfold(Sequence: sequence): sequence`

Returns a new sequence that treats produces alternatively the keys and values produced by

`Sequence`

.list(unfold("cake")) :> [1, "c", 2, "a", 3, "k", 4, "e"]

`fun unique(Sequence: sequence): sequence`

Returns an sequence that returns the unique values produced by

`Sequence`

. Uniqueness is determined by using a`map`

.list(unique("banana")) :> ["b", "a", "n"]

`fun unpack(Sequence: sequence): sequence`

Returns a new sequence unpacks each value generated by

`Sequence`

as keys and values respectively.let L := [("A", "a"), ("B", "b"), ("C", "c")] map(unpack(L)) :> {"A" is "a", "B" is "b", "C" is "c"}

`fun weave(Sequence₁, : sequence, ...): sequence`

Returns a new sequence that produces interleaved values

`Vᵢ`

from each of`Sequenceᵢ`

. The sequence stops produces values when any of the`Sequenceᵢ`

stops.list(weave(1 .. 3, "cake")) :> [1, "c", 2, "a", 3, "k"]

`fun zip(Sequence₁, : sequence, ..., Function: any): sequence`

Returns a new sequence that produces

`Function(V₁₁, ..., Vₙ₁), Function(V₁₂, ..., Vₙ₂), ...`

where`Vᵢⱼ`

is the`j`

-th value produced by`Sequenceᵢ`

. The sequence stops produces values when any of the`Sequenceᵢ`

stops.list(zip(1 .. 3, "cake", tuple)) :> [(1, c), (2, a), (3, k)]

`fun zip2(Sequence₁, : sequence, ..., KeyFn: any, ValueFn: any): sequence`

Returns a new sequence that produces

`KeyFn(K₁₁, ..., Kₙ₁) - ValueFn(V₁₁, ..., Vₙ₁), ...`

where`Kᵢⱼ - Vᵢⱼ`

are the`j`

-th key and value produced by`Sequenceᵢ`

. The sequence stops produces values when any of the`Sequenceᵢ`

stops.map(zip2(1 .. 3, "cake", tuple, tuple)) :> {(1, 1) is (1, c), (2, 2) is (2, a), (3, 3) is (3, k)}

`meth &(Sequence: sequence): Sequence`

Returns an sequence that repeatedly produces the values from

`Sequence`

(for use with`limit`

).list(&(1 .. 3) limit 10) :> [1, 2, 3, 1, 2, 3, 1, 2, 3, 1]

`meth (Sequence₁: sequence) & (Sequence₂: sequence): Sequence`

Returns an sequence that produces the values from

`Sequence₁`

followed by those from`Sequence₂`

.list(1 .. 3 & "cake") :> [1, 2, 3, "c", "a", "k", "e"]

`meth (Base: sequence) -> (F: function): sequence`

Returns a chained sequence equivalent to

`(K₁, F(V₁)), ..., (Kₙ, F(Vₙ))`

where`Kᵢ`

and`Vᵢ`

are the keys and values produced by`Base`

.`meth (Base: sequence) ->! (F: function): sequence`

Returns a chained sequence equivalent to

`(K₁, F ! V₁), ..., (Kₙ, F ! Vₙ)`

where`Kᵢ`

and`Vᵢ`

are the keys and values produced by`Base`

.map({"A" is [1, 2], "B" is [3, 4], "C" is [5, 6]} ->! +) :> {"A" is 3, "B" is 7, "C" is 11}

`meth (Base: sequence) ->!? (F: function): sequence`

Returns a chained sequence equivalent to

`(Kⱼ, Vⱼ), ...`

where`Kᵢ`

and`Vᵢ`

are the keys and values produced by`Base`

and`F ! Vⱼ`

returns non-`nil`

.map({"A" is [1, 2], "B" is [3, 3], "C" is [5, 6]} ->!? !=) :> {"A" is [1, 2], "C" is [5, 6]}

`meth (Sequence: sequence) ->> (Function: function): sequence`

Returns a new sequence that generates the keys and values from

`Function(Value)`

for each value generated by`Sequence`

.list(1 .. 5 ->> (1 .. _)) :> [1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5]

`meth (Base: sequence) ->? (F: function): sequence`

Returns a chained sequence equivalent to

`(Kⱼ, Vⱼ), ...`

where`Kᵢ`

and`Vᵢ`

are the keys and values produced by`Base`

and`F(Vⱼ)`

returns non-`nil`

.list(1 .. 10 ->? (2 | _)) :> [2, 4, 6, 8, 10]

`meth (Base: sequence) ->| (F: function): sequence`

Returns a chained sequence equivalent to

`(Kⱼ, Vⱼ), ...`

where`Kᵢ`

and`Vᵢ`

are the keys and values produced by`Base`

while`F(Vⱼ)`

returns non-`nil`

.list(1 .. 10 ->? (5 !| _)) :> [1, 2, 3, 4, 6, 7, 8, 9] list(1 .. 10 ->| (5 !| _)) :> [1, 2, 3, 4]

`meth (Sequence: sequence) // (Initial: any, Fn: function): sequence`

Returns a sequence that produces

`Initial`

,`Fn(Initial, V₁)`

,`Fn(Fn(Initial, V₁), V₂)`

, ... .Deprecated since version 2.9.0: Use

`distill`

instead.list(1 .. 10 // (10, +)) :> [11, 13, 16, 20, 25, 31, 38, 46, 55, 65]

`meth (Sequence: sequence) // (Fn: function): sequence`

Returns a sequence that produces

`V₁`

,`Fn(V₁, V₂)`

,`Fn(Fn(V₁, V₂), V₃)`

, ... .Deprecated since version 2.9.0: Use

`distill`

instead.list(1 .. 10 // +) :> [1, 3, 6, 10, 15, 21, 28, 36, 45, 55]

`meth (Base: sequence) => (F: function): sequence`

Returns a chained sequence equivalent to

`(K₁, F(K₁, V₁)), ..., (Kₙ, F(Kₙ, Vₙ))`

where`Kᵢ`

and`Vᵢ`

are the keys and values produced by`Base`

.map("cake" => *) :> {1 is "c", 2 is "aa", 3 is "kkk", 4 is "eeee"}

`meth (Base: sequence) => (F₁: function, F₂: function): sequence`

Returns a chained sequence equivalent to

`(F₁(K₁, V₁), F₂(K₁, V₁)), ..., (F₁(Kₙ, Vₙ), F₂(Kₙ, Vₙ))`

where`Kᵢ`

and`Vᵢ`

are the keys and values produced by`Base`

.map("cake" => (tuple, *)) :> {(1, c) is "c", (2, a) is "aa", (3, k) is "kkk", (4, e) is "eeee"}

`meth (Sequence: sequence) =>> (Function: function): sequence`

Returns a new sequence that generates the keys and values from

`Function(Key, Value)`

for each key and value generated by`Sequence`

.list("cake" =>> *) :> ["c", "a", "a", "k", "k", "k", "e", "e", "e", "e"]

`meth (Base: sequence) =>? (F: function): sequence`

Returns a chained sequence equivalent to

`(Kⱼ, Vⱼ), ...`

where`Kᵢ`

and`Vᵢ`

are the keys and values produced by`Base`

and`F(Kⱼ, Vⱼ)`

returns non-`nil`

.let M := map(1 .. 10 -> fun(X) X ^ 2 % 10) :> {1 is 1, 2 is 4, 3 is 9, 4 is 6, 5 is 5, 6 is 6, 7 is 9, 8 is 4, 9 is 1, 10 is 0} map(M =>? !=) :> {2 is 4, 3 is 9, 4 is 6, 7 is 9, 8 is 4, 9 is 1, 10 is 0}

`meth (Base: sequence) =>| (F: function): sequence`

Returns a chained sequence equivalent to

`(Kⱼ, Vⱼ), ...`

where`Kᵢ`

and`Vᵢ`

are the keys and values produced by`Base`

while`F(Kⱼ, Vⱼ)`

returns non-`nil`

.let M := map(1 .. 10 -> fun(X) X ^ 2 % 10) :> {1 is 1, 2 is 4, 3 is 9, 4 is 6, 5 is 5, 6 is 6, 7 is 9, 8 is 4, 9 is 1, 10 is 0} map(M =>? fun(K, V) K + V < 15) :> {1 is 1, 2 is 4, 3 is 9, 4 is 6, 5 is 5, 6 is 6, 8 is 4, 9 is 1, 10 is 0} map(M =>| fun(K, V) K + V < 15) :> {1 is 1, 2 is 4, 3 is 9, 4 is 6, 5 is 5, 6 is 6}

`meth (Sequence: sequence) ^ (Function: function): sequence`

Returns a new sequence that generates the keys and values from

`Function(Value)`

for each value generated by`Sequence`

.Deprecated since version 2.5.0: Use

`->>`

instead.list(1 .. 5 ^ (1 .. _)) :> [1, 1, 2, 1, 2, 3, 1, 2, 3, 4, 1, 2, 3, 4, 5]

`meth (Sequence: sequence):find(Value: any): any | nil`

Returns the first key generated by

`Sequence`

with correspding value`= Value`

.`meth (Sequence: sequence):first: any | nil`

Returns the first value produced by

`Sequence`

.first("cake") :> "c" first([]) :> nil

`meth (Sequence: sequence):first2: tuple(any, any) | nil`

Returns the first key and value produced by

`Sequence`

.first2("cake") :> (1, c) first2([]) :> nil

`meth (Sequence: sequence):generate(Function: function): function`

Returns a new function that returns

`Function(Key, Value)`

where`Key`

and`Value`

are the next key-value pair generated by`Sequence`

. Once`Sequence`

is exhausted, the function returns`nil`

.let f := "cat" generate tuple :> <generator> f() :> (1, c) f() :> (2, a) f() :> (3, t) f() :> nil f() :> nil

`meth (Arg₁: sequence):group(Arg₂: function)`

*TBD*`meth (Sequence: sequence):join: string`

Joins the elements of

`Sequence`

into a string.1 .. 10 join "," :> "1,2,3,4,5,6,7,8,9,10"

`meth (Sequence: sequence):join(Separator: string): string`

Joins the elements of

`Sequence`

into a string using`Separator`

between elements.(1 .. 10):join :> "12345678910"

`meth (Sequence: sequence):last: any | nil`

Returns the last value produced by

`Sequence`

.last("cake") :> "e" last([]) :> nil

`meth (Sequence: sequence):last2: tuple(any, any) | nil`

Returns the last key and value produced by

`Sequence`

.last2("cake") :> (4, e) last2([]) :> nil

`meth (Sequence: sequence):limit(Limit: integer): sequence`

Returns an sequence that produces at most

`Limit`

values from`Sequence`

.list(1 .. 10) :> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] list(1 .. 10 limit 5) :> [1, 2, 3, 4, 5]

`meth (Sequence: sequence):provided(Fn: function): sequence`

Returns an sequence that stops when

`Fn(Value)`

is`nil`

.list("banana") :> ["b", "a", "n", "a", "n", "a"] list("banana" provided (_ != "n")) :> ["b", "a"]

`meth (Sequence: sequence):random(...): any | nil`

Returns a random value produced by

`Sequence`

.random("cake") :> "k" random([]) :> nil

`meth (Sequence: sequence):skip(Skip: integer): sequence`

Returns an sequence that skips the first

`Skip`

values from`Sequence`

and then produces the rest.list(1 .. 10) :> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] list(1 .. 10 skip 5) :> [6, 7, 8, 9, 10]

`meth (Arg₁: sequence):split(Arg₂: function)`

*TBD*`type split < sequence`

*TBD*`meth (Arg₁: type):random(...)`

*TBD*