3-- Module : Streamly.Data.Array
4-- Copyright : (c) 2019 Composewell Technologies
7-- Maintainer : streamly@composewell.com
11-- Unboxed immutable arrays with streaming interfaces.
13-- Please refer to "Streamly.Internal.Data.Array" for functions that have not
16-- For arrays that work on boxed types, not requiring the 'Unbox' constraint,
17-- please refer to "Streamly.Data.Array.Generic". For arrays that can be
18-- mutated in-place, please see "Streamly.Data.MutArray".
20module Streamly.Data.Array
23 -- | To execute the code examples provided in this module in ghci, please
24 -- run the following commands first.
35 -- | When performance matters, the fastest way to generate an array is
36 -- 'writeN'. 'IsList' and 'IsString' instances can be
37 -- used to conveniently construct arrays from literal values.
38 -- 'OverloadedLists' extension or 'fromList' can be used to construct an
39 -- array from a list literal. Similarly, 'OverloadedStrings' extension or
40 -- 'fromList' can be used to construct an array from a string literal.
48 , write -- full buffer
49 , writeLastN -- drop old (ring buffer)
52 -- 'GHC.Exts.toList' from "GHC.Exts" can be used to convert an array to a
72 -- * Pinning & Unpinning
85import Streamly.Internal.Data.Array hiding (read, readRev)
86import Streamly.Internal.Data.Unbox (Unbox (..))
87import Streamly.Internal.Data.Unfold (Unfold)
89import Prelude hiding (read, length)
91#include "DocTestDataArray.hs"
95-- This module provides APIs to create and use unboxed immutable arrays. Once
96-- created, their contents cannot be modified. Only types that are unboxable
97-- via the 'Unbox' type class can be stored in these arrays. Note that the
98-- array memory grows automatically when creating a new array, therefore, an
99-- array can be created from a variable length stream.
103-- Convert array to stream, and fold the stream:
105-- >>> fold f arr = Stream.unfold Array.reader arr & Stream.fold f
106-- >>> fold Fold.sum (Array.fromList [1,2,3::Int])
109-- == Transforming Arrays
111-- Convert array to stream, transform, and fold back to array:
113-- >>> amap f arr = Stream.unfold Array.reader arr & fmap f & Stream.fold Array.write
114-- >>> amap (+1) (Array.fromList [1,2,3::Int])
117-- == Pinned and Unpinned Arrays
119-- The array type can use both pinned and unpinned memory under the hood. The
120-- default array creation operations create unpinned arrays. IO operations
121-- automatically copy an array to pinned memory if the array passed to it is
122-- unpinned. Programmers can use appropriate pinned array generation APIs to
123-- reduce the copying if it happens.
125-- Unpinned arrays have the advantage of allowing automatic defragmentation of
126-- the memory by GC. Whereas pinned arrays have the advantage of not requiring
127-- a copy by GC. Normally you would want to use unpinned arrays. However, in
128-- some cases, for example, for long lived large data storage, and for
129-- interfacing with the operating system or foreign (non-Haskell) consumers you
130-- may want to use pinned arrays.
132-- == Creating Arrays from Non-IO Streams
134-- Array creation folds require 'MonadIO' otherwise the compiler may
135-- incorrectly share the array memory thinking it is pure.
137-- See the 'fromPureStream' unreleased API to generate an array from an
138-- Identity stream safely without using MonadIO constraint.
141-- Note that 'Identity' streams can be generalized to IO streams:
143-- >>> pure = Stream.fromList [1,2,3] :: Stream Identity Int
144-- >>> generally = Stream.morphInner (return . runIdentity)
145-- >>> Stream.fold Array.write (generally pure :: Stream IO Int)
148-- == Programming Tips
150-- This module is designed to be imported qualified:
152-- >>> import qualified Streamly.Data.Array as Array
156{-# DEPRECATED read "Please use 'reader' instead" #-}
157{-# INLINE_NORMAL read #-}
158read :: (Monad m, Unbox a) => Unfold m (Array a) a
161-- | Same as 'readerRev'
163{-# DEPRECATED readRev "Please use 'readerRev' instead" #-}
164{-# INLINE_NORMAL readRev #-}
165readRev :: (Monad m, Unbox a) => Unfold m (Array a) a
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list