Array

/=

defn

(λ [(Ref (Array a)), (Ref (Array a))] Bool)

                    (/= a b)
                

compares two arrays and inverts the result.

=

defn

(λ [(Ref (Array a)), (Ref (Array a))] Bool)

                    (= a b)
                

compares two arrays.

all?

defn

(λ [(Ref (λ [&a] Bool)), (Ref (Array a))] Bool)

                    (all? f a)
                

checks whether all of the elements in a match the function f.

allocate

template

(λ [Int] (Array t))

allocates an uninitialized array. You can initialize members using aset-uninitialized.

any?

defn

(λ [(Ref (λ [&a] Bool)), (Ref (Array a))] Bool)

                    (any? f a)
                

checks whether any of the elements in a match the function f.

aset

template

(λ [(Array t), Int, t] (Array t))

sets an array element at the index n to a new value.

aset!

template

(λ [(Ref (Array t)), Int, t] ())

sets an array element at the index n to a new value in place.

aset-uninitialized!

template

(λ [(Ref (Array t)), Int, t] ())

sets an uninitialized array member. The old member will not be deleted.

aupdate

defn

(λ [(Array a), Int, (Ref (λ [&a] a))] (Array a))

                    (aupdate a i f)
                

transmutes (i.e. updates) the element at index i of an array a using the function f.

aupdate!

defn

(λ [(Ref (Array a)), Int, (Ref (λ [&a] a))] ())

                    (aupdate! a i f)
                

transmutes (i.e. updates) the element at index i of an array a using the function f in place.

concat

defn

(λ [(Ref (Array (Array a)))] (Array a))

                    (concat xs)
                

returns a new array which is the concatenation of the provided nested array xs.

copy

template

(λ [(Ref (Array a))] (Array a))

copies an array.

copy-filter

defn

(λ [(Ref (λ [&a] Bool)), (Ref (Array a))] (Array a))

                    (copy-filter f a)
                

filters the elements in an array.

It will create a copy. If you want to avoid that, consider using endo-filter instead.

copy-map

defn

(λ [(Ref (λ [&a] b)), (Ref (Array a))] (Array b))

                    (copy-map f a)
                

maps over an array a using the function f.

This function copies the array. If you don’t want that, use endo-map.

delete

template

(λ [(Array a)] ())

deletes an array. This function should usually not be called manually.

element-count

defn

(λ [(Ref (Array a)), &a] Int)

                    (element-count a e)
                

counts the occurrences of element e in an array.

empty?

defn

(λ [(Ref (Array a))] Bool)

                    (empty? a)
                

checks whether the array a is empty.

endo-filter

template

(λ [(Ref (λ [&a] Bool)), (Array a)] (Array a))

filters array members using a function. This function takes ownership.

endo-map

template

(λ [(Ref (λ [a] a)), (Array a)] (Array a))

applies a function f to an array a. The type of the elements cannot change.

enumerated

defn

(λ [(Ref (Array a))] (Array (Pair Int a)))

                    (enumerated xs)
                

creates a new array of Pairs where the first position is the index and the second position is the element from the original array xs.

find

defn

(λ [(Ref (λ [&a] Bool)), (Ref (Array a))] (Maybe a))

                    (find f a)
                

finds an element in a that matches the function f and wraps it in a Just.

If it doesn’t find an element, Nothing will be returned.

find-index

defn

(λ [(Ref (λ [&a] Bool)), (Ref (Array a))] (Maybe Int))

                    (find-index f a)
                

finds the index of the first element in a that matches the function f and wraps it in a Just.

If it doesn’t find an index, Nothing will be returned.

first

defn

(λ [(Ref (Array a))] (Maybe a))

                    (first a)
                

takes the first element of an array and returns a Just.

Returns Nothing if the array is empty.

index-of

defn

(λ [(Ref (Array a)), &a] (Maybe Int))

                    (index-of a e)
                

gets the index of element e in an array and wraps it on a Just.

If the element is not found, returns Nothing

last

defn

(λ [(Ref (Array a))] (Maybe a))

                    (last a)
                

takes the last element of an array and returns a Just.

Returns Nothing if the array is empty.

length

template

(λ [(Ref (Array t))] Int)

gets the length of the array.

maximum

defn

(λ [(Ref (Array a))] (Maybe a))

                    (maximum xs)
                

gets the maximum in an array (elements must support <) and wraps it in a Just.

If the array is empty, it returns Nothing.

minimum

defn

(λ [(Ref (Array a))] (Maybe a))

                    (minimum xs)
                

gets the minimum in an array (elements must support >) and wraps it in a Just.

If the array is empty, returns Nothing

nth

template

(λ [(Ref (Array t)), Int] &t)

gets a reference to the nth element from an array a.

pop-back

template

(λ [(Array a)] (Array a))

removes the last element of an array and returns the new array.

pop-back!

template

(λ [(Ref (Array a))] a)

removes an element value from the end of an array a in-place and returns it.

predicate-count

defn

(λ [(Ref (Array a)), (Ref (λ [&a] Bool))] Int)

                    (predicate-count a pred)
                

counts the number of elements satisfying the predicate function pred in an array.

prefix-array

defn

(λ [(Ref (Array a)), Int] (Array a))

                    (prefix-array xs end-index)
                

gets a prefix array to end-index.

prn

defn

(λ [(Ref (Array a))] String)

                    (prn x)
                

push-back

template

(λ [(Array a), a] (Array a))

adds an element value to the end of an array a.

push-back!

template

(λ [(Ref (Array a)), a] ())

adds an element value to the end of an array a in-place.

range

defn

(λ [a, a, a] (Array a))

                    (range start end step)
                

creates an array from start to end with step between them (the elements must support <, <=, >=, and to-int).

raw

template

(λ [(Array t)] (Ptr t))

returns an array a as a raw pointer—useful for interacting with C.

reduce

defn

(λ [(Ref (λ [a, &b] a)), a, (Ref (Array b))] a)

                    (reduce f x xs)
                

will reduce an array xs into a single value using a function f that takes the reduction thus far and the next value. The initial reduction value is x.

As an example, consider this definition of sum based on reduce:

(defn sum [x]
  (reduce + 0 x))

It will sum the previous sum with each new value, starting at 0.

remove

defn

(λ [&a, (Array a)] (Array a))

                    (remove el arr)
                

removes all occurrences of the element el in the array arr, in place.

remove-nth

defn

(λ [Int, (Array a)] (Array a))

                    (remove-nth i arr)
                

removes element at index idx from the array arr.

repeat

defn

(λ [Int, (Ref (λ [] a))] (Array a))

                    (repeat n f)
                

repeats the function f n times and stores the results in an array.

repeat-indexed

defn

(λ [Int, (λ [Int] a)] (Array a))

                    (repeat-indexed n f)
                

repeats function f ntimes and stores the results in an array.

This is similar to repeat, but the function f will be supplied with the index of the element.

replicate

defn

(λ [Int, &a] (Array a))

                    (replicate n e)
                

repeats element e n times and stores the results in an array.

reverse

defn

(λ [(Array a)] (Array a))

                    (reverse a)
                

reverses an array.

sort

defn

(λ [(Array a)] (Array a))

                    (sort arr)
                

Perform an in-place heapsort of a given owned array.

sort!

defn

(λ [(Ref (Array a))] ())

                    (sort! arr)
                

Perform an in-place heapsort of a given array.

sort-by

defn

(λ [(Array a), (Ref (λ [&a, &a] Bool))] (Array a))

                    (sort-by arr f)
                

Perform an in-place heapsort of a given owned array by a comparison function.

sort-by!

defn

(λ [(Ref (Array a)), (Ref (λ [&a, &a] Bool))] ())

                    (sort-by! arr f)
                

Perform an in-place heapsort of a given array by a comparison function.

sorted

defn

(λ [(Ref (Array a))] (Array a))

                    (sorted arr)
                

Perform a heapsort in a new copy of given array.

sorted-by

defn

(λ [(Ref (Array a)), (Ref (λ [&a, &a] Bool))] (Array a))

                    (sorted-by arr f)
                

Perform a heapsort in a new copy of given array by a comparison function.

str

template

(λ [(Ref (Array a))] String)

converts an array to a string.

subarray

defn

(λ [(Ref (Array a)), Int, Int] (Array a))

                    (subarray xs start-index end-index)
                

gets a subarray from start-index to end-index.

suffix-array

defn

(λ [(Ref (Array a)), Int] (Array a))

                    (suffix-array xs start-index)
                

gets a suffix array from start-index.

sum

defn

(λ [(Ref (Array a))] a)

                    (sum xs)
                

sums an array (elements must support + and zero).

sum-length

defn

(λ [(Ref (Array (Array a)))] Int)

                    (sum-length xs)
                

returns the sum of lengths from a nested array xs.

swap

defn

(λ [(Array a), Int, Int] (Array a))

                    (swap a i j)
                

swaps the indices i and j of an array a.

swap!

defn

(λ [(Ref (Array a)), Int, Int] ())

                    (swap! a i j)
                

swaps the indices i and j of an array a in place.

unsafe-first

defn

(λ [(Ref (Array a))] a)

                    (unsafe-first a)
                

takes the first element of an array.

Generates a runtime error if the array is empty.

unsafe-last

defn

(λ [(Ref (Array a))] a)

                    (unsafe-last a)
                

takes the last element of an array.

Generates a runtime error if the array is empty.

zero

defn

(λ [] (Array a))

                    (zero)
                

returns the empty array.

zip

defn

(λ [(Ref (λ [&a, &b] c)), (Ref (Array a)), (Ref (Array b))] (Array c))

                    (zip f a b)
                

maps over two arrays using a function f that takes two arguments. It will produces a new array with the length of the shorter input.

The trailing elements of the longer array will be discarded.