Array
is the indexable collection data structure.
Its literal uses brackets, so an array of integers would look like this:
[1 2 3]
. It provides both a functional and an imperative API, and is one of
the core data structures of Carp. It is heap-allocated, for a stack-allocated
variant you might want to check out StaticArray
.
all?
(Fn [(Ref (Fn [(Ref a b)] Bool c) d), (Ref (Array a) b)] Bool)
(all? f a)
checks whether all of the elements in a
match the function f
.
allocate
(Fn [Int] (Array a))
allocates an uninitialized array. You can initialize members using aset-uninitialized
.
any?
(Fn [(Ref (Fn [(Ref a b)] Bool c) d), (Ref (Array a) b)] Bool)
(any? f a)
checks whether any of the elements in a
match the function f
.
aset
(Fn [(Array a), Int, a] (Array a))
sets an array element at the index n
to a new value.
aset!
(Fn [(Ref (Array a) b), Int, a] ())
sets an array element at the index n
to a new value in place.
aset-uninitialized!
(Fn [(Ref (Array a) b), Int, a] ())
sets an uninitialized array member. The old member will not be deleted.
aupdate
(Fn [(Array a), Int, (Ref (Fn [a] a b) c)] (Array a))
(aupdate a i f)
transmutes (i.e. updates) the element at index i
of an array a
using the function f
.
aupdate!
(Fn [(Ref (Array a) b), Int, (Ref (Fn [a] a c) d)] ())
(aupdate! a i f)
transmutes (i.e. updates) the element at index i
of an array a
using the function f
in place.
concat
(Fn [(Ref (Array (Array a)) b)] (Array a))
(concat xs)
returns a new array which is the concatenation of the provided nested array xs
.
contains?
(Fn [(Ref (Array a) b), (Ref a b)] Bool)
(contains? arr el)
checks wether an element exists in the array.
copy-filter
(Fn [(Ref (Fn [(Ref a b)] Bool c) d), (Ref (Array a) e)] (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
(Fn [(Ref (Fn [(Ref a b)] c d) e), (Ref (Array a) b)] (Array c))
(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
(Fn [(Array a)] ())
deletes an array. This function should usually not be called manually.
element-count
(Fn [(Ref (Array a) b), (Ref a b)] Int)
(element-count a e)
counts the occurrences of element e
in an array.
endo-filter
(Fn [(Ref (Fn [(Ref a b)] Bool c) d), (Array a)] (Array a))
filters array members using a function. This function takes ownership.
endo-map
(Fn [(Ref (Fn [a] a b) c), (Array a)] (Array a))
applies a function f
to an array a
. The type of the elements cannot change.
endo-scan
(Fn [(Ref (Fn [(Ref a b), (Ref a c)] a d) e), (Array a)] (Array a))
(endo-scan f xs)
Like Array.scan
, but uses the first element of the array as the starting value.
Also does not create a new array, but reuses the initial one instead (by taking ownership over xs
.)
For example, an endo-scan using Int.+
over the array [1 1 1 1 1] will return [1 2 3 4 5]
enumerated
(Fn [(Ref (Array a) b)] (Array (Pair Int a)))
(enumerated xs)
creates a new array of Pair
s where the first position is the index and the second position is the element from the original array xs
.
find
(Fn [(Ref (Fn [(Ref a b)] Bool c) d), (Ref (Array a) b)] (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
(Fn [(Ref (Fn [(Ref a b)] Bool c) d), (Ref (Array a) b)] (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
(Fn [(Ref (Array a) b)] (Maybe a))
(first a)
takes the first element of an array and returns a Just
.
Returns Nothing
if the array is empty.
from-static
(Fn [(Ref (StaticArray a) b)] (Array a))
(from-static sarr)
Turns a StaticArray
into an Array
. Copies elements.
index-of
(Fn [(Ref (Array a) b), (Ref a b)] (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
(Fn [(Ref (Array a) b)] (Maybe a))
(last a)
takes the last element of an array and returns a Just
.
Returns Nothing
if the array is empty.
map-reduce
(Fn [(Ref (Fn [(Ref a b), (Ref c d)] (Pair a e) f) g), a, (Ref (Array c) d)] (Pair a (Array e)))
(map-reduce f acc a)
reduces an array a
by invoking the function f
on each
element, while keeping an accumulator and a list.
Returns a Pair
where the first element is the mapped array and the second one
is the final accumulator.
The function f
receives two arguments: the first one is the accumulator, and
the second one is the element. f
must return (Pair accumulator result)
.
Example:
(map-reduce &(fn [acc x] (Pair.init (+ @x @acc) (* @x 2))) 0 &[1 2 3])
; => (Pair 6 [2 4 6])
maximum
(Fn [(Ref (Array a) b)] (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
(Fn [(Ref (Array a) b)] (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
(Fn [(Ref (Array a) b), Int] (Maybe a))
(nth xs index)
gets a reference to the n
th element from an array arr
wrapped on a Maybe
.
If the index
is out of bounds, return Maybe.Nothing
partition
(Fn [(Ref (Array a) b), Int] (Array (Array a)))
(partition arr n)
Partitions an array arr
into an array of arrays of length n
sequentially filled with the arr
's original values.
This function will fill partitions until `arr` is exhuasted.
If `n` is greater than or equal to the length of `arr`, the result of this
function is an array containing a single array of length `n`.
For example:
```clojure
(Array.partition &[1 2 3 4] 2)
=> [[1 2] [3 4]]
(Array.partition &[1 2 3 4] 3)
=> [[1 2 3] [4]]
(Array.partition &[1 2 3 4] 6)
=> [[1 2 3 4]]
```
pop-back
(Fn [(Array a)] (Array a))
removes the last element of an array and returns the new array.
pop-back!
(Fn [(Ref (Array a) b)] a)
removes an element value
from the end of an array a
in-place and returns it.
predicate-count
(Fn [(Ref (Array a) b), (Ref (Fn [(Ref a b)] Bool c) d)] Int)
(predicate-count a pred)
counts the number of elements satisfying the predicate function pred
in an array.
push-back!
(Fn [(Ref (Array a) b), a] ())
adds an element value
to the end of an array a
in-place.
range
(Fn [a, a, a] (Result (Array a) String))
(range start end step)
creates an array from start
to end
with step
between them
(the elements must support <
, <=
, >=
, and to-int
).
It returns a Result.Success
if the input was right, and a Result.Error
if
the input given was wrong, containing an error message.
range-or-default
(Fn [a, a, a] (Array a))
(range-or-default start end step)
is a version of range
that returns an empty
array on failure.
raw
(Fn [(Array a)] (Ptr a))
returns an array a
as a raw pointer—useful for interacting with C.
reduce
(Fn [(Ref (Fn [a, (Ref b c)] a d) e), a, (Ref (Array b) c)] 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 &(fn [x y] (+ x @y)) 0 x))
It will sum the previous sum with each new value, starting at 0
.
remove
(Fn [(Ref a b), (Array a)] (Array a))
(remove el arr)
removes all occurrences of the element el
in the array arr
, in place.
remove-nth
(Fn [Int, (Array a)] (Array a))
(remove-nth i arr)
removes element at index idx
from the array arr
.
repeat
(Fn [Int, (Ref (Fn [] a b) c)] (Array a))
(repeat n f)
repeats the function f
n
times and stores the results in an array.
repeat-indexed
(Fn [Int, (Fn [Int] a b)] (Array a))
(repeat-indexed n f)
repeats function f
n
times 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
(Fn [Int, (Ref a b)] (Array a))
(replicate n e)
repeats element e
n
times and stores the results in an array.
rest
(Fn [(Ref (Array a) b)] (Array a))
(rest xs)
gets all but the first element from the array.
scan
(Fn [(Ref (Fn [(Ref a b), (Ref c d)] a e) f), a, (Ref (Array c) d)] (Array a))
(scan f x xs)
Similar to Array.reduce
, but instead returns an array with the starting element,
and then all intermediate values.
For example, a scan using Int.+
over the array [1 1 1 1 1] (starting at 0) will return [0 1 2 3 4 5].
slice
(Fn [(Ref (Array a) b), Int, Int] (Array a))
(slice xs start-index end-index)
gets a subarray from start-index
to end-index
.
sort
(Fn [(Array a)] (Array a))
(sort arr)
Perform an in-place heapsort of a given owned array.
sort-by
(Fn [(Array a), (Ref (Fn [(Ref a b), (Ref a b)] Bool c) d)] (Array a))
(sort-by arr f)
Perform an in-place heapsort of a given owned array by a comparison function.
sort-by!
(Fn [(Ref (Array a) b), (Ref (Fn [(Ref a b), (Ref a b)] Bool c) d)] ())
(sort-by! arr f)
Perform an in-place heapsort of a given array by a comparison function.
sorted
(Fn [(Ref (Array a) b)] (Array a))
(sorted arr)
Perform a heapsort in a new copy of given array.
sorted-by
(Fn [(Ref (Array a) b), (Ref (Fn [(Ref a c), (Ref a c)] Bool d) e)] (Array a))
(sorted-by arr f)
Perform a heapsort in a new copy of given array by a comparison function.
suffix
(Fn [(Ref (Array a) b), Int] (Array a))
(suffix xs start-index)
gets a suffix array from start-index
.
sum-length
(Fn [(Ref (Array (Array a)) b)] Int)
(sum-length xs)
returns the sum of lengths from a nested array xs
.
swap
(Fn [(Array a), Int, Int] (Array a))
(swap a i j)
swaps the indices i
and j
of an array a
.
swap!
(Fn [(Ref (Array a) b), Int, Int] ())
(swap! a i j)
swaps the indices i
and j
of an array a
in place.
unreduce
(Fn [a, (Ref (Fn [a] Bool b) c), (Ref (Fn [a] a b) d)] (Array a))
(unreduce start test step)
creates an array by producing values using step
until they
no longer satisfy test
. The initial value is start
.
Example:
; if we didn’t have Array.range, we could define it like this:
(defn range [start end step]
(unreduce start &(fn [x] (< x (+ step end))) &(fn [x] (+ x step)))
)
unsafe-first
(Fn [(Ref (Array a) b)] (Ref a b))
(unsafe-first a)
takes the first element of an array.
Generates a runtime error if the array is empty.
unsafe-last
(Fn [(Ref (Array a) b)] (Ref a b))
(unsafe-last a)
takes the last element of an array.
Generates a runtime error if the array is empty.
unsafe-nth
(Fn [(Ref (Array a) b), Int] (Ref a b))
gets a reference to the n
th element from an array a
.
unsafe-nth-value
(Fn [(Ref (Array a) b), Int] a)
returns the value at index i
of an array a
(just like unsafe-nth) but does not take its reference, and does not copy the value. Should only be used for optimizations and when you know what you're doing, circumvents the borrow checker!
unsafe-raw
(Fn [(Ref (Array a) b)] (Ptr a))
returns an array a
as a raw pointer—useful for interacting with C.
zip
(Fn [(Ref (Fn [(Ref a b), (Ref c d)] e f) g), (Ref (Array a) b), (Ref (Array c) d)] (Array e))
(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.