@@ -14,7 +14,8 @@ import Control.Monad (when)
14import Data.Maybe (fromJust)
14import Data.Maybe (fromJust)
15import Prelude
15import Prelude
16 (Monad, Int, (+), ($), (.), return, fmap, even, (>), (<=), (==), (<=),
16 (Monad, Int, (+), ($), (.), return, fmap, even, (>), (<=), (==), (<=),
17− subtract, undefined, Maybe(..), odd, Bool, not, (>>=), mapM_, curry)
17+ subtract, undefined, Maybe(..), odd, Bool, not, (>>=), mapM_, curry,
19import qualified Streamly as S
20import qualified Streamly as S
20import qualified Streamly.Prelude as S
21import qualified Streamly.Prelude as S
@@ -169,7 +170,10 @@ nullHeadTail s = do
169 _ <- S.head s
170 _ <- S.head s
170 S.tail s >>= Prelude.mapM_ nullHeadTail
171 S.tail s >>= Prelude.mapM_ nullHeadTail
174+mapM_ :: Monad m => Stream m Int -> m ()
172mapM_ = S.mapM_ (\_ -> return ())
175mapM_ = S.mapM_ (\_ -> return ())
173toList = S.toList
177toList = S.toList
174foldr = S.foldr (:) []
178foldr = S.foldr (:) []
175foldr1 = S.foldr1 (+)
179foldr1 = S.foldr1 (+)
@@ -200,8 +204,32 @@ product = S.product
200transform :: Monad m => Stream m a -> m ()
204transform :: Monad m => Stream m a -> m ()
201transform = runStream
205transform = runStream
207+{-# INLINE composeN #-}
210+ => Int -> (Stream m Int -> Stream m Int) -> Stream m Int -> m ()
214+ 2 -> transform . f . f
215+ 3 -> transform . f . f . f
216+ 4 -> transform . f . f . f . f
219+-- polymorphic stream version of composeN
220+{-# INLINE composeN' #-}
222+ :: (S.IsStream t, Monad m)
223+ => Int -> (t m Int -> Stream m Int) -> t m Int -> m ()
227+ 2 -> transform . f . S.adapt . f
228+ 3 -> transform . f . S.adapt . f . S.adapt . f
229+ 4 -> transform . f . S.adapt . f . S.adapt . f . S.adapt . f
203{-# INLINE scan #-}
232{-# INLINE scan #-}
205{-# INLINE map #-}
233{-# INLINE map #-}
206{-# INLINE fmap #-}
234{-# INLINE fmap #-}
207{-# INLINE mapMaybe #-}
235{-# INLINE mapMaybe #-}
@@ -217,45 +245,45 @@ transform = runStream
217{-# INLINE dropWhileMTrue #-}
245{-# INLINE dropWhileMTrue #-}
218{-# INLINE findIndices #-}
246{-# INLINE findIndices #-}
219{-# INLINE elemIndices #-}
247{-# INLINE elemIndices #-}
220−scan, mapM_, map, fmap, mapMaybe, filterEven, filterAllOut,
248+scan, map, fmap, mapMaybe, filterEven, filterAllOut,
221 filterAllIn, takeOne, takeAll, takeWhileTrue, takeWhileMTrue, dropAll,
249 filterAllIn, takeOne, takeAll, takeWhileTrue, takeWhileMTrue, dropAll,
222 dropWhileTrue, dropWhileMTrue,
250 dropWhileTrue, dropWhileMTrue,
223 findIndices, elemIndices
251 findIndices, elemIndices
224 :: Monad m
252 :: Monad m
225− => Stream m Int -> m ()
253+ => Int -> Stream m Int -> m ()
227{-# INLINE mapMaybeM #-}
255{-# INLINE mapMaybeM #-}
228−mapMaybeM :: S.MonadAsync m => Stream m Int -> m ()
256+mapMaybeM :: S.MonadAsync m => Int -> Stream m Int -> m ()
230{-# INLINE mapM #-}
258{-# INLINE mapM #-}
231mapM :: (S.IsStream t, S.MonadAsync m)
259mapM :: (S.IsStream t, S.MonadAsync m)
232− => (t m Int -> S.SerialT m Int) -> t m Int -> m ()
260+ => (t m Int -> S.SerialT m Int) -> Int -> t m Int -> m ()
234{-# INLINE sequence #-}
262{-# INLINE sequence #-}
235sequence :: (S.IsStream t, S.MonadAsync m)
263sequence :: (S.IsStream t, S.MonadAsync m)
236 => (t m Int -> S.SerialT m Int) -> t m (m Int) -> m ()
264 => (t m Int -> S.SerialT m Int) -> t m (m Int) -> m ()
238−scan = transform . S.scanl' (+) 0
266+scan n = composeN n $ S.scanl' (+) 0
239−fmap = transform . Prelude.fmap (+1)
267+fmap n = composeN n $ Prelude.fmap (+1)
240−map = transform . S.map (+1)
268+map n = composeN n $ S.map (+1)
241−mapM t = transform . t . S.mapM return
269+mapM t n = composeN' n $ t . S.mapM return
242−mapMaybe = transform . S.mapMaybe
270+mapMaybe n = composeN n $ S.mapMaybe
243− (\x -> if Prelude.odd x then Nothing else Just ())
271+ (\x -> if Prelude.odd x then Nothing else Just x)
244−mapMaybeM = transform . S.mapMaybeM
272+mapMaybeM n = composeN n $ S.mapMaybeM
245− (\x -> if Prelude.odd x then return Nothing else return $ Just ())
273+ (\x -> if Prelude.odd x then return Nothing else return $ Just x)
246sequence t = transform . t . S.sequence
274sequence t = transform . t . S.sequence
247−filterEven = transform . S.filter even
275+filterEven n = composeN n $ S.filter even
248−filterAllOut = transform . S.filter (> maxValue)
276+filterAllOut n = composeN n $ S.filter (> maxValue)
249−filterAllIn = transform . S.filter (<= maxValue)
277+filterAllIn n = composeN n $ S.filter (<= maxValue)
250−takeOne = transform . S.take 1
278+takeOne n = composeN n $ S.take 1
251−takeAll = transform . S.take maxValue
279+takeAll n = composeN n $ S.take maxValue
252−takeWhileTrue = transform . S.takeWhile (<= maxValue)
280+takeWhileTrue n = composeN n $ S.takeWhile (<= maxValue)
253−takeWhileMTrue = transform . S.takeWhileM (return . (<= maxValue))
281+takeWhileMTrue n = composeN n $ S.takeWhileM (return . (<= maxValue))
254−dropAll = transform . S.drop maxValue
282+dropAll n = composeN n $ S.drop maxValue
255−dropWhileTrue = transform . S.dropWhile (<= maxValue)
283+dropWhileTrue n = composeN n $ S.dropWhile (<= maxValue)
256−dropWhileMTrue = transform . S.dropWhileM (return . (<= maxValue))
284+dropWhileMTrue n = composeN n $ S.dropWhileM (return . (<= maxValue))
257−findIndices = transform . S.findIndices (== maxValue)
285+findIndices n = composeN n $ S.findIndices (== maxValue)
258−elemIndices = transform . S.elemIndices maxValue
286+elemIndices n = composeN n $ S.elemIndices maxValue
260-------------------------------------------------------------------------------
288-------------------------------------------------------------------------------
261-- Zipping and concat
289-- Zipping and concat
@@ -289,36 +317,30 @@ zipAsyncM src = do
289concat _n = return ()
317concat _n = return ()
291-------------------------------------------------------------------------------
319-------------------------------------------------------------------------------
292−-- Composition
320+-- Mixed Composition
293-------------------------------------------------------------------------------
321-------------------------------------------------------------------------------
295−{-# INLINE compose #-}
323+{-# INLINE scanMap #-}
296−compose :: Monad m => (Stream m Int -> Stream m Int) -> Stream m Int -> m ()
324+{-# INLINE dropMap #-}
297−compose f = transform . f . f . f . f
325+{-# INLINE dropScan #-}
298−
326+{-# INLINE takeDrop #-}
299−{-# INLINE composeMapM #-}
327+{-# INLINE takeScan #-}
300−{-# INLINE composeAllInFilters #-}
328+{-# INLINE takeMap #-}
301−{-# INLINE composeAllOutFilters #-}
329+{-# INLINE filterDrop #-}
302−{-# INLINE composeMapAllInFilter #-}
330+{-# INLINE filterTake #-}
303−composeAllInFilters, composeAllOutFilters,
331+{-# INLINE filterScan #-}
304− composeMapAllInFilter
332+{-# INLINE filterMap #-}
305− :: Monad m
333+scanMap, dropMap, dropScan, takeDrop, takeScan, takeMap, filterDrop,
334+ filterTake, filterScan, filterMap
306− => Stream m Int -> m ()
335+ :: Monad m => Int -> Stream m Int -> m ()
307−composeMapM :: S.MonadAsync m => Stream m Int -> m ()
336+
308−
337+scanMap n = composeN n $ S.map (subtract 1) . S.scanl' (+) 0
309−composeMapM = compose (S.mapM return)
338+dropMap n = composeN n $ S.map (subtract 1) . S.drop 1
310−composeAllInFilters = compose (S.filter (<= maxValue))
339+dropScan n = composeN n $ S.scanl' (+) 0 . S.drop 1
311−composeAllOutFilters = compose (S.filter (> maxValue))
340+takeDrop n = composeN n $ S.drop 1 . S.take maxValue
312−composeMapAllInFilter =
341+takeScan n = composeN n $ S.scanl' (+) 0 . S.take maxValue
313− compose (S.filter (<= maxValue) . Prelude.fmap (subtract 1))
342+takeMap n = composeN n $ S.map (subtract 1) . S.take maxValue
314−
343+filterDrop n = composeN n $ S.drop 1 . S.filter (<= maxValue)
315−{-# INLINABLE composeScaling #-}
344+filterTake n = composeN n $ S.take maxValue . S.filter (<= maxValue)
316−composeScaling :: Monad m => Int -> Stream m Int -> m ()
345+filterScan n = composeN n $ S.scanl' (+) 0 . S.filter (<= maxBound)
320− 2 -> transform . f . f
321− 3 -> transform . f . f . f
322− 4 -> transform . f . f . f . f
324− where f = S.filter (<= maxValue)
346+filterMap n = composeN n $ S.map (subtract 1) . S.filter (<= maxValue)