fp/list

Values

pub fn all(xs: List(a), f: fn(a) -> Bool) -> Bool

Returns True if all elements in the list satisfy the predicate. Returns True for empty lists (vacuous truth).

Examples

all([1, 2, 3, 4], fn(x) { x > 0 }) // True
all([1, 2, -3, 4], fn(x) { x > 0 }) // False
all([], fn(x) { x > 0 }) // True
pub fn any(xs: List(a), f: fn(a) -> Bool) -> Bool

Returns True if any element in the list satisfies the predicate. Returns False for empty lists.

Examples

any([1, 2, 3, 4], fn(x) { x == 3 }) // True
any([1, 2, 4], fn(x) { x == 3 }) // False
any([], fn(x) { x == 3 }) // False
pub fn chunk(xs: List(a), size: Int) -> List(List(a))

Splits a list into chunks of the specified size. Returns empty list for invalid sizes (≤ 0) to maintain ergonomics. The last chunk may be smaller than the specified size.

Examples

chunk([1, 2, 3, 4, 5], 2) // [[1, 2], [3, 4], [5]]
chunk([1, 2, 3], 1) // [[1], [2], [3]]
chunk([1, 2, 3], 5) // [[1, 2, 3]]
chunk([], 2) // []
chunk([1, 2, 3], 0) // [] (gracefully handles invalid size)
pub fn filter(xs: List(a), f: fn(a) -> Bool) -> List(a)

Keeps only elements that satisfy the predicate.

Examples

filter([1, 2, 3, 4, 5], fn(x) { x % 2 == 0 }) // [2, 4]
filter([], fn(x) { x % 2 == 0 }) // []
filter(["apple", "banana", "cherry"], fn(s) { string.length(s) > 5 })
// ["banana", "cherry"]
pub fn flat_map(xs: List(a), f: fn(a) -> List(b)) -> List(b)

Maps a function over a list and flattens the result. Also known as bind or >>= in other functional languages.

Examples

flat_map([1, 2, 3], fn(x) { [x, x * 2] })
// Result: [1, 2, 2, 4, 3, 6]

flat_map([], fn(x) { [x, x * 2] })
// Result: []
pub fn uniq(xs: List(a)) -> List(a)

Removes duplicate elements from a list, keeping the first occurrence.

Examples

uniq([1, 2, 2, 3, 1, 4]) // [1, 2, 3, 4]
uniq([]) // []
uniq(["a", "b", "a"]) // ["a", "b"]
Search Document