Module

Data.Array.ST

Package
purescript-arrays
Repository
purescript/purescript-arrays

Helper functions for working with mutable arrays using the ST effect.

This module can be used when performance is important and mutation is a local effect.

#STArray Source

data STArray :: Region -> Type -> Type

A reference to a mutable array.

The first type parameter represents the memory region which the array belongs to. The second type parameter defines the type of elements of the mutable array.

The runtime representation of a value of type STArray h a is the same as that of Array a, except that mutation is allowed.

#Assoc Source

type Assoc a = { index :: Int, value :: a }

An element and its index.

#withArray Source

withArray :: forall b a h. (STArray h a -> ST h b) -> Array a -> ST h (Array a)

Perform an effect requiring a mutable array on a copy of an immutable array, safely returning the result as an immutable array.

#empty Source

empty :: forall a h. ST h (STArray h a)

Create an empty mutable array.

#peek Source

peek :: forall a h. Int -> STArray h a -> ST h (Maybe a)

Read the value at the specified index in a mutable array.

#poke Source

poke :: forall a h. Int -> a -> STArray h a -> ST h Boolean

Change the value at the specified index in a mutable array.

#push Source

push :: forall a h. a -> STArray h a -> ST h Int

Append an element to the end of a mutable array. Returns the new length of the array.

#modify Source

modify :: forall a h. Int -> (a -> a) -> STArray h a -> ST h Boolean

Mutate the element at the specified index using the supplied function.

#pushAll Source

pushAll :: forall a h. Array a -> STArray h a -> ST h Int

Append the values in an immutable array to the end of a mutable array. Returns the new length of the mutable array.

#splice Source

splice :: forall a h. Int -> Int -> Array a -> STArray h a -> ST h (Array a)

Remove and/or insert elements from/into a mutable array at the specified index.

#sort Source

sort :: forall h a. Ord a => STArray h a -> ST h (STArray h a)

Sort a mutable array in place.

#sortBy Source

sortBy :: forall h a. (a -> a -> Ordering) -> STArray h a -> ST h (STArray h a)

Sort a mutable array in place using a comparison function.

#sortWith Source

sortWith :: forall h b a. Ord b => (a -> b) -> STArray h a -> ST h (STArray h a)

Sort a mutable array in place based on a projection.

#freeze Source

freeze :: forall a h. STArray h a -> ST h (Array a)

Create an immutable copy of a mutable array.

#thaw Source

thaw :: forall a h. Array a -> ST h (STArray h a)

Create a mutable copy of an immutable array.

#unsafeFreeze Source

unsafeFreeze :: forall a h. STArray h a -> ST h (Array a)

O(1). Convert a mutable array to an immutable array, without copying. The mutable array must not be mutated afterwards.

#unsafeThaw Source

unsafeThaw :: forall a h. Array a -> ST h (STArray h a)

O(1) Convert an immutable array to a mutable array, without copying. The input array must not be used afterward.

#toAssocArray Source

toAssocArray :: forall a h. STArray h a -> ST h (Array (Assoc a))

Create an immutable copy of a mutable array, where each element is labelled with its index in the original array.