on master help sign in
expose lcatMaybes via the Streamly.Internal module
commita3ee4a852d11a6afb9a76ae8e6fba8962fdcb531 authorHarendra Kumar <harendra.kumar@gmail.com> dateWed 7 Aug 2019, 01:32 (7 years ago) parent27d7ea8
3 files changed+12−8
src/Streamly/Fold/Types.hs+8 src/Streamly/Internal.hs+3−1 src/Streamly/Prelude.hs+1−7
src/Streamly/Fold/Types.hs+8
@@ -17,6 +17,7 @@ module Streamly.Fold.Types
1717 , lmapM
1818 , lfilter
1919 , lfilterM
20+ , lcatMaybes
2021 , ltake
2122 , ltakeWhile
2223 , lsessionsOf
@@ -33,6 +34,7 @@ import Control.Monad (void)
3334import Control.Monad.Catch (throwM)
3435import Control.Monad.IO.Class (MonadIO(..))
3536import Control.Monad.Trans.Control (control)
37+import Data.Maybe (isJust, fromJust)
3638import Data.Semigroup (Semigroup(..))
3739import Streamly.Strict (Tuple'(..), Tuple3'(..), Either'(..))
3840import Streamly.SVar (MonadAsync)
@@ -241,6 +243,12 @@ lfilterM f (Fold step begin done) = Fold step' begin done
241243 use <- f a
242244 if use then step x a else return x
243245
246+-- | Transform a fold from a pure input to a 'Maybe' input, consuming only
247+-- 'Just' values.
248+{-# INLINE lcatMaybes #-}
249+lcatMaybes :: Monad m => Fold m a b -> Fold m (Maybe a) b
250+lcatMaybes = lfilter isJust . lmap fromJust
251+
244252-- | Take first 'n' elements from the stream and discard the rest.
245253--
246254-- @since 0.7.0
src/Streamly/Internal.hs+3−1
@@ -28,6 +28,7 @@ module Streamly.Internal
2828 , lmapM
2929 , lfilter
3030 , lfilterM
31+ , lcatMaybes
3132 , ltake
3233 , ltakeWhile
3334 , lsessionsOf
@@ -36,6 +37,7 @@ module Streamly.Internal
3637 )
3738where
3839
39−import Streamly.Fold.Types
4040import Streamly.Streams.Combinators (inspectMode)
4141import Streamly.Streams.Parallel (tapAsync)
42+
43+import Streamly.Fold.Types
src/Streamly/Prelude.hs+1−7
@@ -2674,12 +2674,6 @@ chunksOf
26742674 => Int -> Fold m a b -> t m a -> t m b
26752675chunksOf n f m = D.fromStreamD $ D.groupsOf n f (D.toStreamD m)
26762676
2677−-- | Transform a fold from a pure input to a 'Maybe' input, consuming only
2678−-- 'Just' values.
2679−{-# INLINE lcatMaybes #-}
2680−lcatMaybes :: Monad m => Fold m a b -> Fold m (Maybe a) b
2681−lcatMaybes = FL.lfilter isJust . FL.lmap fromJust
2682−
26832677-- XXX we can implement this by repeatedly applying the 'lrunFor' fold.
26842678-- XXX add this example after fixing the serial stream rate control
26852679-- >>> S.toList $ S.take 5 $ sessionsOf 1 FL.sum $ constRate 2 $ S.enumerateFrom 1
@@ -2694,7 +2688,7 @@ sessionsOf
26942688 :: (IsStream t, MonadAsync m)
26952689 => Double -> Fold m a b -> t m a -> t m b
26962690sessionsOf n f xs =
2697− splitBySuffix isNothing (lcatMaybes f)
2691+ splitBySuffix isNothing (FL.lcatMaybes f)
26982692 (intersperseByTime n (return Nothing) (Serial.map Just xs))
26992693
27002694------------------------------------------------------------------------------