on 7cb856b help sign in
147 lines 4.5 kB history raw
1{-# LANGUAGE CPP #-}
2-- |
3-- Module : Streamly.Data.Array
4-- Copyright : (c) 2019 Composewell Technologies
5--
6-- License : BSD3
7-- Maintainer : streamly@composewell.com
8-- Stability : released
9-- Portability : GHC
10--
11-- Unboxed immutable arrays with streaming interfaces.
12--
13-- Please refer to "Streamly.Internal.Data.Array" for functions that have not
14-- yet been released.
15--
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
21 (
22 -- * Setup
23 -- | To execute the code examples provided in this module in ghci, please
24 -- run the following commands first.
25 --
26 -- $setup
28 -- * Overview
29 -- $overview
31 -- * The Array Type
32 Array
34 -- * Construction
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.
42 -- Pure List APIs
43 , fromListN
44 , fromList
46 -- Monadic APIs
47 , writeN -- drop new
48 , write -- full buffer
49 , writeLastN -- drop old (ring buffer)
51 -- * Conversion
52 -- 'GHC.Exts.toList' from "GHC.Exts" can be used to convert an array to a
53 -- list.
54 , toList
56 -- * Unfolds
57 , reader
58 , readerRev
60 -- * Casting
61 , cast
62 , asBytes
64 -- * Random Access
65 , length
66 -- , (!!)
67 , getIndex
69 -- * Unbox Type Class
70 , Unbox (..)
72 -- * Pinning & Unpinning
73 , pin
74 , unpin
75 , isPinned
76 )
77where
79#include "inline.hs"
81import Streamly.Internal.Data.Array hiding (read, readRev)
82import Streamly.Internal.Data.Unbox (Unbox (..))
84import Prelude hiding (read, length)
86#include "DocTestDataArray.hs"
88-- $overview
89--
90-- This module provides APIs to create and use unboxed immutable arrays. Once
91-- created, their contents cannot be modified. Only types that are unboxable
92-- via the 'Unbox' type class can be stored in these arrays. Note that the
93-- array memory grows automatically when creating a new array, therefore, an
94-- array can be created from a variable length stream.
95--
96-- == Folding Arrays
97--
98-- Convert array to stream, and fold the stream:
99--
100-- >>> fold f arr = Stream.unfold Array.reader arr & Stream.fold f
101-- >>> fold Fold.sum (Array.fromList [1,2,3::Int])
102-- 6
103--
104-- == Transforming Arrays
105--
106-- Convert array to stream, transform, and fold back to array:
107--
108-- >>> amap f arr = Stream.unfold Array.reader arr & fmap f & Stream.fold Array.write
109-- >>> amap (+1) (Array.fromList [1,2,3::Int])
110-- fromList [2,3,4]
111--
112-- == Pinned and Unpinned Arrays
113--
114-- The array type can use both pinned and unpinned memory under the hood. The
115-- default array creation operations create unpinned arrays. IO operations
116-- automatically copy an array to pinned memory if the array passed to it is
117-- unpinned. Programmers can use appropriate pinned array generation APIs to
118-- reduce the copying if it happens.
119--
120-- Unpinned arrays have the advantage of allowing automatic defragmentation of
121-- the memory by GC. Whereas pinned arrays have the advantage of not requiring
122-- a copy by GC. Normally you would want to use unpinned arrays. However, in
123-- some cases, for example, for long lived large data storage, and for
124-- interfacing with the operating system or foreign (non-Haskell) consumers you
125-- may want to use pinned arrays.
126--
127-- == Creating Arrays from Non-IO Streams
128--
129-- Array creation folds require 'MonadIO' otherwise the compiler may
130-- incorrectly share the array memory thinking it is pure.
131--
132-- See the 'fromPureStream' unreleased API to generate an array from an
133-- Identity stream safely without using MonadIO constraint.
134--
135--
136-- Note that 'Identity' streams can be generalized to IO streams:
137--
138-- >>> pure = Stream.fromList [1,2,3] :: Stream Identity Int
139-- >>> generally = Stream.morphInner (return . runIdentity)
140-- >>> Stream.fold Array.write (generally pure :: Stream IO Int)
141-- fromList [1,2,3]
142--
143-- == Programming Tips
144--
145-- This module is designed to be imported qualified:
146--
147-- >>> import qualified Streamly.Data.Array as Array