Fuzion Logo
fuzion-lang.dev — The Fuzion Language Portal
JavaScript seems to be disabled. Functionality is limited.

Sequence

Sequence

Sequence -- ancestor for features that can be converted to a 'list' or a
'stream'

Sequences are empty, finite or infinite ordered collections of instances
of a given type. Sequences may calculate elements lazily on demand.

Sequence is a 'ref' type, i.e., different sub-features may be assigned
to a field of type 'Sequence'.

Heirs of Sequence must implement either 'as_list' or 'as_stream'. Failure
to implement any of these results in an endless recursion when the Sequence
is used.

Functions

create a string representation of this Sequence including all the string
representations of its contents, separated by ',' and enclosed in '['
and ']'.

In case this Sequence is known to be `finite` or has at most (Sequence T).type
.AS_STRING_NON_FINITE_MAX_ELEMENTS elements, all elements will be shown in the
resulting string. Otherwise, only the first elements will be shown followed by
",…" as in "[1,2,3,4,5,6,7,8,9,10,…]".

To force printing of all elements of a finite `Sequence` for which `finite` is
false (which may be the case since a Sequence in general might not know that it
if finite), you may use `as_string_all`.

redefines:

create a string representation of this Sequence including all the string
representations of its contents, separated by 'sep'.

NOTE: In case this Sequence is not finite, this will attempt to create an
infinitely long string resulting in failure due to resource exchaustion.
filter elements using predicate f, infix operator
synonym of filter.

NYI: What is better, 'infix |&' or 'infix &', or something else?
map the Sequence to a new Sequence applying function f to all elements

This is an infix operator alias of map enabling piping code like

l := 1..10 | *10 | 300-

to obtain 290,280,270,...200

Note that map and therefore also this operator is lazy, so

_ := (1..10 | say)

will not print anything while

(1..10 | say).for_each _->unit

will print the elements since `for_each` is not lazy.
create a list from this Sequence.

A list is immutable, so it can be reused and shared between threads.

Heirs must provide an implementation of as_list.
is this sequence known to be finite? For infinite sequences, features like
count diverge.
is this Sequence known to be array backed? If so, this means that operations
like index[] are fast.
count the number of elements in this Sequence. Note that this typically
runs forever if executed on an endless list

For lists that are not array backed, this might require time in O(count).
collect the contents of this Sequence into an array
create a list and call 'for_each f' on it
reverse the order of the elements in this Sequence
create a list that consists of the elements of this Sequence except the first
n elements

NOTE: this may have performance in O(n) unless it is backed by an immutable array.
get a function that, given an index, returns the element at that index
§index [ ](i i32)
 => 
Sequence.T
:
Any 
the nth element in the sequence, must exist
create a slice from this Sequence that consists of the elements starting at index
from (including) up to index to (excluding).
fold the elements of this Sequence using the given monoid.

e.g., to sum the elements of a Sequence of i32, use s.fold i32.sum
is this Sequence empty?
§first
 => 
Sequence.T
:
Any 
get the first element of this Sequence

Sequence must not be empty, causes precondition failure if debug is enabled.
§first(default Sequence.T)
 => 
Sequence.T
:
Any 
get the first element of this Sequence or default if sequence is empty
§last
 => 
Sequence.T
:
Any 
get the last element of this Sequence

Sequence must not be empty, causes precondition failure if debug is enabled.

This may take time in O(count), in particular, it may not terminate
for an infinite Sequence.
§last(default Sequence.T)
 => 
Sequence.T
:
Any 
get the last element of this Sequence or default if sequence is empty
create an array backed version of this sequence in case this is not array
backed. This will ensure that operations like index[] or drop perform
in constant time.

returns Sequence.this if is_array_backed.
calls `f` for element in the Sequence.

Unlike `for_each` this returns itself
allowing easier composition with
other Sequence features.

example:
[1,2,3,4,5]
.filter is_prime
.peek say
.drop_while <10
consume all elements of this Sequence by f. This is an infix operator alias
for for_each.

Ex.: To print all the elements of a list, you can use

1..10 ! say
apply 'f' to each element 'e' as long as 'f e'
create a new list that contains the first elements of
this Sequence for which 'f e' is false
filter elements using predicate f
values for which f is false are dropped
filter elements using predicate f, infix operator
synonym of filter.
check if predicate f holds for all elements
check if predicate f holds for at least one element
create a list that consists only of the first n elements of this
Sequence, fewer if this Sequence has fewer elements
create a tuple of two Sequences by splitting this at the given index, i.e.,
a Sequence of length 'at' and one of length 'count-at'.

at may be <= 0 or >= count, in which case the resulting tuple will be the
(empty list, Sequence.this.as_list) or (Sequence.this.as_list, empty list), resp.
Lazily take the first elements of this Sequence for which predicate 'p' holds.
Lazily drop the first elements of this Sequence for which predicate 'p' holds.
create a Sequence that consists of all the elements of this Sequence followed
by all the elements of s
infix operand synonym for concat_sequences
create a list that repeats the current Sequence indefinitely. In case 'Sequence.this'
is empty, returns 'nil'
create a lazy list of all the tails of this Sequence, including the complete Sequence
as a list and the empty list 'nil'.
create a string representation of this Sequence including all the string
representations of its contents, separated by ", " and enclosed in '['
and ']'.

NOTE: In case this Sequence is not finite, this will attempt to create an
infinitely long string resulting in failure due to resource exchaustion.
call 'as_string' on the elements
map the Sequence to a new Sequence applying function f to all elements

This performs a lazy mapping, f is called only when the elements
in the resulting list are accessed.
map each element of this Sequence to Sequence
Then flatten the result by one level,
essentially combining all the sequences.
Map this Sequence to f applied to neighboring pairs of values
in this Sequence.

In case this Sequence has less than two elements, the result will
be the empty list.

ex. to obtain a list of differences you, you may use `map_pairs (-)`:

[2,3,5,7,11,13,17,19,23,29].map_pairs a,b->b-a

results in `[1,2,2,4,2,4,2,4,6]`
fold the elements of this non-empty Sequence using the given function

e.g., to find the minimum of a Sequence of i32, use `s.fold1 (<=)`
§foldf(B 
type
, f Function Sequence.foldf.B Sequence.foldf.B Sequence.T, e Sequence.foldf.B)
 => 
Sequence.foldf.B
:
Any 
fold the elements of this Sequence using the given function and initial
value.

In case this Sequence is empty, the result is `e`.

e.g., to find the product of a Sequence of i32, use `s.foldf (*) 1`
map this Sequence to a list that contains the result of folding
all prefixes using the given monoid.

e.g., for a Sequence of i32 s, s.scan i32.sum creates a list of
partial sums (0..).map x->(s.take x).fold i32.sum
reduce this Sequence to R with an initial value init
and a reducing function f.
the reduction is finished once f yields abort or
if the end of the sequence is reached.
reduce this Sequence to `outcome R`
with an initial value `init` and a reducing function `f`.
the reduction is finished once `f` yields `abort` or
if the end of the sequence is reached.
§insert(at i32, v Sequence.T)
 => 
list Sequence.T
:
Any 
insert element v at position at
sort this Sequence using the total order defined by less_or_equal
sort this Sequence using total order defined for 'f a[i]'
create a new list from the result of applying 'f' to the
elements of this Sequence and 'b' in order.
create a new Sequence from the result of applying 'f' to the
elements all combinations of elements of this Sequence and
all elements of 'b' iterating of 'b' repeatedly as follows

Sequence.this[0] , b[0]
Sequence.this[0] , b[1]
Sequence.this[0] , b[2]
Sequence.this[0] , ...
Sequence.this[0] , b.last
Sequence.this[1] , b[0]
Sequence.this[1] , b[1]
Sequence.this[1] , ...
... , ...
Sequence.this.last, b.last
create a new Sequence from tuples of all combinations of elements
of this Sequence and all elements of 'b' iterating of 'b' repeatedly
as follows

(Sequence.this[0] , b[0] )
(Sequence.this[0] , b[1] )
(Sequence.this[0] , b[2] )
(Sequence.this[0] , ... )
(Sequence.this[0] , b.last)
(Sequence.this[1] , b[0] )
(Sequence.this[1] , b[1] )
(Sequence.this[1] , ... )
(... , ... )
(Sequence.this.last, b.last)
takes a transducer xf, a reducer f and an initial value
returns the result of applying the reducer xf f to the Sequence
apply transducer to sequence, returning a sequence of results

example usage:
human(age i32) is
ages := map (Sequence i32) human i32 (x -> x.age)
gt_ten := filter (Sequence i32) i32 (x -> x > 10)
xf := ages ∘ gt_ten
say ([human(4), human(12), human(30)].into xf) # [12,30]
the nth element in the sequence if it exists, wrapped in an option,
nil otherwise.
adds the corresponding index to
every element in the sequence
§indexed(I 
type
, start_idx Sequence.indexed.I)
 => 
list (tuple Sequence.indexed.I Sequence.T)
:
Any 
adds an index to every element
in the sequence starting at start_idx
convenience feature to work around type inference issues
NYI remove when type inference gets better
chop this Sequence into chunks of `chunk_size`.
the last chunk may be smaller than `chunk_size`.
group the elements of this sequence by a key of type K

f determines the key of an element

Type Features

monoid of Sequences with infix concatentation operation.
Maximum number of elements shown for on a call to `as_string` for a non-finite
Sequence.