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

sorted_array

container.sorted_array

sorted_array -- sorted one-dimensional immutable array

This takes an unsorted array and a compare function as an arguments and
returns a sorted one.

Non-mutating heap sort is used internally. This gives guaranteed peformance in
O(n log n) comparisons and assignments for an array of size n.

This is a little wasteful on allocated memory, which is also O(n log n) since
partially sorted arrays are thrown away. This might be improved to use an
in-place heap sort to bring allocated memory down to O(n).

Functions

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.
§
:
Any
 => 
list array.T 
[Inherited from  array]
create a list from this array

redefines:

§(i i32)
:
Any
 => 
list array.T 
[Inherited from  array]
create a list from this array starting at the given index
convenience feature to work around type inference issues
NYI remove when type inference gets better
§
:
Any
 => 
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 ']'.

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:

§(sep String)
:
Any
 => 
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.
§
:
Any
 => 
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.
call 'as_string' on the elements
create a new list that contains the first elements of
this Sequence for which 'f e' is false
§(chunk_size i32)
:
Any
 => 
list (list Sequence.T) 
[Inherited from  Sequence]
chop this Sequence into chunks of `chunk_size`.
the last chunk may be smaller than `chunk_size`.
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 Sequence that consists of all the elements of this Sequence followed
by all the elements of s
§
:
Any
 => 
i32 
[Inherited from  array]
redefines Sequence.count for array,
reducing complexity from O(n) to O(1).

redefines:

§
:
Any
 => 
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)
:
Any
 => 
list array.T 
[Inherited from  array]
create a list that consists of the elements of this Sequence except the first
n elements

For arrays, this has performance in O(1).

redefines:

Lazily drop the first elements of this Sequence for which predicate 'p' holds.
§
:
Any
 => 
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 hierachy of types. So for Type values, dynamic_type is redefined
to just return Type.type.
get a list of tuples indices and elements in this array
filter elements using predicate f
values for which f is false are dropped
find index of key for which cmp returns 0

The guaranteed performance is in O(log n) comparisons.

result is the index where cmp results in 0 or nil if no such index
was found in this array. In case several instance equal match,
the index of one matching key will be returned, but is not
specified which one.
§(key container.sorted_array.T)
:
Any
 => 
option i32
find index of given key using binary search

The guaranteed performance is in O(log n) comparisons.

result is the index where key was found or nil if key is not
in this array. In case several instance equal to key are in
this sorted_array, the index of one of the matching keys will be
returned, but is not specified which one.
§
:
Any
 => 
bool 
[Inherited from  array]
is this sequence known to be finite? For infinite sequences, features like
count diverge.

redefines:

§
:
Any
 => 
Sequence.T 
[Inherited from  Sequence]
get the first element of this Sequence

Sequence must not be empty, causes precondition failure if debug is enabled.
§(default Sequence.T)
:
Any
 => 
Sequence.T 
[Inherited from  Sequence]
get the first element of this Sequence or default if sequence is empty
map each element of this Sequence to Sequence
Then flatten the result by one level,
essentially combining all the sequences.
§(m Monoid array.T)
:
Any
 => 
array.T 
[Inherited from  array]
fold the elements of this array using the given monoid.

e.g., to sum the elements of an array of i32, use a.fold i32.sum

redefines:

§(i i32, s array.T, m Monoid array.T)
:
Any
 => 
array.T 
[Inherited from  array]
fold the elements of this array using the given monoid and initial value

Used to fold an array tail-recursively
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 Function Sequence.foldf.B Sequence.foldf.B Sequence.T, e Sequence.foldf.B)
:
Any
 => 
Sequence.foldf.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 array.T)
:
Any
 => 
unit 
[Inherited from  array]
apply f to all elements in this array

redefines:

apply 'f' to each element 'e' as long as 'f e'
group the elements of this sequence by a key of type K

f determines the key of an element
get a function that, given an index, returns the element at that index
§(i i32)
:
Any
 => 
array.T 
[Inherited from  array]
get the contents of this array at the given index
adds the corresponding index to
every element in the sequence
§(I 
type
, start_idx Sequence.indexed.I)
:
Any
 => 
list (tuple Sequence.indexed.I Sequence.T) 
[Inherited from  Sequence]
adds an index to every element
in the sequence starting at start_idx
§
:
Any
 => 
interval i32 
[Inherited from  array]
a sequence of all valid indices to access this array. Useful e.g., for
`for`-loops:

for i in arr.indices do
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
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)
:
Any
 => 
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]
§
:
Any
 => 
bool 
[Inherited from  array]
is this Sequence known to be array backed? If so, this means that operations
like index[] are fast.
§
:
Any
 => 
bool 
[Inherited from  Sequence]
is this Sequence empty?
§(i i32)
:
Any
 => 
bool 
[Inherited from  array]
check if argument is a valid index in this array.

Unlike for general Sequences, this perfoms in O(1).
§
:
Any
 => 
Sequence.T 
[Inherited from  Sequence]
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.
§(default Sequence.T)
:
Any
 => 
Sequence.T 
[Inherited from  Sequence]
get the last element of this Sequence or default if sequence is empty
§
:
Any
 => 
i32 
[Inherited from  array]
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.
variant of map which additionally passes the index to
the mapping function f
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]`
map the array to a new array applying function f to all elements
§
:
Any
 => 
String 
[Inherited from  Type]
name of this type, including type parameters, e.g. 'option (list i32)'.
§(n i32)
:
Any
 => 
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)
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
§
:
Any
 => 
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.
§(i i32, v array.T)
:
Any
 => 
array array.T 
[Inherited from  array]
create a new array with element i set to v. Grow the array in case i == length.

Complexity: O(array.this.length)
§(i i32, v array.T, z array.T)
:
Any
 => 
array array.T 
[Inherited from  array]
create a new array with element i set to v. Grow the array in case i >= length.
New array elements at indices array.this.length..i-1 will be set to z.

Complexity: O(max(i, array.this.length))
§(R 
type
, init Sequence.reduce.R, f Function (choice Sequence.reduce.R (abort Sequence.reduce.R)) Sequence.reduce.R Sequence.T)
:
Any
 => 
Sequence.reduce.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.
§
:
Any
 => 
Sequence array.T 
[Inherited from  array]
reverse the order of the elements in this array

redefines:

§
:
Any
 => 
array array.T 
[Inherited from  array]
reverse the order of the elements in this array
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)
:
Any
 => 
Sequence array.T 
[Inherited from  array]
create a slice from this array's elements at index 'from' (included)
up to 'to' (excluded).

Complexity:
index access : O(1)
count : O(1)

redefines:

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 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.
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)
:
Any
 => 
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.
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 Sequence and 'b' in order.

Type Features

Maximum number of elements shown for on a call to `as_string` for a non-finite
Sequence.
monoid of Sequences with infix concatentation operation.
§(length i32, init Unary array.type.T i32)
:
Any
 is
 
[Inherited from  array]
array -- create initialized one-dimensional immutable array
§
:
Any
 is
 
[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`.