Copyright | (c) Alexey Kuleshevich 2018 |
---|---|

License | BSD3 |

Maintainer | Alexey Kuleshevich <lehins@yandex.ru> |

Stability | experimental |

Portability | non-portable |

Safe Haskell | None |

Language | Haskell2010 |

- class Manifest r ix e => Mutable r ix e where
- msize :: Mutable r ix e => MArray s r ix e -> ix
- new :: (Mutable r ix e, PrimMonad m) => ix -> m (MArray (PrimState m) r ix e)
- thaw :: (Mutable r ix e, PrimMonad m) => Array r ix e -> m (MArray (PrimState m) r ix e)
- freeze :: (Mutable r ix e, PrimMonad m) => Comp -> MArray (PrimState m) r ix e -> m (Array r ix e)
- read :: (Mutable r ix e, PrimMonad m) => MArray (PrimState m) r ix e -> ix -> m (Maybe e)
- read' :: (Mutable r ix e, PrimMonad m) => MArray (PrimState m) r ix e -> ix -> m e
- write :: (Mutable r ix e, PrimMonad m) => MArray (PrimState m) r ix e -> ix -> e -> m Bool
- write' :: (Mutable r ix e, PrimMonad m) => MArray (PrimState m) r ix e -> ix -> e -> m ()
- modify :: (Mutable r ix e, PrimMonad m) => MArray (PrimState m) r ix e -> (e -> e) -> ix -> m Bool
- modify' :: (Mutable r ix e, PrimMonad m) => MArray (PrimState m) r ix e -> (e -> e) -> ix -> m ()
- swap :: (Mutable r ix e, PrimMonad m) => MArray (PrimState m) r ix e -> ix -> ix -> m Bool
- swap' :: (Mutable r ix e, PrimMonad m) => MArray (PrimState m) r ix e -> ix -> ix -> m ()
- generateM :: (Monad m, Mutable r ix e) => Comp -> ix -> (ix -> m e) -> m (Array r ix e)
- generateLinearM :: (Monad m, Mutable r ix e) => Comp -> ix -> (Int -> m e) -> m (Array r ix e)
- mapM :: (Monad m, Source r ix e, Mutable r' ix e') => r' -> (e -> m e') -> Array r ix e -> m (Array r' ix e')
- imapM :: (Monad m, Source r ix e, Mutable r' ix e') => r' -> (ix -> e -> m e') -> Array r ix e -> m (Array r' ix e')
- forM :: (Monad m, Source r ix e, Mutable r' ix e') => r' -> Array r ix e -> (e -> m e') -> m (Array r' ix e')
- iforM :: (Monad m, Source r ix e, Mutable r' ix e') => r' -> Array r ix e -> (ix -> e -> m e') -> m (Array r' ix e')
- sequenceM :: (Monad m, Source r ix (m e), Mutable r' ix e) => r' -> Array r ix (m e) -> m (Array r' ix e)

# Documentation

new :: (Mutable r ix e, PrimMonad m) => ix -> m (MArray (PrimState m) r ix e) Source #

Initialize a new mutable array. Negative size will result in an empty array.

thaw :: (Mutable r ix e, PrimMonad m) => Array r ix e -> m (MArray (PrimState m) r ix e) Source #

*O(n)* - Yield a mutable copy of the immutable array

freeze :: (Mutable r ix e, PrimMonad m) => Comp -> MArray (PrimState m) r ix e -> m (Array r ix e) Source #

*O(n)* - Yield an immutable copy of the mutable array

read :: (Mutable r ix e, PrimMonad m) => MArray (PrimState m) r ix e -> ix -> m (Maybe e) Source #

*O(1)* - Lookup an element in the mutable array. Return `Nothing`

when index is out of bounds.

read' :: (Mutable r ix e, PrimMonad m) => MArray (PrimState m) r ix e -> ix -> m e Source #

*O(1)* - Same as `read`

, but throws an error if index is out of bounds.

write :: (Mutable r ix e, PrimMonad m) => MArray (PrimState m) r ix e -> ix -> e -> m Bool Source #

*O(1)* - Write an element into the cell of a mutable array. Returns `False`

when index is out
of bounds.

write' :: (Mutable r ix e, PrimMonad m) => MArray (PrimState m) r ix e -> ix -> e -> m () Source #

*O(1)* - Same as `write`

, but throws an error if index is out of bounds.

modify :: (Mutable r ix e, PrimMonad m) => MArray (PrimState m) r ix e -> (e -> e) -> ix -> m Bool Source #

*O(1)* - Modify an element in the cell of a mutable array with a supplied function. Returns
`False`

when index is out of bounds.

modify' :: (Mutable r ix e, PrimMonad m) => MArray (PrimState m) r ix e -> (e -> e) -> ix -> m () Source #

*O(1)* - Same as `modify`

, but throws an error if index is out of bounds.

swap :: (Mutable r ix e, PrimMonad m) => MArray (PrimState m) r ix e -> ix -> ix -> m Bool Source #

*O(1)* - Swap two elements in a mutable array by supplying their indices. Returns `False`

when
either one of the indices is out of bounds.

swap' :: (Mutable r ix e, PrimMonad m) => MArray (PrimState m) r ix e -> ix -> ix -> m () Source #

*O(1)* - Same as `swap`

, but throws an error if index is out of bounds.

# Generate (experimental)

Functions in this sections can monadically generate manifest arrays using their associated mutable interface. Due to the sequential nature of monads generation is done also sequentially regardless of supplied computation strategy. All of functions here are very much experimental, so please report an issue if you see something not working properly.

Here is a very imperative like for loop that creates an array while performing a side effect for each newly created element:

printSquare :: Int -> IO (Array P Ix1 Int) printSquare n = forM P (range Seq 0 n) $ i -> do let e = i*i putStrLn $ "Element at index: " ++ show i ++ " = " ++ show e ++ ";" return e

`>>>`

Element at index: 0 = 0; Element at index: 1 = 1; Element at index: 2 = 4; Element at index: 3 = 9; Element at index: 4 = 16; (Array P Seq (5) [ 0,1,4,9,16 ])`printSquare 5`

generateM :: (Monad m, Mutable r ix e) => Comp -> ix -> (ix -> m e) -> m (Array r ix e) Source #

*O(n)* - Generate an array monadically using it's mutable interface. Computation will be done
sequentially, regardless of `Comp`

argument.

*Since: 0.1.1*

generateLinearM :: (Monad m, Mutable r ix e) => Comp -> ix -> (Int -> m e) -> m (Array r ix e) Source #

*O(n)* - Same as `generateM`

but using a flat index.

*Since: 0.1.1*

mapM :: (Monad m, Source r ix e, Mutable r' ix e') => r' -> (e -> m e') -> Array r ix e -> m (Array r' ix e') Source #

*O(n)* - Map a monadic action over an Array. This operation will force computation sequentially
and will result in a manifest Array.

#### Examples

`>>>`

Just (Array P Seq (5) [ 0,1,4,9,16 ])`mapM P (\i -> Just (i*i)) $ range Seq 0 5`

*Since: 0.1.1*

imapM :: (Monad m, Source r ix e, Mutable r' ix e') => r' -> (ix -> e -> m e') -> Array r ix e -> m (Array r' ix e') Source #

*O(n)* - Map an index aware monadic action over an Array. This operation will force computation
sequentially and will result in a manifest Array.

*Since: 0.1.1*

forM :: (Monad m, Source r ix e, Mutable r' ix e') => r' -> Array r ix e -> (e -> m e') -> m (Array r' ix e') Source #

*O(n)* - Same as `mapM`

, but with its arguments flipped.

*Since: 0.1.1*