Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions core/src/Streamly/Internal/Data/Unfold/Type.hs
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ module Streamly.Internal.Data.Unfold.Type
, unfoldr
, functionM
, function
, functionMaybeM
, identity

-- * From Values
Expand Down Expand Up @@ -886,6 +887,26 @@ functionM f = Unfold step inject
function :: Applicative m => (a -> b) -> Unfold m a b
function f = functionM $ pure Prelude.. f

-- | Lift a monadic Maybe returning function into an unfold. The unfold
-- generates a singleton stream.
--
{-# INLINE functionMaybeM #-}
functionMaybeM :: Monad m => (a -> m (Maybe b)) -> Unfold m a b
functionMaybeM f = Unfold step inject

where

inject a = return (Just a)

{-# INLINE_LATE step #-}
step (Just a) = do
result <- f a
case result of
Just b -> pure $ Yield b Nothing
Nothing -> pure Stop

step Nothing = pure Stop

-- | Identity unfold. The unfold generates a singleton stream having the input
-- as the only element.
--
Expand Down
Loading