@@ -7,6 +7,7 @@
77
88{-# LANGUAGE CPP #-}
99{-# LANGUAGE FlexibleContexts #-}
10+{-# LANGUAGE ScopedTypeVariables #-}
1011
1112module LinearOps where
1213
@@ -16,6 +17,7 @@ import Prelude
1617 (Monad, Int, (+), ($), (.), return, fmap, even, (>), (<=), (==), (<=),
1718 subtract, undefined, Maybe(..), odd, Bool, not, (>>=), mapM_, curry,
1819 maxBound)
20+import qualified Prelude as P
1921
2022import qualified Streamly as S
2123import qualified Streamly.Prelude as S
@@ -285,6 +287,41 @@ dropWhileMTrue n = composeN n $ S.dropWhileM (return . (<= maxValue))
285287findIndices n = composeN n $ S.findIndices (== maxValue)
286288elemIndices n = composeN n $ S.elemIndices maxValue
287289
290+-------------------------------------------------------------------------------
291+-- Iteration
292+-------------------------------------------------------------------------------
293+
294+{-# INLINE iterateSource #-}
295+iterateSource
296+ :: S.MonadAsync m
297+ => (Stream m Int -> Stream m Int) -> Int -> Int -> Stream m Int
298+iterateSource g i n = f i (sourceUnfoldrMN 10 n)
299+ where
300+ f (0 :: Int) m = g m
301+ f x m = g (f (x P.- 1) m)
302+
303+{-# INLINE iterateMapM #-}
304+{-# INLINE iterateScan #-}
305+{-# INLINE iterateFilterEven #-}
306+{-# INLINE iterateTakeAll #-}
307+{-# INLINE iterateDropOne #-}
308+{-# INLINE iterateDropWhileFalse #-}
309+{-# INLINE iterateDropWhileTrue #-}
310+iterateMapM, iterateScan, iterateFilterEven, iterateTakeAll, iterateDropOne,
311+ iterateDropWhileFalse, iterateDropWhileTrue
312+ :: S.MonadAsync m
313+ => Int -> Stream m Int
314+
315+-- this is quadratic
316+iterateScan = iterateSource (S.scanl' (+) 0) 1000
317+
318+iterateMapM = iterateSource (S.mapM return) 100000
319+iterateFilterEven = iterateSource (S.filter even) 100000
320+iterateTakeAll = iterateSource (S.take maxValue) 100000
321+iterateDropOne = iterateSource (S.drop 1) 100000
322+iterateDropWhileFalse = iterateSource (S.dropWhile (<= 1)) 100000
323+iterateDropWhileTrue = iterateSource (S.dropWhile (<= maxValue)) 100000
324+
288325-------------------------------------------------------------------------------
289326-- Zipping and concat
290327-------------------------------------------------------------------------------