@@ -46,22 +46,15 @@ import Prelude hiding (concat)
4646
4747data Unfold m a b =
4848 -- | @Unfold step inject extract@
49− forall s. Unfold (s -> m (Step s b)) (a -> m s) (s -> m (Maybe a))
49+ forall s. Unfold (s -> m (Step s b)) (a -> m s) (s -> m a)
5050
5151------------------------------------------------------------------------------
5252-- Unfolds
5353------------------------------------------------------------------------------
5454
55−-- XXX we do not need a Maybe, we can just use undefined or error. There is no
56−-- state to extract in this case, and it should not be used.
57−--
58−-- However, in certain cases we may not be able to extract, e.g. if we are
59−-- unfolding a Map, we may not be able to resume reading a Map. In such cases
60−-- it may be useful to return a Nothing. Or maybe we do not allow unfolding
61−-- such structures?
6255{-# INLINE nilM #-}
6356nilM :: Monad m => (a -> m c) -> Unfold m a b
64−nilM f = Unfold step return (\_ -> return Nothing)
57+nilM f = Unfold step return return
6558
6659 where
6760
@@ -74,26 +67,26 @@ nil = nilM (\_ -> return ())
7467
7568{-# INLINE unfoldrM #-}
7669unfoldrM :: Monad m => (a -> m (Maybe (b, a))) -> Unfold m a b
77−unfoldrM next = Unfold step return (return . Just)
70+unfoldrM next = Unfold step return return
7871 where
7972 {-# INLINE_LATE step #-}
8073 step st = do
8174 r <- next st
8275 return $ case r of
8376 Just (x, s) -> Yield x s
84− Nothing -> Stop
77+ Nothing -> Stop
8578
8679-- | Convert a list of pure values to a 'Stream'
8780--
8881-- /Internal/
8982{-# INLINE_LATE fromList #-}
9083fromList :: Monad m => Unfold m [a] a
91−fromList = Unfold step inject (return . Just)
84+fromList = Unfold step inject return
9285 where
9386 inject = return
9487 {-# INLINE_LATE step #-}
9588 step (x:xs) = return $ Yield x xs
96− step [] = return Stop
89+ step [] = return Stop
9790
9891------------------------------------------------------------------------------
9992-- Mapping
@@ -106,7 +99,7 @@ fromList = Unfold step inject (return . Just)
10699{-# INLINE_NORMAL lmap #-}
107100lmap :: Functor m => (a -> c) -> (c -> a) -> Unfold m c b -> Unfold m a b
108101lmap f g (Unfold ustep uinject uextract) =
109− Unfold ustep (uinject . f) (fmap (fmap g) . uextract)
102+ Unfold ustep (uinject . f) (fmap g . uextract)
110103
111104------------------------------------------------------------------------------
112105-- Nesting
@@ -115,6 +108,8 @@ lmap f g (Unfold ustep uinject uextract) =
115108{-# ANN type ConcatState Fuse #-}
116109data ConcatState s1 s2 = ConcatOuter s1 | ConcatInner s1 s2
117110
111+-- XXX Use Either a (a,b) for extracted state.
112+--
118113-- | Apply the second unfold to each output element of the first unfold and
119114-- flatten the output in a single stream.
120115--
@@ -152,17 +147,8 @@ concat (Unfold step1 inject1 extract1) (Unfold step2 inject2 extract2) =
152147 Skip s -> Skip (ConcatInner ost s)
153148 Stop -> Skip (ConcatOuter ost)
154149
155− extract (ConcatOuter s1) = do
156− r <- extract1 s1
157− return $ case r of
158− Nothing -> Nothing
159− Just a -> Just (a, Nothing)
150+ extract (ConcatOuter s1) = (,Nothing) <$> extract1 s1
160151 extract (ConcatInner s1 s2) = do
161152 r1 <- extract1 s1
162− case r1 of
163− Nothing -> return Nothing
164− Just a -> do
165− r2 <- extract2 s2
166− return $ case r2 of
167− Nothing -> Just (a, Nothing)
168− Just b -> Just (a, Just b)
153+ r2 <- extract2 s2
154+ return (r1, Just r2)