@@ -14,7 +14,8 @@ import Control.Monad (when)
1414import Data.Maybe (fromJust)
1515import Prelude
1616 (Monad, Int, (+), ($), (.), return, fmap, even, (>), (<=), (==), (<=),
17− subtract, undefined, Maybe(..), odd, Bool, not, (>>=), mapM_, curry)
17+ subtract, undefined, Maybe(..), odd, Bool, not, (>>=), mapM_, curry,
18+ maxBound)
1819
1920import qualified Streamly as S
2021import qualified Streamly.Prelude as S
@@ -169,7 +170,10 @@ nullHeadTail s = do
169170 _ <- S.head s
170171 S.tail s >>= Prelude.mapM_ nullHeadTail
171172
173+{-# INLINE mapM_ #-}
174+mapM_ :: Monad m => Stream m Int -> m ()
172175mapM_ = S.mapM_ (\_ -> return ())
176+
173177toList = S.toList
174178foldr = S.foldr (:) []
175179foldr1 = S.foldr1 (+)
@@ -200,8 +204,32 @@ product = S.product
200204transform :: Monad m => Stream m a -> m ()
201205transform = runStream
202206
207+{-# INLINE composeN #-}
208+composeN
209+ :: Monad m
210+ => Int -> (Stream m Int -> Stream m Int) -> Stream m Int -> m ()
211+composeN n f =
212+ case n of
213+ 1 -> transform . f
214+ 2 -> transform . f . f
215+ 3 -> transform . f . f . f
216+ 4 -> transform . f . f . f . f
217+ _ -> undefined
218+
219+-- polymorphic stream version of composeN
220+{-# INLINE composeN' #-}
221+composeN'
222+ :: (S.IsStream t, Monad m)
223+ => Int -> (t m Int -> Stream m Int) -> t m Int -> m ()
224+composeN' n f =
225+ case n of
226+ 1 -> transform . f
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
230+ _ -> undefined
231+
203232{-# INLINE scan #-}
204−{-# INLINE mapM_ #-}
205233{-# INLINE map #-}
206234{-# INLINE fmap #-}
207235{-# INLINE mapMaybe #-}
@@ -217,45 +245,45 @@ transform = runStream
217245{-# INLINE dropWhileMTrue #-}
218246{-# INLINE findIndices #-}
219247{-# INLINE elemIndices #-}
220−scan, mapM_, map, fmap, mapMaybe, filterEven, filterAllOut,
248+scan, map, fmap, mapMaybe, filterEven, filterAllOut,
221249 filterAllIn, takeOne, takeAll, takeWhileTrue, takeWhileMTrue, dropAll,
222250 dropWhileTrue, dropWhileMTrue,
223251 findIndices, elemIndices
224252 :: Monad m
225− => Stream m Int -> m ()
253+ => Int -> Stream m Int -> m ()
226254
227255{-# INLINE mapMaybeM #-}
228−mapMaybeM :: S.MonadAsync m => Stream m Int -> m ()
256+mapMaybeM :: S.MonadAsync m => Int -> Stream m Int -> m ()
229257
230258{-# INLINE mapM #-}
231259mapM :: (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 ()
233261
234262{-# INLINE sequence #-}
235263sequence :: (S.IsStream t, S.MonadAsync m)
236264 => (t m Int -> S.SerialT m Int) -> t m (m Int) -> m ()
237265
238−scan = transform . S.scanl' (+) 0
239−fmap = transform . Prelude.fmap (+1)
240−map = transform . S.map (+1)
241−mapM t = transform . t . S.mapM return
242−mapMaybe = transform . S.mapMaybe
243− (\x -> if Prelude.odd x then Nothing else Just ())
244−mapMaybeM = transform . S.mapMaybeM
245− (\x -> if Prelude.odd x then return Nothing else return $ Just ())
266+scan n = composeN n $ S.scanl' (+) 0
267+fmap n = composeN n $ Prelude.fmap (+1)
268+map n = composeN n $ S.map (+1)
269+mapM t n = composeN' n $ t . S.mapM return
270+mapMaybe n = composeN n $ S.mapMaybe
271+ (\x -> if Prelude.odd x then Nothing else Just x)
272+mapMaybeM n = composeN n $ S.mapMaybeM
273+ (\x -> if Prelude.odd x then return Nothing else return $ Just x)
246274sequence t = transform . t . S.sequence
247−filterEven = transform . S.filter even
248−filterAllOut = transform . S.filter (> maxValue)
249−filterAllIn = transform . S.filter (<= maxValue)
250−takeOne = transform . S.take 1
251−takeAll = transform . S.take maxValue
252−takeWhileTrue = transform . S.takeWhile (<= maxValue)
253−takeWhileMTrue = transform . S.takeWhileM (return . (<= maxValue))
254−dropAll = transform . S.drop maxValue
255−dropWhileTrue = transform . S.dropWhile (<= maxValue)
256−dropWhileMTrue = transform . S.dropWhileM (return . (<= maxValue))
257−findIndices = transform . S.findIndices (== maxValue)
258−elemIndices = transform . S.elemIndices maxValue
275+filterEven n = composeN n $ S.filter even
276+filterAllOut n = composeN n $ S.filter (> maxValue)
277+filterAllIn n = composeN n $ S.filter (<= maxValue)
278+takeOne n = composeN n $ S.take 1
279+takeAll n = composeN n $ S.take maxValue
280+takeWhileTrue n = composeN n $ S.takeWhile (<= maxValue)
281+takeWhileMTrue n = composeN n $ S.takeWhileM (return . (<= maxValue))
282+dropAll n = composeN n $ S.drop maxValue
283+dropWhileTrue n = composeN n $ S.dropWhile (<= maxValue)
284+dropWhileMTrue n = composeN n $ S.dropWhileM (return . (<= maxValue))
285+findIndices n = composeN n $ S.findIndices (== maxValue)
286+elemIndices n = composeN n $ S.elemIndices maxValue
259287
260288-------------------------------------------------------------------------------
261289-- Zipping and concat
@@ -289,36 +317,30 @@ zipAsyncM src = do
289317concat _n = return ()
290318
291319-------------------------------------------------------------------------------
292−-- Composition
320+-- Mixed Composition
293321-------------------------------------------------------------------------------
294322
295−{-# INLINE compose #-}
296−compose :: Monad m => (Stream m Int -> Stream m Int) -> Stream m Int -> m ()
297−compose f = transform . f . f . f . f
298−
299−{-# INLINE composeMapM #-}
300−{-# INLINE composeAllInFilters #-}
301−{-# INLINE composeAllOutFilters #-}
302−{-# INLINE composeMapAllInFilter #-}
303−composeAllInFilters, composeAllOutFilters,
304− composeMapAllInFilter
305− :: Monad m
306− => Stream m Int -> m ()
307−composeMapM :: S.MonadAsync m => Stream m Int -> m ()
308−
309−composeMapM = compose (S.mapM return)
310−composeAllInFilters = compose (S.filter (<= maxValue))
311−composeAllOutFilters = compose (S.filter (> maxValue))
312−composeMapAllInFilter =
313− compose (S.filter (<= maxValue) . Prelude.fmap (subtract 1))
314−
315−{-# INLINABLE composeScaling #-}
316−composeScaling :: Monad m => Int -> Stream m Int -> m ()
317−composeScaling m =
318− case m of
319− 1 -> transform . f
320− 2 -> transform . f . f
321− 3 -> transform . f . f . f
322− 4 -> transform . f . f . f . f
323− _ -> undefined
324− where f = S.filter (<= maxValue)
323+{-# INLINE scanMap #-}
324+{-# INLINE dropMap #-}
325+{-# INLINE dropScan #-}
326+{-# INLINE takeDrop #-}
327+{-# INLINE takeScan #-}
328+{-# INLINE takeMap #-}
329+{-# INLINE filterDrop #-}
330+{-# INLINE filterTake #-}
331+{-# INLINE filterScan #-}
332+{-# INLINE filterMap #-}
333+scanMap, dropMap, dropScan, takeDrop, takeScan, takeMap, filterDrop,
334+ filterTake, filterScan, filterMap
335+ :: Monad m => Int -> Stream m Int -> m ()
336+
337+scanMap n = composeN n $ S.map (subtract 1) . S.scanl' (+) 0
338+dropMap n = composeN n $ S.map (subtract 1) . S.drop 1
339+dropScan n = composeN n $ S.scanl' (+) 0 . S.drop 1
340+takeDrop n = composeN n $ S.drop 1 . S.take maxValue
341+takeScan n = composeN n $ S.scanl' (+) 0 . S.take maxValue
342+takeMap n = composeN n $ S.map (subtract 1) . S.take maxValue
343+filterDrop n = composeN n $ S.drop 1 . S.filter (<= maxValue)
344+filterTake n = composeN n $ S.take maxValue . S.filter (<= maxValue)
345+filterScan n = composeN n $ S.scanl' (+) 0 . S.filter (<= maxBound)
346+filterMap n = composeN n $ S.map (subtract 1) . S.filter (<= maxValue)