Loading...

Streamly.Data.MutArray.Generic

Unconstrained version of Streamly.Data.MutArray module.

See the Streamly.Data.MutArray module for documentation.

Setup

To execute the code examples provided in this module in ghci, please run the following commands first.

>>> :m
>>> import qualified Streamly.Data.Fold as Fold
>>> import qualified Streamly.Data.MutArray.Generic as MutArray
>>> import qualified Streamly.Data.Stream as Stream

For APIs that have not been released yet.

>>> import Streamly.Internal.Data.MutArray.Generic as MutArray

Type

Construction

emptyOf :: MonadIO m => Int -> m (MutArray a) Source #

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

Pre-release

fromListN :: MonadIO m => Int -> [a] -> m (MutArray a) Source #

fromList :: MonadIO m => [a] -> m (MutArray a) Source #

createOf :: MonadIO m => Int -> Fold m a (MutArray a) Source #

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

>>> createOf n = Fold.take n (MutArray.unsafeCreateOf n)

Pre-release

create :: MonadIO m => Fold m a (MutArray a) Source #

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.

Appending elements

snoc :: MonadIO m => MutArray a -> a -> m (MutArray a) Source #

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.

>>> snoc = MutArray.snocWith (* 2)

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

Pre-release

Inplace mutation

putIndex :: MonadIO m => Int -> MutArray a -> a -> m () Source #

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

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

Pre-release

unsafePutIndex :: forall m a. MonadIO m => Int -> MutArray a -> a -> m () Source #

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

modifyIndex :: MonadIO m => Int -> MutArray a -> (a -> (a, b)) -> m b Source #

Modify a given index of an array using a modifier function.

Pre-release

unsafeModifyIndex :: MonadIO m => Int -> MutArray a -> (a -> (a, b)) -> m b Source #

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

Random reads

getIndex :: MonadIO m => Int -> MutArray a -> m (Maybe a) Source #

O(1) Lookup the element at the given index. Index starts from 0.

unsafeGetIndex :: MonadIO m => Int -> MutArray a -> m a Source #

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

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

Conversion

toList :: MonadIO m => MutArray a -> m [a] Source #

Convert an Array into a list.

Pre-release

Streams

read :: MonadIO m => MutArray a -> Stream m a Source #

Generates a stream from the elements of a MutArray.

>>> read = Stream.unfold MutArray.reader

Unfolds

reader :: MonadIO m => Unfold m (MutArray a) a Source #

Unfold an array into a stream.

Stream of Arrays

chunksOf :: forall m a. MonadIO m => Int -> Stream m a -> Stream m (MutArray a) Source #

chunksOf n stream groups the input stream into a stream of arrays of size n.

chunksOf n = foldMany (MutArray.writeN n)

Pre-release

Size

Deprecated

new :: MonadIO m => Int -> m (MutArray a) Source #

Deprecated: Please use emptyOf instead.

writeN :: MonadIO m => Int -> Fold m a (MutArray a) Source #

Deprecated: Please use createOf instead.

write :: MonadIO m => Fold m a (MutArray a) Source #

Deprecated: Please use create instead.

modifyIndexUnsafe :: MonadIO m => Int -> MutArray a -> (a -> (a, b)) -> m b Source #

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

putIndexUnsafe :: forall m a. MonadIO m => Int -> MutArray a -> a -> m () Source #

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

getIndexUnsafe :: MonadIO m => Int -> MutArray a -> m a Source #

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.