@@ -11,7 +11,7 @@ module LinearOps where
1111
1212import Data.Maybe (fromJust)
1313import Prelude
14− (Monad, Int, (+), ($), (.), return, fmap, even, (>), (<=),
14+ (Monad, Int, (+), ($), (.), return, fmap, even, (>), (<=), (==), (<=),
1515 subtract, undefined, Maybe(..), odd, Bool, not)
1616
1717import qualified Streamly as S
@@ -25,76 +25,6 @@ maxValue = value + 1000
2525-- Benchmark ops
2626-------------------------------------------------------------------------------
2727
28−{-# INLINE uncons #-}
29−{-# INLINE nullHeadTail #-}
30−{-# INLINE scan #-}
31−{-# INLINE mapM_ #-}
32−{-# INLINE map #-}
33−{-# INLINE fmap #-}
34−{-# INLINE mapMaybe #-}
35−{-# INLINE filterEven #-}
36−{-# INLINE filterAllOut #-}
37−{-# INLINE filterAllIn #-}
38−{-# INLINE takeOne #-}
39−{-# INLINE takeAll #-}
40−{-# INLINE takeWhileTrue #-}
41−{-# INLINE takeWhileMTrue #-}
42−{-# INLINE dropAll #-}
43−{-# INLINE dropWhileTrue #-}
44−{-# INLINE dropWhileMTrue #-}
45−{-# INLINE zip #-}
46−{-# INLINE zipM #-}
47−{-# INLINE concat #-}
48−{-# INLINE composeAllInFilters #-}
49−{-# INLINE composeAllOutFilters #-}
50−{-# INLINE composeMapAllInFilter #-}
51−uncons, nullHeadTail, scan, mapM_, map, fmap, mapMaybe, filterEven, filterAllOut,
52− filterAllIn, takeOne, takeAll, takeWhileTrue, takeWhileMTrue, dropAll,
53− dropWhileTrue, dropWhileMTrue, zip, zipM,
54− concat, composeAllInFilters, composeAllOutFilters,
55− composeMapAllInFilter
56− :: Monad m
57− => Stream m Int -> m ()
58−
59−{-# INLINE composeMapM #-}
60−{-# INLINE zipAsync #-}
61−{-# INLINE zipAsyncM #-}
62−{-# INLINE mapMaybeM #-}
63−composeMapM, zipAsync, zipAsyncM, mapMaybeM :: S.MonadAsync m => Stream m Int -> m ()
64−
65−{-# INLINE toList #-}
66−{-# INLINE foldr #-}
67−{-# INLINE foldrM #-}
68−toList, foldr, foldrM :: Monad m => Stream m Int -> m [Int]
69−
70−{-# INLINE last #-}
71−{-# INLINE maximum #-}
72−{-# INLINE minimum #-}
73−last, minimum, maximum :: Monad m => Stream m Int -> m (Maybe Int)
74−
75−{-# INLINE foldl #-}
76−{-# INLINE length #-}
77−{-# INLINE sum #-}
78−{-# INLINE product #-}
79−foldl, length, sum, product :: Monad m => Stream m Int -> m Int
80−
81−{-# INLINE all #-}
82−{-# INLINE any #-}
83−{-# INLINE elem #-}
84−{-# INLINE notElem #-}
85−elem, notElem, all, any :: Monad m => Stream m Int -> m Bool
86−
87−{-# INLINE toNull #-}
88−toNull :: Monad m => (t m Int -> S.SerialT m Int) -> t m Int -> m ()
89−
90−{-# INLINE mapM #-}
91−mapM :: (S.IsStream t, S.MonadAsync m)
92− => (t m Int -> S.SerialT m Int) -> t m Int -> m ()
93−
94−{-# INLINE sequence #-}
95−sequence :: (S.IsStream t, S.MonadAsync m)
96− => (t m Int -> S.SerialT m Int) -> t m (m Int) -> m ()
97−
9828-------------------------------------------------------------------------------
9929-- Stream generation and elimination
10030-------------------------------------------------------------------------------
@@ -167,12 +97,65 @@ sourceUnfoldrMAction n = S.serially $ S.unfoldrM step n
16797runStream :: Monad m => Stream m a -> m ()
16898runStream = S.runStream
16999
100+{-# INLINE toList #-}
101+{-# INLINE foldr #-}
102+{-# INLINE foldrM #-}
103+toList, foldr, foldrM :: Monad m => Stream m Int -> m [Int]
104+
105+{-# INLINE last #-}
106+{-# INLINE maximum #-}
107+{-# INLINE minimum #-}
108+{-# INLINE find #-}
109+{-# INLINE findIndex #-}
110+{-# INLINE elemIndex #-}
111+{-# INLINE foldl1' #-}
112+{-# INLINE foldr1 #-}
113+last, minimum, maximum, find, findIndex, elemIndex, foldl1', foldr1 :: Monad m => Stream m Int -> m (Maybe Int)
114+
115+{-# INLINE foldl' #-}
116+{-# INLINE length #-}
117+{-# INLINE sum #-}
118+{-# INLINE product #-}
119+foldl', length, sum, product :: Monad m => Stream m Int -> m Int
120+
121+{-# INLINE all #-}
122+{-# INLINE any #-}
123+{-# INLINE and #-}
124+{-# INLINE or #-}
125+{-# INLINE elem #-}
126+{-# INLINE notElem #-}
127+elem, notElem, all, any, and, or :: Monad m => Stream m Int -> m Bool
128+
129+{-# INLINE toNull #-}
130+toNull :: Monad m => (t m Int -> S.SerialT m Int) -> t m Int -> m ()
170131toNull t = runStream . t
132+
133+{-# INLINE uncons #-}
134+uncons :: Monad m => Stream m Int -> m ()
171135uncons s = do
172136 r <- S.uncons s
173137 case r of
174138 Nothing -> return ()
175139 Just (_, t) -> uncons t
140+
141+{-# INLINE init #-}
142+init :: Monad m => Stream m a -> m ()
143+init s = do
144+ r <- S.init s
145+ case r of
146+ Nothing -> return ()
147+ Just x -> S.runStream x
148+
149+{-# INLINE tail #-}
150+tail :: Monad m => Stream m a -> m ()
151+tail s = do
152+ r <- S.tail s
153+ case r of
154+ Nothing -> return ()
155+ Just x -> tail x
156+
157+{-# INLINE nullHeadTail #-}
158+nullHeadTail :: Monad m => Stream m Int -> m ()
176159nullHeadTail s = do
177160 r <- S.null s
178161 if not r
@@ -183,17 +166,25 @@ nullHeadTail s = do
183166 Nothing -> return ()
184167 Just x -> nullHeadTail x
185168 else return ()
169+
186170mapM_ = S.mapM_ (\_ -> return ())
187171toList = S.toList
188172foldr = S.foldr (:) []
173+foldr1 = S.foldr1 (+)
189174foldrM = S.foldrM (\a xs -> return (a : xs)) []
190−foldl = S.foldl' (+) 0
175+foldl' = S.foldl' (+) 0
176+foldl1' = S.foldl1' (+)
191177last = S.last
192178elem = S.elem maxValue
193179notElem = S.notElem maxValue
194180length = S.length
195181all = S.all (<= maxValue)
196182any = S.any (> maxValue)
183+and = S.and . S.map (<= maxValue)
184+or = S.or . S.map (> maxValue)
185+find = S.find (== maxValue)
186+findIndex = S.findIndex (== maxValue)
187+elemIndex = S.elemIndex maxValue
197188maximum = S.maximum
198189minimum = S.minimum
199190sum = S.sum
@@ -207,6 +198,41 @@ product = S.product
207198transform :: Monad m => Stream m a -> m ()
208199transform = runStream
209200
201+{-# INLINE scan #-}
202+{-# INLINE mapM_ #-}
203+{-# INLINE map #-}
204+{-# INLINE fmap #-}
205+{-# INLINE mapMaybe #-}
206+{-# INLINE filterEven #-}
207+{-# INLINE filterAllOut #-}
208+{-# INLINE filterAllIn #-}
209+{-# INLINE takeOne #-}
210+{-# INLINE takeAll #-}
211+{-# INLINE takeWhileTrue #-}
212+{-# INLINE takeWhileMTrue #-}
213+{-# INLINE dropAll #-}
214+{-# INLINE dropWhileTrue #-}
215+{-# INLINE dropWhileMTrue #-}
216+{-# INLINE findIndices #-}
217+{-# INLINE elemIndices #-}
218+scan, mapM_, map, fmap, mapMaybe, filterEven, filterAllOut,
219+ filterAllIn, takeOne, takeAll, takeWhileTrue, takeWhileMTrue, dropAll,
220+ dropWhileTrue, dropWhileMTrue,
221+ findIndices, elemIndices
222+ :: Monad m
223+ => Stream m Int -> m ()
224+
225+{-# INLINE mapMaybeM #-}
226+mapMaybeM :: S.MonadAsync m => Stream m Int -> m ()
227+
228+{-# INLINE mapM #-}
229+mapM :: (S.IsStream t, S.MonadAsync m)
230+ => (t m Int -> S.SerialT m Int) -> t m Int -> m ()
231+
232+{-# INLINE sequence #-}
233+sequence :: (S.IsStream t, S.MonadAsync m)
234+ => (t m Int -> S.SerialT m Int) -> t m (m Int) -> m ()
235+
210236scan = transform . S.scanl' (+) 0
211237fmap = transform . Prelude.fmap (+1)
212238map = transform . S.map (+1)
@@ -226,11 +252,22 @@ takeWhileMTrue = transform . S.takeWhileM (return . (<= maxValue))
226252dropAll = transform . S.drop maxValue
227253dropWhileTrue = transform . S.dropWhile (<= maxValue)
228254dropWhileMTrue = transform . S.dropWhileM (return . (<= maxValue))
255+findIndices = transform . S.findIndices (== maxValue)
256+elemIndices = transform . S.elemIndices maxValue
229257
230258-------------------------------------------------------------------------------
231259-- Zipping and concat
232260-------------------------------------------------------------------------------
233261
262+{-# INLINE zip #-}
263+{-# INLINE zipM #-}
264+{-# INLINE concat #-}
265+zip, zipM, concat :: Monad m => Stream m Int -> m ()
266+
267+{-# INLINE zipAsync #-}
268+{-# INLINE zipAsyncM #-}
269+zipAsync, zipAsyncM :: S.MonadAsync m => Stream m Int -> m ()
270+
234271zip src = do
235272 r <- S.tail src
236273 let src1 = fromJust r
@@ -257,6 +294,16 @@ concat _n = return ()
257294compose :: Monad m => (Stream m Int -> Stream m Int) -> Stream m Int -> m ()
258295compose f = transform . f . f . f . f
259296
297+{-# INLINE composeMapM #-}
298+{-# INLINE composeAllInFilters #-}
299+{-# INLINE composeAllOutFilters #-}
300+{-# INLINE composeMapAllInFilter #-}
301+composeAllInFilters, composeAllOutFilters,
302+ composeMapAllInFilter
303+ :: Monad m
304+ => Stream m Int -> m ()
305+composeMapM :: S.MonadAsync m => Stream m Int -> m ()
306+
260307composeMapM = compose (S.mapM return)
261308composeAllInFilters = compose (S.filter (<= maxValue))
262309composeAllOutFilters = compose (S.filter (> maxValue))