Skip to content

List Operations with ard/list

The ard/list module provides functional operations for working with lists.

The list module provides:

  • List creation with List::new()
  • Functional transformations like map, filter, and find
  • List utilities for concatenation, dropping elements, and partitioning
use ard/list
fn main() {
let numbers = [1, 2, 3, 4, 5]
let evens = List::keep(numbers, fn(n) { n % 2 == 0 })
}

Create a new empty list. Type parameters must be explicitly provided.

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

Concatenate two lists into a new list, with elements from a followed by elements from b.

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

Create a new list with elements starting from the given index. Elements before the index are dropped.

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

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

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

Filter a list, keeping only elements that match the given predicate function.

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

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

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

Transform each element in a list using the given function, returning a new list of the transformed elements.

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

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

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

Find the first element in a list that matches the given predicate. Returns a Maybe type.

use ard/list
let numbers = [1, 2, 3, 4, 5]
let first_even = List::find(numbers, fn(n) { n % 2 == 0 }) // some(2)

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

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

Split a list into two based on a predicate. Returns a Partition struct with selected and others fields.

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

Result of partitioning a list.

  • selected: [$T] - Elements matching the predicate
  • others: [$T] - Elements not matching the predicate