@@ -61,9 +61,9 @@ module Streamly.Internal.Data.Unfold
62 -- * Operations on Input
62 -- * Operations on Input
65− , first
65+ , supplyFirst
66− , second
66+ , supplySecond
67 , discardFirst
67 , discardFirst
68 , discardSecond
68 , discardSecond
@@ -138,6 +138,9 @@ import qualified Streamly.Streams.StreamD as D
138-- Input operations
138-- Input operations
139-------------------------------------------------------------------------------
139-------------------------------------------------------------------------------
141+-- | Map a function on the input argument of the 'Unfold'.
141{-# INLINE_NORMAL lmap #-}
144{-# INLINE_NORMAL lmap #-}
142lmap :: (a -> c) -> Unfold m c b -> Unfold m a b
145lmap :: (a -> c) -> Unfold m c b -> Unfold m a b
143lmap f (Unfold ustep uinject) = Unfold ustep (uinject . f)
146lmap f (Unfold ustep uinject) = Unfold ustep (uinject . f)
@@ -146,21 +149,19 @@ lmap f (Unfold ustep uinject) = Unfold ustep (uinject . f)
147-- /Internal/
150-- /Internal/
149−{-# INLINE_NORMAL close #-}
152+{-# INLINE_NORMAL supply #-}
150−close :: Unfold m a b -> a -> Unfold m Void b
153+supply :: Unfold m a b -> a -> Unfold m Void b
151−close unf a = lmap (const a) unf
154+supply unf a = lmap (const a) unf
153−-- XXX rename to closeFst/closeSnd
155-- | Supply the first component of the tuple to an unfold that accepts a tuple
156-- | Supply the first component of the tuple to an unfold that accepts a tuple
156-- as a seed resulting in a fold that accepts the second component of the tuple
157-- as a seed resulting in a fold that accepts the second component of the tuple
157-- as a seed.
158-- as a seed.
159-- /Internal/
160-- /Internal/
161−{-# INLINE_NORMAL first #-}
162+{-# INLINE_NORMAL supplyFirst #-}
162−first :: Unfold m (a, b) c -> a -> Unfold m b c
163+supplyFirst :: Unfold m (a, b) c -> a -> Unfold m b c
163−first unf a = lmap (a, ) unf
164+supplyFirst unf a = lmap (a, ) unf
165-- | Supply the second component of the tuple to an unfold that accepts a tuple
166-- | Supply the second component of the tuple to an unfold that accepts a tuple
166-- as a seed resulting in a fold that accepts the first component of the tuple
167-- as a seed resulting in a fold that accepts the first component of the tuple
@@ -168,18 +169,35 @@ first unf a = lmap (a, ) unf
169-- /Internal/
170-- /Internal/
171−{-# INLINE_NORMAL second #-}
172+{-# INLINE_NORMAL supplySecond #-}
172−second :: Unfold m (a, b) c -> b -> Unfold m a c
173+supplySecond :: Unfold m (a, b) c -> b -> Unfold m a c
173−second unf b = lmap (, b) unf
174+supplySecond unf b = lmap (, b) unf
176+-- | Convert an 'Unfold' into an unfold accepting a tuple as an argument,
177+-- using the argument of the original fold as the second element of tuple and
178+-- discarding the first element of the tuple.
175{-# INLINE_NORMAL discardFirst #-}
182{-# INLINE_NORMAL discardFirst #-}
176discardFirst :: Unfold m a b -> Unfold m (c, a) b
183discardFirst :: Unfold m a b -> Unfold m (c, a) b
177discardFirst = lmap snd
184discardFirst = lmap snd
186+-- | Convert an 'Unfold' into an unfold accepting a tuple as an argument,
187+-- using the argument of the original fold as the first element of tuple and
188+-- discarding the second element of the tuple.
179{-# INLINE_NORMAL discardSecond #-}
192{-# INLINE_NORMAL discardSecond #-}
180discardSecond :: Unfold m a b -> Unfold m (a, c) b
193discardSecond :: Unfold m a b -> Unfold m (a, c) b
181discardSecond = lmap fst
194discardSecond = lmap fst
196+-- | Convert an 'Unfold' that accepts a tuple as an argument into an unfold
197+-- that accepts a tuple with elements swapped.
183{-# INLINE_NORMAL swap #-}
201{-# INLINE_NORMAL swap #-}
184swap :: Unfold m (a, c) b -> Unfold m (c, a) b
202swap :: Unfold m (a, c) b -> Unfold m (c, a) b
185swap = lmap Tuple.swap
203swap = lmap Tuple.swap
@@ -433,7 +451,7 @@ enumerateFromStepIntegral = Unfold step inject
433{-# INLINE enumerateFromToIntegral #-}
451{-# INLINE enumerateFromToIntegral #-}
434enumerateFromToIntegral :: (Monad m, Integral a) => a -> Unfold m a a
452enumerateFromToIntegral :: (Monad m, Integral a) => a -> Unfold m a a
435enumerateFromToIntegral to =
453enumerateFromToIntegral to =
436− takeWhile (<= to) $ second enumerateFromStepIntegral 1
454+ takeWhile (<= to) $ supplySecond enumerateFromStepIntegral 1
438{-# INLINE enumerateFromIntegral #-}
456{-# INLINE enumerateFromIntegral #-}
439enumerateFromIntegral :: (Monad m, Integral a, Bounded a) => Unfold m a a
457enumerateFromIntegral :: (Monad m, Integral a, Bounded a) => Unfold m a a
@@ -719,7 +737,7 @@ bracket bef aft (Unfold step1 inject1) = Unfold step inject
719 Stop -> aft v >> return Stop
737 Stop -> aft v >> return Stop
721-- | When unfolding if an exception occurs, unfold the exception using the
739-- | When unfolding if an exception occurs, unfold the exception using the
722−-- exception unfold.
740+-- exception unfold supplied as the first argument to 'handle'.
724-- /Internal/
742-- /Internal/
725{-# INLINE_NORMAL handle #-}
743{-# INLINE_NORMAL handle #-}