@@ -0,0 +1,150 @@
2+-- Module : Streamly.Benchmark.Data.PrimArrayOps
3+-- Copyright : (c) 2019 Composewell Technologies
5+-- License : BSD-3-Clause
6+-- Maintainer : streamly@composewell.com
7+-- Stability : experimental
11+{-# LANGUAGE DeriveAnyClass #-}
12+{-# LANGUAGE DeriveGeneric #-}
13+{-# LANGUAGE FlexibleContexts #-}
14+{-# LANGUAGE ScopedTypeVariables #-}
16+module Streamly.Benchmark.Data.PrimArrayOps where
18+import Control.Monad.IO.Class (MonadIO)
19+import Prelude (Int, Bool, (+), ($), (==), (>), (.), Maybe(..), undefined)
20+import qualified Prelude as P
22+import qualified Data.Foldable as F
25+import qualified Streamly as S hiding (foldMapWith, runStream)
26+import qualified Streamly.Data.PrimArray as A
27+import qualified Streamly.Prelude as S
32+-------------------------------------------------------------------------------
34+-------------------------------------------------------------------------------
36+-------------------------------------------------------------------------------
37+-- Stream generation and elimination
38+-------------------------------------------------------------------------------
40+type Stream = A.PrimArray
42+{-# INLINE sourceUnfoldr #-}
43+sourceUnfoldr :: MonadIO m => Int -> m (Stream Int)
44+sourceUnfoldr n = S.fold (A.writeN value) $ S.unfoldr step n
49+ else (Just (cnt, cnt + 1))
51+{-# INLINE sourceIntFromTo #-}
52+sourceIntFromTo :: MonadIO m => Int -> m (Stream Int)
53+sourceIntFromTo n = S.fold (A.writeN value) $ S.enumerateFromTo n (n + value)
55+{-# INLINE sourceIntFromToFromStream #-}
56+sourceIntFromToFromStream :: MonadIO m => Int -> m (Stream Int)
57+sourceIntFromToFromStream n = S.fold A.write $ S.enumerateFromTo n (n + value)
59+{-# INLINE sourceIntFromToFromList #-}
60+sourceIntFromToFromList :: MonadIO m => Int -> m (Stream Int)
61+sourceIntFromToFromList n = P.return $ A.fromList $ [n..n + value]
63+{-# INLINE sourceFromList #-}
64+sourceFromList :: MonadIO m => Int -> m (Stream Int)
65+sourceFromList n = S.fold (A.writeN value) $ S.fromList [n..n+value]
67+{-# INLINE sourceIsList #-}
68+sourceIsList :: Int -> Stream Int
69+sourceIsList n = GHC.fromList [n..n+value]
71+{-# INLINE sourceIsString #-}
72+sourceIsString :: Int -> Stream P.Char
73+sourceIsString n = GHC.fromString (P.replicate (n + value) 'a')
75+-------------------------------------------------------------------------------
77+-------------------------------------------------------------------------------
79+{-# INLINE composeN #-}
81+ => Int -> (Stream Int -> m (Stream Int)) -> Stream Int -> m (Stream Int)
86+ 3 -> f x P.>>= f P.>>= f
87+ 4 -> f x P.>>= f P.>>= f P.>>= f
91+{-# INLINE scanl1' #-}
95+ :: MonadIO m => Int -> Stream Int -> m (Stream Int)
97+{-# INLINE onArray #-}
99+ :: MonadIO m => (S.SerialT m Int -> S.SerialT m Int)
102+onArray f arr = S.fold (A.writeN value) $ f $ (S.unfold A.read arr)
104+scanl' n = composeN n $ onArray $ S.scanl' (+) 0
105+scanl1' n = composeN n $ onArray $ S.scanl1' (+)
106+map n = composeN n $ onArray $ S.map (+1)
108+{-# INLINE eqInstance #-}
109+eqInstance :: Stream Int -> Bool
110+eqInstance src = src == src
112+{-# INLINE eqInstanceNotEq #-}
113+eqInstanceNotEq :: Stream Int -> Bool
114+eqInstanceNotEq src = src P./= src
116+{-# INLINE ordInstance #-}
117+ordInstance :: Stream Int -> Bool
118+ordInstance src = src P.< src
120+{-# INLINE ordInstanceMin #-}
121+ordInstanceMin :: Stream Int -> Stream Int
122+ordInstanceMin src = P.min src src
124+{-# INLINE showInstance #-}
125+showInstance :: Stream Int -> P.String
126+showInstance src = P.show src
129+{-# INLINE readInstance #-}
130+readInstance :: P.String -> Stream Int
135+ _ -> P.error "readInstance: no parse"
138+{-# INLINE pureFoldl' #-}
139+pureFoldl' :: MonadIO m => Stream Int -> m Int
140+pureFoldl' = S.foldl' (+) 0 . S.unfold A.read
143+{-# INLINE foldableFoldl' #-}
144+foldableFoldl' :: Stream Int -> Int
145+foldableFoldl' = F.foldl' (+) 0
147+{-# INLINE foldableSum #-}
148+foldableSum :: Stream Int -> Int