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

Mutable_Linked_List

container.Mutable_Linked_List

§
(LM 
type
, T 
type
)
 ref
:
Linked_List T
 is
 
[Private constructor]

a type for mutable singly linked lists

On call to `Mutable_Linked_List LM T data` creates a minimal list consisting
of only one single element. To create larger rings, you can either call
`append` to add single cells, or `concat` to concatenate two lists.

Type Parameters

§
LM
:
Any 
[Private constructor]
§
T
:
Any 
[Private constructor]
type of data stored in this list

Fields

§
data
 T  
[Redefinition of  container.Linked_List.data]
the data stored in this element.

Functions

§
(data container.Mutable_Linked_List.T)
 => 
unit
append an element to the linked list
§
 => 
array Sequence.T  
[Inherited from  Sequence]
collect the contents of this Sequence into an array
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.
return this linked list as a list

redefines:

§
 => 
Sequence Sequence.T  
[Inherited from  Sequence]
convenience feature to work around type inference issues
NYI remove when type inference gets better
§
(sep String)
 => 
String  
[Inherited from  Sequence]
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.
§
 => 
String  
[Inherited from  Linked_List]
a representation of this linked list as a string.
§
 => 
String  
[Inherited from  Sequence]
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.
§
 => 
Sequence String  
[Inherited from  Sequence]
call 'as_string' on the elements
§
 => 
option Sequence.T  
[Inherited from  Sequence]
the arithmetic mean of the sequence
https://en.wikipedia.org/wiki/Arithmetic_mean
create a new list that contains the first elements of
this Sequence for which 'f e' is false
§
(chunk_size i32)
 => 
list (list Sequence.T)  
[Inherited from  Sequence]
chop this Sequence into chunks of `chunk_size`.
the last chunk may be smaller than `chunk_size`.
§
(U 
type
, V 
type
, b Sequence U, f Binary V Sequence.T U)
 => 
Sequence V  
[Inherited from  Sequence]
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
append an entire list to this linked list
create a Sequence that consists of all the elements of this Sequence followed
by all the elements of s
§
(x Sequence.T)
 => 
bool  
[Inherited from  Sequence]
does the Sequence contain element x?
§
 => 
i32  
[Inherited from  Sequence]
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).
§
(l Sequence Sequence.T)
 => 
i32  
[Inherited from  Sequence]
get the number of non-overlapping matches of l within this
get the number of matches of l
§
 => 
list Sequence.T  
[Inherited from  Sequence]
create a list that repeats the current Sequence indefinitely. In case 'Sequence.this'
is empty, returns 'nil'
§
(n i32)
 => 
list Sequence.T  
[Inherited from  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.
Lazily drop the first elements of this Sequence for which predicate 'p' holds.
§
 => 
Type  
[Inherited from  Any]
Get the dynamic type of this instance. For value instances `x`, this is
equal to `type_of x`, but for `x` with a `ref` type `x.dynamic_type` gives
the actual runtime type, while `type_of x` results in the static
compile-time type.

There is no dynamic type of a type instance since this would result in an
endless hierarchy of types. So for Type values, dynamic_type is redefined
to just return Type.type.
§
 => 
Sequence.T  
[Inherited from  Sequence]
the euclidean norm of this sequence
i.e. the square of the sum of squares of this sequence
filter elements using predicate f
values for which f is false are dropped
§
(pattern Sequence Sequence.T)
 => 
option i32  
[Inherited from  Sequence]
get the index of pattern within this Sequence or nil if it does not exist

uses the Knuth-Morris-Pratt algorithm
port of racket code from this paper:
https://www.cambridge.org/core/services/aop-cambridge-core/content/view/8EFA77D663D585B68630E372BCE1EBA4/S0956796824000017a.pdf/knuth-morris-pratt-illustrated.pdf

worst-case performance: O( seq_length ) + O( pattern_length )
worst-case space complexity: O( pattern_length )
§
 => 
trit  
[Inherited from  Sequence]
is this sequence known to be finite? For infinite sequences, features like
count diverge.

TRUE = known finite
FALSE = known infinite
nil = unknown
§
 => 
option Sequence.T  
[Inherited from  Sequence]
get the first element of this Sequence
§
(default Sequence.T)
 => 
Sequence.T  
[Inherited from  Sequence]
get the first element of this Sequence or default if sequence is empty
§
(B 
type
, f Unary (Sequence B) Sequence.T)
 => 
Sequence B  
[Inherited from  Sequence]
map each element of this Sequence to Sequence
Then flatten the result by one level,
essentially combining all the sequences.
§
(m Monoid Sequence.T)
 => 
Sequence.T  
[Inherited from  Sequence]
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
§
(f Binary Sequence.T Sequence.T Sequence.T)
 => 
Sequence.T  
[Inherited from  Sequence]
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 (<=)`
§
(B 
type
, f Binary B B Sequence.T, e B)
 => 
B  
[Inherited from  Sequence]
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`
§
(f Unary unit Sequence.T)
 => 
unit  
[Inherited from  Sequence]
create a list and call 'for_each f' on it
§
(f Unary bool Sequence.T)
 => 
unit  
[Inherited from  Sequence]
apply 'f' to each element 'e' as long as 'f e'
freeze this list, i.e., turn all references into immutable values
get a function that, given an index, returns the element at that index
§
(i i32)
 => 
Sequence.T  
[Inherited from  Sequence]
the nth element in the sequence, must exist
§
(x Sequence.T)
 => 
option i32  
[Inherited from  Sequence]
determine the index of element x within this list. 0 if x is at the
head of the list, 1 if it comes directly after head, etc. nil if x is
not in the list.
adds the corresponding index to
every element in the sequence
§
(I 
type
, start_idx I)
 => 
list (tuple I Sequence.T)  
[Inherited from  Sequence]
adds an index to every element
in the sequence starting at start_idx
§
(f Unary unit Sequence.T)
 => 
unit  
[Inherited from  Sequence]
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
filter elements using predicate f, infix operator
synonym of filter.

NYI: What is better, 'infix |&' or 'infix &', or something else?
infix operand synonym for concat_sequences
§
(B 
type
, f Unary B Sequence.T)
 => 
Sequence B  
[Inherited from  Sequence]
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.
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
§
(at i32, v Sequence.T)
 => 
list Sequence.T  
[Inherited from  Sequence]
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]
§
 => 
bool  
[Inherited from  Sequence]
is this Sequence known to be array backed? If so, this means that operations
like index[] are fast.
§
 => 
bool  
[Inherited from  Sequence]
is this Sequence empty?
§
(i i32)
 => 
bool  
[Inherited from  Sequence]
check if argument is a valid index in this sequence.

Note that this may have a performance in O(i) unless this
Sequence is_array_backed.
§
 => 
option Sequence.T  
[Inherited from  Sequence]
get the last element of this Sequence

This may take time in O(count), in particular, it may not terminate
for an infinite Sequence.
§
(default Sequence.T)
 => 
Sequence.T  
[Inherited from  Sequence]
get the last element of this Sequence or default if sequence is empty
§
(B 
type
, f Unary B Sequence.T)
 => 
Sequence B  
[Inherited from  Sequence]
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.
§
(B 
type
, f Binary B Sequence.T Sequence.T)
 => 
list B  
[Inherited from  Sequence]
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]`
§
 => 
option Sequence.T  
[Inherited from  Sequence]
maximum value in the sequence
§
 => 
option Sequence.T  
[Inherited from  Sequence]
the median of the sequence
https://en.wikipedia.org/wiki/Median
§
 => 
option Sequence.T  
[Inherited from  Sequence]
minimum value in the sequence
short-hand features to get the mutable references from `n`
§
(n i32)
 => 
option Sequence.T  
[Inherited from  Sequence]
the nth element in the sequence if it exists, wrapped in an option,
nil otherwise.

Complexity: if Sequence is array backed O(1) otherwise O(n)
§
(U 
type
, b Sequence U)
 => 
Sequence (tuple Sequence.T U)  
[Inherited from  Sequence]
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)
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
§
 => 
String  
[Inherited from  Any]
convenience prefix operator to create a string from a value.

This permits usage of `$` as a prefix operator in a similar way both
inside and outside of constant strings: $x and "$x" will produce the
same string.
§
(R 
type
, init R, f Binary (choice R (abort R)) R Sequence.T)
 => 
R  
[Inherited from  Sequence]
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.
replace all occurrences of old by new
replace the first n occurrences of old by new
§
 => 
Sequence Sequence.T  
[Inherited from  Sequence]
reverse the order of the elements in this Sequence
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
§
(from i32, to i32)
 => 
Sequence Sequence.T  
[Inherited from  Sequence]
create a slice from this Sequence that consists of the elements starting at index
from (including) up to index to (excluding).
sort this Sequence
sort this Sequence using the order defined by less_or_equal
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.
§
(l Sequence Sequence.T)
 => 
bool  
[Inherited from  Sequence]
does this sequence start with l?
§
 => 
Sequence Sequence.T  
[Inherited from  Sequence]
the standard deviation of the sequence
https://en.wikipedia.org/wiki/Standard_deviation
create a lazy list of all the tails of this Sequence, including the complete Sequence
as a list and the empty list 'nil'.
§
(n i32)
 => 
list Sequence.T  
[Inherited from  Sequence]
create a list that consists only of the first n elements of this
Sequence, fewer if this Sequence has fewer elements
Lazily take the first elements of this Sequence for which predicate 'p' holds.
§
(TA 
type
, U 
type
, xf transducer TA U Sequence.T, rf Binary TA TA U, init TA)
 => 
TA  
[Inherited from  Sequence]
takes a transducer xf, a reducer f and an initial value
returns the result of applying the reducer xf f to the Sequence
§
 => 
option Sequence.T  
[Inherited from  Sequence]
the variance of the sequence
https://en.wikipedia.org/wiki/Variance
§
(U 
type
, V 
type
, b Sequence U, f Binary V Sequence.T U)
 => 
list V  
[Inherited from  Sequence]
create a new list from the result of applying 'f' to the
elements of this Sequence and 'b' in order.

Type Functions

§
 => 
String  
[Inherited from  Type]
string representation of this type to be used for debugging.

result has the form "Type of '<name>'", but this might change in the future

redefines:

Maximum number of elements shown for on a call to `as_string` for a non-finite
Sequence.
monoid of Sequences with infix concatentation operation.
§
 => 
Type  
[Inherited from  Type]
There is no dynamic type of a type instance since this would result in an
endless hierarchy of types, so dynamic_type is redefined to just return
Type.type here.

redefines:

§
(a property.equatable.this.type, b property.equatable.this.type)
 => 
bool  
[Inherited from  equatable]
  
[Abstract feature]
equality implements the default equality relation for values of this type.

This relation must be

- reflexive (equality a b),
- symmetric (equality a b = equality b a), and
- transitive ((equality a b && equality b c) : equality a c).

result is true iff 'a' is considered to represent the same abstract value
as 'b'.
§
(T 
type
)
 => 
bool  
[Inherited from  Type]
Is this type assignable to a type parameter with constraint `T`?

The result of this is a compile-time constant that can be used to specialize
code for a particular type.

is_of_integer_type(n T : numeric) => T : integer
say (is_of_integer_type 1234) # true
say (is_of_integer_type 3.14) # false

it is most useful in conjunction preconditions or `if` statements as in

pair(a,b T) is

=>

or

val(n T) is

§
 => 
bool  
[Inherited from  Sequence]
Sequence is equatable only if T is
§
 => 
String  
[Inherited from  Type]
name of this type, including type parameters, e.g. 'option (list i32)'.
create a mutable linked list from a given sequence

this results in a mutable linked list that is not yet frozen, i.e. it
can still be modified. however, the list needs to be frozen manually
before leaving the mutate environment.
create a mutable linked list from a given sequence

the argument freeze specifies whether the resulting list is frozen. if
true, the list is frozen after creation, i.e. it can no longer be changed,
but the mutate environment can be left.
§
 => 
String  
[Inherited from  Type]
convenience prefix operator to create a string from a value.

This permits usage of `$` as a prefix operator in a similar way both
inside and outside of constant strings: $x and "$x" will produce the
same string.

NYI: Redefinition allows the type feature to be distinguished from its normal counterpart, see #3913

redefines:

§
 => 
Type  
[Inherited from  Any]
Get a type as a value.

This is a feature with the effect equivalent to Fuzion's `expr.type` call tail.
It is recommended to use `expr.type` and not `expr.type_value`.

`type_value` is here to show how this can be implemented and to illustrate the
difference to `dynamic_type`.