@@ -11,7 +11,7 @@ module LinearOps where
12import Data.Maybe (fromJust)
12import Data.Maybe (fromJust)
13import Prelude
13import Prelude
14− (Monad, Int, (+), ($), (.), return, fmap, even, (>), (<=),
14+ (Monad, Int, (+), ($), (.), return, fmap, even, (>), (<=), (==), (<=),
15 subtract, undefined, Maybe(..), odd, Bool, not)
15 subtract, undefined, Maybe(..), odd, Bool, not)
17import qualified Streamly as S
17import qualified Streamly as S
@@ -25,76 +25,6 @@ maxValue = value + 1000
25-- Benchmark ops
25-- Benchmark ops
26-------------------------------------------------------------------------------
26-------------------------------------------------------------------------------
29−{-# INLINE nullHeadTail #-}
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 #-}
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
57− => Stream m Int -> m ()
59−{-# INLINE composeMapM #-}
60−{-# INLINE zipAsync #-}
61−{-# INLINE zipAsyncM #-}
62−{-# INLINE mapMaybeM #-}
63−composeMapM, zipAsync, zipAsyncM, mapMaybeM :: S.MonadAsync m => Stream m Int -> m ()
68−toList, foldr, foldrM :: Monad m => Stream m Int -> m [Int]
71−{-# INLINE maximum #-}
72−{-# INLINE minimum #-}
73−last, minimum, maximum :: Monad m => Stream m Int -> m (Maybe Int)
78−{-# INLINE product #-}
79−foldl, length, sum, product :: Monad m => Stream m Int -> m Int
84−{-# INLINE notElem #-}
85−elem, notElem, all, any :: Monad m => Stream m Int -> m Bool
88−toNull :: Monad m => (t m Int -> S.SerialT m Int) -> t m Int -> m ()
91−mapM :: (S.IsStream t, S.MonadAsync m)
92− => (t m Int -> S.SerialT m Int) -> t m Int -> m ()
94−{-# INLINE sequence #-}
95−sequence :: (S.IsStream t, S.MonadAsync m)
96− => (t m Int -> S.SerialT m Int) -> t m (m Int) -> m ()
98-------------------------------------------------------------------------------
28-------------------------------------------------------------------------------
99-- Stream generation and elimination
29-- Stream generation and elimination
100-------------------------------------------------------------------------------
30-------------------------------------------------------------------------------
@@ -167,12 +97,65 @@ sourceUnfoldrMAction n = S.serially $ S.unfoldrM step n
167runStream :: Monad m => Stream m a -> m ()
97runStream :: Monad m => Stream m a -> m ()
168runStream = S.runStream
98runStream = S.runStream
100+{-# INLINE toList #-}
102+{-# INLINE foldrM #-}
103+toList, foldr, foldrM :: Monad m => Stream m Int -> m [Int]
106+{-# INLINE maximum #-}
107+{-# INLINE minimum #-}
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)
115+{-# INLINE foldl' #-}
116+{-# INLINE length #-}
118+{-# INLINE product #-}
119+foldl', length, sum, product :: Monad m => Stream m Int -> m Int
126+{-# INLINE notElem #-}
127+elem, notElem, all, any, and, or :: Monad m => Stream m Int -> m Bool
129+{-# INLINE toNull #-}
130+toNull :: Monad m => (t m Int -> S.SerialT m Int) -> t m Int -> m ()
170toNull t = runStream . t
131toNull t = runStream . t
133+{-# INLINE uncons #-}
134+uncons :: Monad m => Stream m Int -> m ()
171uncons s = do
135uncons s = do
172 r <- S.uncons s
136 r <- S.uncons s
173 case r of
137 case r of
174 Nothing -> return ()
138 Nothing -> return ()
175 Just (_, t) -> uncons t
139 Just (_, t) -> uncons t
142+init :: Monad m => Stream m a -> m ()
146+ Nothing -> return ()
147+ Just x -> S.runStream x
150+tail :: Monad m => Stream m a -> m ()
154+ Nothing -> return ()
157+{-# INLINE nullHeadTail #-}
158+nullHeadTail :: Monad m => Stream m Int -> m ()
176nullHeadTail s = do
159nullHeadTail s = do
177 r <- S.null s
160 r <- S.null s
@@ -183,17 +166,25 @@ nullHeadTail s = do
183 Nothing -> return ()
166 Nothing -> return ()
184 Just x -> nullHeadTail x
167 Just x -> nullHeadTail x
185 else return ()
168 else return ()
186mapM_ = S.mapM_ (\_ -> return ())
170mapM_ = S.mapM_ (\_ -> return ())
187toList = S.toList
171toList = S.toList
188foldr = S.foldr (:) []
172foldr = S.foldr (:) []
173+foldr1 = S.foldr1 (+)
189foldrM = S.foldrM (\a xs -> return (a : xs)) []
174foldrM = S.foldrM (\a xs -> return (a : xs)) []
190−foldl = S.foldl' (+) 0
175+foldl' = S.foldl' (+) 0
176+foldl1' = S.foldl1' (+)
191last = S.last
177last = S.last
192elem = S.elem maxValue
178elem = S.elem maxValue
193notElem = S.notElem maxValue
179notElem = S.notElem maxValue
194length = S.length
180length = S.length
195all = S.all (<= maxValue)
181all = S.all (<= maxValue)
196any = S.any (> maxValue)
182any = 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
197maximum = S.maximum
188maximum = S.maximum
198minimum = S.minimum
189minimum = S.minimum
199sum = S.sum
190sum = S.sum
@@ -207,6 +198,41 @@ product = S.product
207transform :: Monad m => Stream m a -> m ()
198transform :: Monad m => Stream m a -> m ()
208transform = runStream
199transform = runStream
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
223+ => Stream m Int -> m ()
225+{-# INLINE mapMaybeM #-}
226+mapMaybeM :: S.MonadAsync m => Stream m Int -> m ()
229+mapM :: (S.IsStream t, S.MonadAsync m)
230+ => (t m Int -> S.SerialT m Int) -> t m Int -> m ()
232+{-# INLINE sequence #-}
233+sequence :: (S.IsStream t, S.MonadAsync m)
234+ => (t m Int -> S.SerialT m Int) -> t m (m Int) -> m ()
210scan = transform . S.scanl' (+) 0
236scan = transform . S.scanl' (+) 0
211fmap = transform . Prelude.fmap (+1)
237fmap = transform . Prelude.fmap (+1)
212map = transform . S.map (+1)
238map = transform . S.map (+1)
@@ -226,11 +252,22 @@ takeWhileMTrue = transform . S.takeWhileM (return . (<= maxValue))
226dropAll = transform . S.drop maxValue
252dropAll = transform . S.drop maxValue
227dropWhileTrue = transform . S.dropWhile (<= maxValue)
253dropWhileTrue = transform . S.dropWhile (<= maxValue)
228dropWhileMTrue = transform . S.dropWhileM (return . (<= maxValue))
254dropWhileMTrue = transform . S.dropWhileM (return . (<= maxValue))
255+findIndices = transform . S.findIndices (== maxValue)
256+elemIndices = transform . S.elemIndices maxValue
230-------------------------------------------------------------------------------
258-------------------------------------------------------------------------------
231-- Zipping and concat
259-- Zipping and concat
232-------------------------------------------------------------------------------
260-------------------------------------------------------------------------------
264+{-# INLINE concat #-}
265+zip, zipM, concat :: Monad m => Stream m Int -> m ()
267+{-# INLINE zipAsync #-}
268+{-# INLINE zipAsyncM #-}
269+zipAsync, zipAsyncM :: S.MonadAsync m => Stream m Int -> m ()
234zip src = do
271zip src = do
235 r <- S.tail src
272 r <- S.tail src
236 let src1 = fromJust r
273 let src1 = fromJust r
@@ -257,6 +294,16 @@ concat _n = return ()
257compose :: Monad m => (Stream m Int -> Stream m Int) -> Stream m Int -> m ()
294compose :: Monad m => (Stream m Int -> Stream m Int) -> Stream m Int -> m ()
258compose f = transform . f . f . f . f
295compose f = transform . f . f . f . f
297+{-# INLINE composeMapM #-}
298+{-# INLINE composeAllInFilters #-}
299+{-# INLINE composeAllOutFilters #-}
300+{-# INLINE composeMapAllInFilter #-}
301+composeAllInFilters, composeAllOutFilters,
302+ composeMapAllInFilter
304+ => Stream m Int -> m ()
305+composeMapM :: S.MonadAsync m => Stream m Int -> m ()
260composeMapM = compose (S.mapM return)
307composeMapM = compose (S.mapM return)
261composeAllInFilters = compose (S.filter (<= maxValue))
308composeAllInFilters = compose (S.filter (<= maxValue))
262composeAllOutFilters = compose (S.filter (> maxValue))
309composeAllOutFilters = compose (S.filter (> maxValue))