HORIZON HASKELLDocslts/ghc-10.0.x8fef28d2026-11-02Search packagesCtrl K
Theme: |

GHC 10.0.0.20260917 · lts/ghc-10.0.x · 8fef28d · 2026-11-02

Type

Constructing and Writing

Construction

Utils

Uninitialized Arrays

valueemptyOf :: MonadIO m => Int -> m (MutArray a)

emptyOf count allocates a zero length array that can be extended to hold up to count items without reallocating.

Pre-release

From streams

valueunsafeCreateOf :: MonadIO m => Int -> Fold m a (MutArray a)

Like createOf but does not check the array bounds when writing. The fold driver must not call the step function more than n times otherwise it will corrupt the memory and crash. This function exists mainly because any conditional in the step function blocks fusion causing 10x performance slowdown.

Pre-release

valuecreateOf :: MonadIO m => Int -> Fold m a (MutArray a)

createOf n folds a maximum of n elements from the input stream to an Array.

Example1 expression
createOf n = Fold.take n (MutArray.unsafeCreateOf n)

Pre-release

valuecreateWith :: MonadIO m => Int -> Fold m a (MutArray a)

createWith minCount folds the whole input to a single array. The array starts at a size big enough to hold minCount elements, the size is doubled every time the array needs to be grown.

Caution! Do not use this on infinite streams.

Pre-release

valuecreate :: MonadIO m => Fold m a (MutArray a)

Fold the whole input to a single array.

Same as createWith using an initial array size of arrayChunkSize bytes rounded up to the element size.

Caution! Do not use this on infinite streams.

valuefromStreamN :: MonadIO m => Int -> Stream m a -> m (MutArray a)

Create a MutArray from the first n elements of a stream. The array is allocated to size n, if the stream terminates before n elements then the array may hold less than n elements.

From containers

Random writes

valueputIndex :: MonadIO m => Int -> MutArray a -> a -> m ()

O(1) Write the given element at the given index in the array. Performs in-place mutation of the array.

Example1 expression
putIndex ix arr val = MutArray.modifyIndex ix arr (const (val, ()))

Pre-release

valueunsafePutIndex :: MonadIO m => Int -> MutArray a -> a -> m ()

Write the given element to the given index of the array. Does not check if the index is out of bounds of the array.

Pre-release

valueunsafeModifyIndex :: MonadIO m => Int -> MutArray a -> (a -> (a, b)) -> m b

Modify a given index of an array using a modifier function without checking the bounds.

Unsafe because it does not check the bounds of the array.

Pre-release

Growing and Shrinking

Reallocation

valuerealloc :: MonadIO m => Int -> MutArray a -> m (MutArray a)

Reallocates the array according to the new size. This is a safe function that always creates a new array and copies the old array into the new one. If the reallocated size is less than the original array it results in a truncated version of the original array.

valueuninit :: MonadIO m => MutArray a -> Int -> m (MutArray a)

Make the uninitialized memory in the array available for use extending it by the supplied length beyond the current length of the array. The array may be reallocated.

Appending elements

valuesnocWith :: MonadIO m => (Int -> Int) -> MutArray a -> a -> m (MutArray a)

snocWith sizer arr elem mutates arr to append elem. The length of the array increases by 1.

If there is no reserved space available in arr it is reallocated to a size in bytes determined by the sizer oldSize function, where oldSize is the original size of the array.

Note that the returned array may be a mutated version of the original array.

Pre-release

valuesnoc :: MonadIO m => MutArray a -> a -> m (MutArray a)

The array is mutated to append an additional element to it. If there is no reserved space available in the array then it is reallocated to double the original size.

This is useful to reduce allocations when appending unknown number of elements.

Note that the returned array may be a mutated version of the original array.

Example1 expression
snoc = MutArray.snocWith (* 2)

Performs O(n * log n) copies to grow, but is liberal with memory allocation.

Pre-release

valueunsafeSnoc :: MonadIO m => MutArray a -> a -> m (MutArray a)

Really really unsafe, appends the element into the first array, may cause silent data corruption or if you are lucky a segfault if the index is out of bounds.

Internal

Appending streams

Truncation

Eliminating and Reading

Unfolds

To containers

valueread :: MonadIO m => MutArray a -> Stream m a

Generates a stream from the elements of a MutArray.

Example1 expression
read = Stream.unfold MutArray.reader

Random reads

valueunsafeGetIndex :: MonadIO m => Int -> MutArray a -> m a

Return the element at the specified index without checking the bounds.

Unsafe because it does not check the bounds of the array.

Size

In-place Mutation Algorithms

Folding

valuecmp :: (MonadIO m, Ord a) => MutArray a -> MutArray a -> m Ordering

Compare the length of the arrays. If the length is equal, compare the lexicographical ordering of two underlying byte arrays otherwise return the result of length comparison.

Pre-release

Arrays of arrays

Operations dealing with multiple arrays, streams of arrays or multidimensional array representations.

Construct from streams

Eliminate to streams

Construct from arrays

valuesliceOffLen
  1. :: Int

    from index

  2. -> Int

    length of the slice

  3. -> MutArray a
  4. -> MutArray a

O(1) Slice an array in constant time. Throws an error if the slice extends out of the array bounds.

Pre-release

Appending arrays

Deprecated

valuegetSlice
  1. :: Int

    from index

  2. -> Int

    length of the slice

  3. -> MutArray a
  4. -> MutArray a

Deprecated. Please use sliceOffLen instead.

O(1) Slice an array in constant time. Throws an error if the slice extends out of the array bounds.

Pre-release

valuegetIndexUnsafe :: MonadIO m => Int -> MutArray a -> m a

Deprecated. Please use unsafeGetIndex instead.

Return the element at the specified index without checking the bounds.

Unsafe because it does not check the bounds of the array.

Deprecated. Please use unsafeGetIndexWith instead.

Return the element at the specified index without checking the bounds from a MutableArray# RealWorld.

Unsafe because it does not check the bounds of the array.

valueputIndexUnsafe :: MonadIO m => Int -> MutArray a -> a -> m ()

Deprecated. Please use unsafePutIndex instead.

Write the given element to the given index of the array. Does not check if the index is out of bounds of the array.

Pre-release

valuemodifyIndexUnsafe :: MonadIO m => Int -> MutArray a -> (a -> (a, b)) -> m b

Deprecated. Please use unsafeModifyIndex instead.

Modify a given index of an array using a modifier function without checking the bounds.

Unsafe because it does not check the bounds of the array.

Pre-release

valuesnocUnsafe :: MonadIO m => MutArray a -> a -> m (MutArray a)

Deprecated. Please use unsafeSnoc instead.

Really really unsafe, appends the element into the first array, may cause silent data corruption or if you are lucky a segfault if the index is out of bounds.

Internal

valuegetSliceUnsafe
  1. :: Int

    from index

  2. -> Int

    length of the slice

  3. -> MutArray a
  4. -> MutArray a

Deprecated. Please use unsafeSliceOffLen instead.

O(1) Slice an array in constant time.

Unsafe: The bounds of the slice are not checked.

Unsafe

Pre-release