Copyright | (c) 2020 Composewell Technologies |
---|---|
License | BSD3-3-Clause |
Maintainer | streamly@composewell.com |
Stability | experimental |
Portability | GHC |
Safe Haskell | Safe-Inferred |
Language | Haskell2010 |
Unconstrained version of Streamly.Data.MutArray module.
See the Streamly.Data.MutArray module for documentation.
Synopsis
- data MutArray a
- writeN :: MonadIO m => Int -> Fold m a (MutArray a)
- new :: MonadIO m => Int -> m (MutArray a)
- snoc :: MonadIO m => MutArray a -> a -> m (MutArray a)
- toList :: MonadIO m => MutArray a -> m [a]
- reader :: MonadIO m => Unfold m (MutArray a) a
- getIndex :: MonadIO m => Int -> MutArray a -> m a
- putIndex :: MonadIO m => Int -> MutArray a -> a -> m ()
- modifyIndex :: MonadIO m => Int -> MutArray a -> (a -> (a, b)) -> m b
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
Unfolds
Random reads
getIndex :: MonadIO m => Int -> MutArray a -> m a Source #
O(1) Lookup the element at the given index. Index starts from 0.