Skip to content

ard/list

The ard/list module provides a small set of generic helpers for working with Ard lists.

It is available from the prelude as List. You can also import ard/list when you want the lowercase module namespace explicitly:

use ard/list

Create a new empty list. The expected type is usually inferred from context, or named with an explicit type argument.

use ard/list
let nums: [Int] = list::new()

An explicit type argument composes with mut to start an empty mutable list without a binding annotation. Through the prelude alias this needs no import:

let items = mut List::new<Int>()
items.push(1)

Return a new list containing the elements of a followed by the elements of b.

use ard/list
let combined = list::concat([1, 2, 3], [4, 5])

Return a new list containing elements whose index is greater than or equal to till.

use ard/list
let tail = list::drop([1, 2, 3, 4], 2) // [3, 4]

Return a new list containing only values for which where returns true.

use ard/list
let evens = list::keep([1, 2, 3, 4], fn(n: Int) Bool { n % 2 == 0 })

map(list: [$A], transform: fn($A) $B) [$B]

Section titled “map(list: [$A], transform: fn($A) $B) [$B]”

Transform each element and return the transformed values in order.

use ard/list
let doubled = list::map([1, 2, 3], fn(n: Int) Int { n * 2 })

Return the first matching value, or none if no value matches.

use ard/list
let found = list::find([1, 2, 3], fn(n: Int) Bool { n == 2 })

contains(list: [$T], where: fn($T) Bool) Bool

Section titled “contains(list: [$T], where: fn($T) Bool) Bool”

Return true when any element matches the predicate. Evaluation stops after the first match.

use ard/list
let has_even = list::contains([1, 2, 3], fn(n: Int) Bool { n % 2 == 0 })

Matching is defined entirely by the predicate, so contains works with any element type accepted by find.

partition(list: [$T], where: fn($T) Bool) Partition<$T>

Section titled “partition(list: [$T], where: fn($T) Bool) Partition<$T>”

Split a list into matching and non-matching values.

use ard/list
let parts = list::partition([1, 2, 3, 4], fn(n: Int) Bool { n > 2 })
// parts.selected == [3, 4]
// parts.others == [1, 2]

partition_int(list: [Int], where: fn(Int) Bool) IntPartition

Section titled “partition_int(list: [Int], where: fn(Int) Bool) IntPartition”

Specialized Int version of partition.

struct Partition {
selected: [$T],
others: [$T],
}
struct IntPartition {
selected: [Int],
others: [Int],
}