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 #-}
15-- DATA_ARRAY_PRIM_PINNED
17module Streamly.Benchmark.Data.ArrayOps where
19import Control.Monad.IO.Class (MonadIO)
20import Prelude (Bool, Int, Maybe(..), ($), (+), (.), (==), (>), undefined)
22import qualified Prelude as P
23import qualified Streamly.Prelude as S
25#if !defined(DATA_ARRAY_PRIM_PINNED)
28import qualified Data.Foldable as F
33#if defined(DATA_ARRAY_PRIM_PINNED)
34import qualified Streamly.Internal.Data.Array.Prim.Pinned as A
36#elif defined(DATA_ARRAY)
37import qualified Streamly.Internal.Data.Array as A
41-------------------------------------------------------------------------------
43-------------------------------------------------------------------------------
45-------------------------------------------------------------------------------
46-- Stream generation and elimination
47-------------------------------------------------------------------------------
49-------------------------------------------------------------------------------
52-- DATA_ARRAY_PRIM_PINNED
53-------------------------------------------------------------------------------
55{-# INLINE sourceUnfoldr #-}
56sourceUnfoldr :: MonadIO m => Int -> Int -> m (Stream Int)
57sourceUnfoldr value n = S.fold (A.writeN value) $ S.unfoldr step n
62 else (Just (cnt, cnt + 1))
64{-# INLINE sourceIntFromTo #-}
65sourceIntFromTo :: MonadIO m => Int -> Int -> m (Stream Int)
66sourceIntFromTo value n = S.fold (A.writeN value) $ S.enumerateFromTo n (n + value)
68{-# INLINE sourceFromList #-}
69sourceFromList :: MonadIO m => Int -> Int -> m (Stream Int)
70sourceFromList value n = S.fold (A.writeN value) $ S.fromList [n..n+value]
73{-# INLINE sourceIntFromToFromList #-}
74sourceIntFromToFromList :: MonadIO m => Int -> Int -> m (Stream Int)
75sourceIntFromToFromList value n = P.return $ A.fromList $ [n..n + value]
77-------------------------------------------------------------------------------
80-- DATA_ARRAY_PRIM_PINNED
81-------------------------------------------------------------------------------
84{-# INLINE sourceIntFromToFromStream #-}
85sourceIntFromToFromStream :: MonadIO m => Int -> Int -> m (Stream Int)
86sourceIntFromToFromStream value n = S.fold A.write $ S.enumerateFromTo n (n + value)
88-------------------------------------------------------------------------------
90-------------------------------------------------------------------------------
92-------------------------------------------------------------------------------
95-- DATA_ARRAY_PRIM_PINNED
96-------------------------------------------------------------------------------
98{-# INLINE composeN #-}
100 => Int -> (Stream Int -> m (Stream Int)) -> Stream Int -> m (Stream Int)
105 3 -> f x P.>>= f P.>>= f
106 4 -> f x P.>>= f P.>>= f P.>>= f
110{-# INLINE scanl1' #-}
114 :: MonadIO m => Int -> Int -> Stream Int -> m (Stream Int)
117{-# INLINE onArray #-}
119 :: MonadIO m => Int -> (S.SerialT m Int -> S.SerialT m Int)
122onArray value f arr = S.fold (A.writeN value) $ f $ (S.unfold A.read arr)
124scanl' value n = composeN n $ onArray value $ S.scanl' (+) 0
125scanl1' value n = composeN n $ onArray value $ S.scanl1' (+)
126map value n = composeN n $ onArray value $ S.map (+1)
127-- map n = composeN n $ A.map (+1)
129{-# INLINE eqInstance #-}
130eqInstance :: Stream Int -> Bool
131eqInstance src = src == src
133{-# INLINE eqInstanceNotEq #-}
134eqInstanceNotEq :: Stream Int -> Bool
135eqInstanceNotEq src = src P./= src
137{-# INLINE ordInstance #-}
138ordInstance :: Stream Int -> Bool
139ordInstance src = src P.< src
141{-# INLINE ordInstanceMin #-}
142ordInstanceMin :: Stream Int -> Stream Int
143ordInstanceMin src = P.min src src
145{-# INLINE showInstance #-}
146showInstance :: Stream Int -> P.String
147showInstance src = P.show src
149{-# INLINE pureFoldl' #-}
150pureFoldl' :: MonadIO m => Stream Int -> m Int
151pureFoldl' = S.foldl' (+) 0 . S.unfold A.read
153-------------------------------------------------------------------------------
156-------------------------------------------------------------------------------
159#if !defined(DATA_ARRAY_PRIM_PINNED)
160{-# INLINE readInstance #-}
161readInstance :: P.String -> Stream Int
166 _ -> P.error "readInstance: no parse"
170{-# INLINE foldableFoldl' #-}
171foldableFoldl' :: Stream Int -> Int
172foldableFoldl' = F.foldl' (+) 0
174{-# INLINE foldableSum #-}
175foldableSum :: Stream Int -> Int
181-------------------------------------------------------------------------------
183-------------------------------------------------------------------------------
185-------------------------------------------------------------------------------
188-- DATA_ARRAY_PRIM_PINNED
189-------------------------------------------------------------------------------
191{-# INLINE unfoldReadDrain #-}
192unfoldReadDrain :: MonadIO m => Stream Int -> m ()
193unfoldReadDrain = S.drain . S.unfold A.read
195{-# INLINE toStreamRevDrain #-}
196toStreamRevDrain :: MonadIO m => Stream Int -> m ()
197toStreamRevDrain = 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