streamly-core-0.1.0: Streaming, parsers, arrays and more
Copyright(c) 2020 Composewell Technologies
LicenseBSD3-3-Clause
Maintainerstreamly@composewell.com
Stabilityexperimental
PortabilityGHC
Safe HaskellSafe-Inferred
LanguageHaskell2010

Streamly.Data.MutArray.Generic

Description

Unconstrained version of Streamly.Data.MutArray module.

See the Streamly.Data.MutArray module for documentation.

Synopsis

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.Array.Generic.Mut.Type as MutArray

Type

Construction

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

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

>>> writeN n = Fold.take n (MutArray.writeNUnsafe n)

Pre-release

Appending elements

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

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

Pre-release

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

Conversion

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

Convert an Array into a list.

Pre-release

Unfolds

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

Unfold an array into a stream.

Random reads

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

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

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

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