Collection — Collection Operations

Part of the std module. Available via import std.

Collection operations for arrays, sets, and optionals.

Interface

for

interface for: f a. (a. b). f b

Map a function over a collection. Built-in support exists for arrays, sets, and optionals without importing std.

for [1, 2, 3] (x. x + 1)     | [2, 3, 4]
for names (name. "Hi, " ++ name)

for is map with the arguments flipped: the collection comes first, then the function.

Functions

reduce

reduce: xs:[a]. f:(a. b. b). b:b. b

Fold over a structure, accumulating a result. Built-in support for arrays, sets, and optionals.

reduce [1, 2, 3] (x. acc. x + acc) 0

filter

filter: xs:[a]. f:(a. Boolean). [a]

Remove elements that don’t satisfy a predicate.

map

map: f:(a. b). xs:[a]. [b]

for with arguments flipped — function first, then collection.

iterate

iterate: xs:[a]. f:(a. [b]). [b]

Monadic bind for arrays. Applies a function that returns an array to each element and flattens the results.

take

take: n:Natural. xs:[a]. [a]

Take the first n elements from an array.

drop

drop: n:Natural. xs:[a]. [a]

Drop the first n elements from an array.

any

any: xs:[a]. f:(a. Boolean). Boolean

Returns true if any element satisfies the predicate.

all

all: xs:[a]. f:(a. Boolean). Boolean

Returns true if all elements satisfy the predicate.

elem

elem: xs:[a]. a:a. Boolean

Returns true if the element is in the array.

notelem

notelem: xs:[a]. a:a. Boolean

Returns true if the element is not in the array.

zip

zip: xs:[a]. ys:[b]. [Tuple a b]

Combine two arrays into an array of pairs. Uses the Tuple type from algebra.dox (also in std).

zipWith

zipWith: f:(a. b. c). ass:[a]. bs:[b]. [c]

Combine two arrays using a function.

cons

cons: x:a. xs:[a]. [a]

Add an element to the front of an array.

snoc

snoc: xs:[a]. Tuple a [a]?

Separate the first element from the rest. Returns an optional tuple — () if the array is empty.

head

head: xs:[a]. a?

Get the first element, or () if empty.

tail

tail: xs:[a]. [a]

Get the array with the first element removed.