streamly-0.10.1: Streaming, dataflow programming and declarative concurrency
Copyright(c) 2019 Composewell Technologies
LicenseBSD3
Maintainerstreamly@composewell.com
Stabilityreleased
PortabilityGHC
Safe HaskellSafe-Inferred
LanguageHaskell2010

Streamly.Data.Array.Foreign

Description

Deprecated: Please use Streamly.Data.Array module from the streamly-core package.

This module provides immutable arrays in pinned memory (non GC memory) suitable for long lived data storage, random access and for interfacing with the operating system.

Arrays in this module are chunks of pinned memory that hold a sequence of Storable values of a given type, they cannot store non-serializable data like functions. Once created an array cannot be modified. Pinned memory allows efficient buffering of long lived data without adding any impact to GC. One array is just one pointer visible to GC and it does not have to be copied across generations. Moreover, pinned memory allows communication with foreign consumers and producers (e.g. file or network IO) without copying the data.

Programmer Notes

Array creation APIs require a MonadIO Monad, except fromList which is a pure API. To operate on streams in pure Monads like Identity you can hoist it to IO monad as follows:

>>> import Data.Functor.Identity (Identity, runIdentity)
>>> s = Stream.fromList [1..10] :: SerialT Identity Int
>>> s1 = Stream.hoist (return . runIdentity) s :: SerialT IO Int
>>> Stream.fold Array.write s1 :: IO (Array Int)
fromList [1,2,3,4,5,6,7,8,9,10]

unsafePerformIO can be used to get a pure API from IO, as long as you know it is safe to do so:

>>> import System.IO.Unsafe (unsafePerformIO)
>>> unsafePerformIO $ Stream.fold Array.write s1 :: Array Int
fromList [1,2,3,4,5,6,7,8,9,10]

To apply a transformation to an array use read to unfold the array into a stream, apply a transformation on the stream and then use write to fold it back to an array.

This module is designed to be imported qualified:

import qualified Streamly.Data.Array as Array

For experimental APIs see Streamly.Internal.Data.Array.

Synopsis

Documentation

data Array a #

Instances

Instances details
NFData1 Array Source # 
Instance details

Defined in Streamly.Data.Array.Foreign

Methods

liftRnf :: (a -> ()) -> Array a -> () Source #

a ~ Char => IsString (Array a) 
Instance details

Defined in Streamly.Internal.Data.Array.Type

Methods

fromString :: String -> Array a Source #

Unbox a => Monoid (Array a) 
Instance details

Defined in Streamly.Internal.Data.Array.Type

Methods

mempty :: Array a Source #

mappend :: Array a -> Array a -> Array a Source #

mconcat :: [Array a] -> Array a Source #

Unbox a => Semigroup (Array a) 
Instance details

Defined in Streamly.Internal.Data.Array.Type

Methods

(<>) :: Array a -> Array a -> Array a Source #

sconcat :: NonEmpty (Array a) -> Array a Source #

stimes :: Integral b => b -> Array a -> Array a Source #

Unbox a => IsList (Array a) 
Instance details

Defined in Streamly.Internal.Data.Array.Type

Associated Types

type Item (Array a) Source #

Methods

fromList :: [Item (Array a)] -> Array a Source #

fromListN :: Int -> [Item (Array a)] -> Array a Source #

toList :: Array a -> [Item (Array a)] Source #

(Unbox a, Read a, Show a) => Read (Array a) 
Instance details

Defined in Streamly.Internal.Data.Array.Type

(Show a, Unbox a) => Show (Array a) 
Instance details

Defined in Streamly.Internal.Data.Array.Type

NFData (Array a) Source # 
Instance details

Defined in Streamly.Data.Array.Foreign

Methods

rnf :: Array a -> () Source #

Eq (Array Int16) 
Instance details

Defined in Streamly.Internal.Data.Array.Type

Eq (Array Int32) 
Instance details

Defined in Streamly.Internal.Data.Array.Type

Eq (Array Int64) 
Instance details

Defined in Streamly.Internal.Data.Array.Type

Eq (Array Int8) 
Instance details

Defined in Streamly.Internal.Data.Array.Type

Eq (Array Word16) 
Instance details

Defined in Streamly.Internal.Data.Array.Type

Eq (Array Word32) 
Instance details

Defined in Streamly.Internal.Data.Array.Type

Eq (Array Word64) 
Instance details

Defined in Streamly.Internal.Data.Array.Type

Eq (Array Word8) 
Instance details

Defined in Streamly.Internal.Data.Array.Type

Eq (Array Char) 
Instance details

Defined in Streamly.Internal.Data.Array.Type

Eq (Array Int) 
Instance details

Defined in Streamly.Internal.Data.Array.Type

(Unbox a, Eq a) => Eq (Array a) 
Instance details

Defined in Streamly.Internal.Data.Array.Type

Methods

(==) :: Array a -> Array a -> Bool Source #

(/=) :: Array a -> Array a -> Bool Source #

(Unbox a, Ord a) => Ord (Array a) 
Instance details

Defined in Streamly.Internal.Data.Array.Type

Methods

compare :: Array a -> Array a -> Ordering Source #

(<) :: Array a -> Array a -> Bool Source #

(<=) :: Array a -> Array a -> Bool Source #

(>) :: Array a -> Array a -> Bool Source #

(>=) :: Array a -> Array a -> Bool Source #

max :: Array a -> Array a -> Array a Source #

min :: Array a -> Array a -> Array a Source #

type Item (Array a) 
Instance details

Defined in Streamly.Internal.Data.Array.Type

type Item (Array a) = a

Arrays

Construction

When performance matters, the fastest way to generate an array is writeN. IsList and IsString instances can be used to conveniently construct arrays from literal values. OverloadedLists extension or fromList can be used to construct an array from a list literal. Similarly, OverloadedStrings extension or fromList can be used to construct an array from a string literal.

fromListN :: Unbox a => Int -> [a] -> Array a #

fromList :: Unbox a => [a] -> Array a #

writeN :: forall (m :: Type -> Type) a. (MonadIO m, Unbox a) => Int -> Fold m a (Array a) #

write :: forall (m :: Type -> Type) a. (MonadIO m, Unbox a) => Fold m a (Array a) #

writeLastN :: forall a (m :: Type -> Type). (Storable a, Unbox a, MonadIO m) => Int -> Fold m a (Array a) #

Elimination

toList :: Unbox a => Array a -> [a] #

read :: forall (m :: Type -> Type) a. (Monad m, Unbox a) => Array a -> Stream m a #

readRev :: forall (m :: Type -> Type) a. (Monad m, Unbox a) => Array a -> Stream m a #

Casting

cast :: forall a b. Unbox b => Array a -> Maybe (Array b) #

Random Access

length :: Unbox a => Array a -> Int #

getIndex :: Unbox a => Int -> Array a -> Maybe a #

Orphan instances

NFData1 Array Source # 
Instance details

Methods

liftRnf :: (a -> ()) -> Array a -> () Source #

NFData (Array a) Source # 
Instance details

Methods

rnf :: Array a -> () Source #