Fuzion Logo
flang.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 'asList' or 'asStream'. Failure
to implement any of these results in an endless recursion when the Sequence
is used.

call 'asString' on the elements

collect the contents of this Sequence into an array

create a list from this Sequence.

A list is immutable, so it can be reused and shared between threads.
Compared to a stream, a list may require more (heap) allocation.

Default implementation uses asStream. Heirs must redefine at least
one of asList or asStream.

create a stream of T.

A stream contains mutable state, so it cannot be reused or shared
between threads.

Default implementation uses asList. Heirs must redefine at least
one of asList or asStream.

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

redefines Object.asString:

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

create a new stream that contains the first elements of this stream for
which 'f e' is false

create a Sequence that consists of all the elements of this Sequence followed
by all the elements of s

count the number of elements in this Sequence. Note that this typically
runs forever if executed on an endless list

create a list that repeats the current Sequence indefinitely. In case 'Sequence.this'
is empty, returns 'nil'

create a list that consists of the elements of this Sequence except the first
n elements

Lazily drop the first elements of a list for which predicate 'p' holds.

create a new stream and filter its elements using predicate f

get the head of this list, panic if list is empty

§  first(default T)
 => 
T
:
Object

get the head of this list or default if sequence is empty

NYI "error 1: Cannot redefine feature with generic arguments
To solve this, ask the Fuzion team to remove this restriction :-)"

flatMap(B type, f T -> Sequence<B>) =>
asList.flatMap f

fold the elements of this Sequence using the given monoid.

e.g., to sum the elements of a stream of i32, use s.fold i32.sum

create a stream and call 'forAll f' on it

create a new stream and apply 'f' to each element 'e' as long as 'f e'

create a new stream and filter its elements using predicate f, infix operator
synonyme of filter.

NYI: What is better, 'infix |&' or 'infix &', or something else?

infix operand synonyme for concatSequences

create a stream and have it consumed by f, infix operator synonyme of forAll.

create a new stream and filter its elements using predicate f, infix operator
synonyme of filter.

create a stream and check if predicate f holds for all elements produced

create a stream and check if predicate f holds for at least one element produced

insert element v at position at

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]

is this list empty?

get the last element of this list, panic if list is empty

This may take time in O(count), in particular, it may not terminate
for an infinite list.

§  last(default T)
 => 
T
:
Object

get the last element of this list or default if sequence is empty

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 list are accessed.

NYI: As long as covariance for result type is not permitted we cannot
call this 'map' since this would clash with, e.g., 'array.map'

the nth element in the sequence,

create a stream, infix operator synonyme for asStream

reduce this Sequence to R with an initial value init
and a reducing function f

create a slice from this Sequence that consists of the elements starting at index
from (including) up to index to (excluding).

sort this Sequence using the total order defined by lessOrEqual

sort this Sequence using total order defined for 'f a[i]'

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.asList) or (Sequence.this.asList, empty list), resp.

create a lazy list of all the tails of this list, including the complete list
'list.this' and the empty list 'nil'.

create a list that consists only of the first n elements of this
Sequence, fewer if this stream has fewer elements

Lazily take the first elements of a list for which predicate 'p' holds.

takes a transducer xf, a reducer f and an initial value
returns the result of applying the reducer xf f to the Sequence

create a new list from the result of applying 'f' to the
elements of this list and 'b' in order.