2-- Module : Streamly.Benchmark.Data.ArrayOps
3-- Copyright : (c) 2020 Composewell Technologies
5-- License : BSD-3-Clause
6-- Maintainer : streamly@composewell.com
9{-# LANGUAGE FlexibleContexts #-}
10{-# LANGUAGE ScopedTypeVariables #-}
18module Streamly.Benchmark.Data.ArrayOps where
20import Control.Monad.IO.Class (MonadIO)
21import Prelude (Bool, Int, Maybe(..), ($), (+), (.), (==), (>), undefined)
23import qualified Prelude as P
24import qualified Streamly as S hiding (foldMapWith, runStream)
25import qualified Streamly.Prelude as S
27#ifndef DATA_PRIM_ARRAY
29import qualified Data.Foldable as F
34import qualified Streamly.Internal.Data.SmallArray as A
35type Stream = A.SmallArray
36#elif defined(MEMORY_ARRAY)
37import qualified GHC.Exts as GHC
38import qualified Streamly.Memory.Array as A
39import qualified Streamly.Internal.Memory.Array as A
41#elif defined(DATA_PRIM_ARRAY)
42import qualified Streamly.Internal.Data.Prim.Array as A
43type Stream = A.PrimArray
45import qualified Streamly.Internal.Data.Array as A
49-------------------------------------------------------------------------------
51-------------------------------------------------------------------------------
53-------------------------------------------------------------------------------
54-- Stream generation and elimination
55-------------------------------------------------------------------------------
57-------------------------------------------------------------------------------
63-------------------------------------------------------------------------------
65{-# INLINE sourceUnfoldr #-}
66sourceUnfoldr :: MonadIO m => Int -> Int -> m (Stream Int)
67sourceUnfoldr value n = S.fold (A.writeN value) $ S.unfoldr step n
72 else (Just (cnt, cnt + 1))
74{-# INLINE sourceIntFromTo #-}
75sourceIntFromTo :: MonadIO m => Int -> Int -> m (Stream Int)
76sourceIntFromTo value n = S.fold (A.writeN value) $ S.enumerateFromTo n (n + value)
78{-# INLINE sourceFromList #-}
79sourceFromList :: MonadIO m => Int -> Int -> m (Stream Int)
80sourceFromList value n = S.fold (A.writeN value) $ S.fromList [n..n+value]
82-- Different defination of sourceIntFromToFromList for DATA_SMALLARRAY
84{-# INLINE sourceIntFromToFromList #-}
85sourceIntFromToFromList :: MonadIO m => Int -> Int -> m (Stream Int)
86#ifndef DATA_SMALLARRAY
87sourceIntFromToFromList value n = P.return $ A.fromList $ [n..n + value]
89sourceIntFromToFromList value n = P.return $ A.fromListN value $ [n..n + value]
92-------------------------------------------------------------------------------
97-------------------------------------------------------------------------------
100#ifndef DATA_SMALLARRAY
101{-# INLINE sourceIntFromToFromStream #-}
102sourceIntFromToFromStream :: MonadIO m => Int -> Int -> m (Stream Int)
103sourceIntFromToFromStream value n = S.fold A.write $ S.enumerateFromTo n (n + value)
106-------------------------------------------------------------------------------
109-------------------------------------------------------------------------------
113{-# INLINE sourceIsList #-}
114sourceIsList :: Int -> Int -> Stream Int
115sourceIsList value n = GHC.fromList [n..n+value]
117{-# INLINE sourceIsString #-}
118sourceIsString :: Int -> Int -> Stream P.Char
119sourceIsString value n = GHC.fromString (P.replicate (n + value) 'a')
122-------------------------------------------------------------------------------
124-------------------------------------------------------------------------------
126-------------------------------------------------------------------------------
132-------------------------------------------------------------------------------
134{-# INLINE composeN #-}
136 => Int -> (Stream Int -> m (Stream Int)) -> Stream Int -> m (Stream Int)
141 3 -> f x P.>>= f P.>>= f
142 4 -> f x P.>>= f P.>>= f P.>>= f
146{-# INLINE scanl1' #-}
150 :: MonadIO m => Int -> Int -> Stream Int -> m (Stream Int)
153{-# INLINE onArray #-}
155 :: MonadIO m => Int -> (S.SerialT m Int -> S.SerialT m Int)
158onArray value f arr = S.fold (A.writeN value) $ f $ (S.unfold A.read arr)
160scanl' value n = composeN n $ onArray value $ S.scanl' (+) 0
161scanl1' value n = composeN n $ onArray value $ S.scanl1' (+)
162map value n = composeN n $ onArray value $ S.map (+1)
163-- map n = composeN n $ A.map (+1)
165{-# INLINE eqInstance #-}
166eqInstance :: Stream Int -> Bool
167eqInstance src = src == src
169{-# INLINE eqInstanceNotEq #-}
170eqInstanceNotEq :: Stream Int -> Bool
171eqInstanceNotEq src = src P./= src
173{-# INLINE ordInstance #-}
174ordInstance :: Stream Int -> Bool
175ordInstance src = src P.< src
177{-# INLINE ordInstanceMin #-}
178ordInstanceMin :: Stream Int -> Stream Int
179ordInstanceMin src = P.min src src
181{-# INLINE showInstance #-}
182showInstance :: Stream Int -> P.String
183showInstance src = P.show src
185{-# INLINE pureFoldl' #-}
186pureFoldl' :: MonadIO m => Stream Int -> m Int
187pureFoldl' = S.foldl' (+) 0 . S.unfold A.read
189-------------------------------------------------------------------------------
194-------------------------------------------------------------------------------
197#ifndef DATA_PRIM_ARRAY
198{-# INLINE readInstance #-}
199readInstance :: P.String -> Stream Int
204 _ -> P.error "readInstance: no parse"
207{-# INLINE foldableFoldl' #-}
208foldableFoldl' :: Stream Int -> Int
209foldableFoldl' = F.foldl' (+) 0
211{-# INLINE foldableSum #-}
212foldableSum :: Stream Int -> Int
217-------------------------------------------------------------------------------
219-------------------------------------------------------------------------------
221-------------------------------------------------------------------------------
227-------------------------------------------------------------------------------
229{-# INLINE unfoldReadDrain #-}
230unfoldReadDrain :: MonadIO m => Stream Int -> m ()
231unfoldReadDrain = S.drain . S.unfold A.read
233{-# INLINE toStreamRevDrain #-}
234toStreamRevDrain :: MonadIO m => Stream Int -> m ()
235toStreamRevDrain = S.drain . A.toStreamRev
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