Data.Array

lib/Data/Array.gob

No module comment.

type Array a

A fixed-length array: its length is set when it is made and never changes, and its elements can be assigned. Its one field cannot be assigned, so at run time an Array is the primitive array itself, with its length in the header. That header is the only length there is, so the test get and set make for the program's error message is also the one that keeps the access inside the storage, and in a loop bounded by len(xs) the compiler proves both away.

An array literal the program uses as an Array is built as this constructor applied to the primitive array, by name; renaming it means changing how the compiler lowers a literal in the same commit.

fun ofStorage : fun(Prim.Array a) -> Array a

An Array of storage the caller made and hands over, for the rest of the library.

fun storage : fun(Array a) -> Prim.Array a

The storage an Array is, for the rest of the library to hand to a bulk primitive: ofStorage the other way.

fun view : fun(Array Byte) -> ByteView

Returns a read-only view of xs, without copying it. The view cannot write to xs, but whoever holds xs still can, and the view shows each write as it happens.

fun filled : fun(Int, a) -> Array a
fun init : fun(Int, fun(Int) -> a) -> Array a

Element i is f(i), computed in order from 0.

fun map : [Iterator c, Length c, Item c ~ a] fun(c, fun(a) -> b) -> Array b

f of each element of xs, in order. xs is anything with a length that can be iterated, so a Vec maps straight to an Array.

The loop counts to the length rather than running to the end of the iteration, so every slot of the result is written: an iterator that ends early -- a Vec that f pops from -- panics instead of leaving a hole.

fun filter : fun(Array a, fun(a) -> Bool) -> Array a

How many elements are kept is known only at the end, and keep is called once per element, in order. So the kept ones go into scratch storage as long as xs, which is copied once into an array of their number unless they are all of it.

fun fold : fun(Array a, b, fun(b, a) -> b) -> b

Returns the result of calling step on the accumulator and each element in turn. Iter.fold takes the same arguments and works on anything iterable.

fun reverse : fun(Array a) -> Array a
fun append : fun(Array a, Array a) -> Array a
fun copy : fun(Array a, Int, Array a, Int, Int) -> Unit

Copy count elements of source, from index from on, over those of target from index at on. The two may be one array, and the runs may overlap: every element arrives as it was before the copy began. Panics unless both runs are inside their arrays.

fun fill : fun(Array a, Int, Int, a) -> Unit

Set the count elements of target from index at on to value. Panics unless they are all elements of target.

fun at : fun(Array a, Int) -> Option a
fun last : fun(Array a) -> Option a
fun swap : fun(Array a, Int, Int) -> Unit
fun slice : fun(Array a, Int, Int) -> Array a

A copy, not a view: writing to one leaves the other as it was.

fun indexOf : [Eq a] fun(Array a, a) -> Option Int
fun contains : [Eq a] fun(Array a, a) -> Bool
fun sortBy : fun(Array a, fun(a, a) -> Ordering) -> Array a

A stable sort, answering a new Array; Turkey.Array says how.

fun sort : [Ord a] fun(Array a) -> Array a
fun sortByKey : [Ord k] fun(Array a, fun(a) -> k) -> Array a

Sorted by key, stably, calling key once per element. Each element is paired with its key and the pairs sorted, which costs two arrays of pairs where comparing key(a) with key(b) would cost none, and is worth it: that would call key twice per comparison, and a key is often a lookup or a system call.

fun threeWay : [Ord a] fun(a, a) -> Ordering

Ord answers in Bool, so a three-way comparison costs two questions. Ordering is the shape a sort wants, and this is the adapter.

instance Index (Array a)
instance SetIndex (Array a)
instance Length (Array a)
instance Iterator (Array a)
instance Add (Array a)
instance Semigroup (Array a)
instance Monoid (Array a)
instance Foldable Array
instance Functor Array
instance Applicative Array
instance Monad Array

Every f(x) is made first, so the result is allocated once, at their total length.

instance Eq (Array a) : Eq a

Equal when they have the same length and equal elements at each index.

instance Ord (Array a) : Ord a

Compares element by element from the front: the first index where the two differ decides, and an array that is a proper prefix of the other is less.

instance Debug (Array a) : Debug a

Its elements, as the array literal that builds it.

instance Show (Array a) : Debug a

What its Debug prints: a container's text shows its elements' Debug, so that a string in it is quoted.