List
Helios linked-list type.
The syntax for a list type uses empty brackets followed by a type parameter: []ItemType
.
Example:
example: []Int = []Int{1, 2, 3, 4, 5}; ...
Looping over multiple lists at once can be done with a recursive function call:
func add_element_wise(a: []Int, b: []Int) -> []Int {
if (a.is_empty()) {
[]Int{}
} else {
add_element_wise(a.tail, b.tail).prepend(a.head + b.head)
}
}
Associated Functions
from_data
[]ItemType::from_data(data: Data) -> []ItemType
new
Creates a new list of length n
, where every contained item is determined by fn(i: Int)
(i
is the 0-based index of the item).
[]ItemType::new(n: Int, fn: (i: Int) -> ItemType) -> []ItemType
new_const
Creates a new list of length n
, where very contained item is the same.
[]ItemType::new_const(n: Int, item: ItemType) -> []ItemType
Getters
length
Returns the length of a list.
list.length -> Int
head
Returns the first item in a list. Throws an error if the list is empty.
list.head -> ItemType
tail
Returns the list items following the first item. Throws an error if the list is empty.
list.tail -> []ItemType
Note:
tail
doesn't return the last item in a list but returns everything afterhead
as a new list.
Operators
==
[]ItemType == []ItemType -> Bool
!=
[]ItemType != []ItemType -> Bool
+
List concatenation
[]ItemType + []ItemType -> []ItemType
Methods
all
Returns true
if all of the items in the list satisfy the predicate.
list.all(predicate: (ItemType) -> Bool) -> Bool
any
Returns true
if any of the items in the list satisfy the predicate.
list.any(predicate: (ItemType) -> Bool) -> Bool
filter
Returns a list of all the items in the old list that satisfy the predicate.
list.filter(predicate: (ItemType) -> Bool) -> []ItemType
find
Returns the first item in the list that satisfies the predicate. Throws an error if no item satisfies the predicate.
list.find(predicate: (ItemType) -> Bool) -> ItemType
find_safe
Returns the first item in the list that satisfies the predicate, wrapped in an Option
. Returns a Option[ItemType]::None
if no items match the predicate.
list.find_safe(predicate: (ItemType) -> Bool) -> Option[ItemType]
fold
Folds a list into a single value by continuosly applying the binary function to the items of the list. The result type is a type parameter of this method: ReducedType
.
list.fold(
reducer: (ReducedType, ItemType) -> ReducedType,
init: ReducedType
) -> ReducedType
fold_lazy
Fold that allows breaking the loop before reaching the end of the list. Can also be used to fold from the last to the first entry of the list instead of the other way around.
list.fold_lazy(
reducer: (item: ItemType, next: () -> ReducedType) -> ReducedType,
final: ReducedType
) -> ReducedType
for_each
Print or assert something for each item. Returns void.
list.for_each(fn: (item: ItemType) -> ()) -> ()
get
Returns the item at the given position in the list (0-based index). Throws an error if the index is out of range.
list.get(index: Int) -> ItemType
Note:
get(n)
has to iterate until it encounters then
-th item, so this method is O(n) and not O(1).
is_empty
Returns true
if the list is empty.
list.is_empty() -> Bool
map
Transforms each item of a list. The resulting list item type is a type parameter of this method: NewItemType
.
list.map(mapper: (ItemType) -> NewItemType) -> []NewItemType
prepend
Creates a new list by prepending an item to the old list.
list.prepend(item: ItemType) -> []ItemType
serialize
list.serialize() -> ByteArray
sort
Sorts the list using insertion sort.
list.sort((a: ItemType, b: ItemType) -> Bool) -> []ItemType