on master help sign in
Implement "finally" using "bracket"
f8f323aHarendra Kumar
src/Streamly/Streams/StreamD.hs+4−3
@@ -2292,7 +2292,7 @@ after action (Stream step state) = Stream step' state
2292-- Error constructor in step.
2292-- Error constructor in step.
2293--
2293--
2294-- | Run a side effect whenever the stream aborts due to an exception. The
2294-- | Run a side effect whenever the stream aborts due to an exception. The
2295−-- exception is not caught simply rethrown.
2295+-- exception is not caught, simply rethrown.
2296{-# INLINE_NORMAL onException #-}
2296{-# INLINE_NORMAL onException #-}
2297onException :: MonadCatch m => m b -> Stream m a -> Stream m a
2297onException :: MonadCatch m => m b -> Stream m a -> Stream m a
2298onException action (Stream step state) = Stream step' state
2298onException action (Stream step state) = Stream step' state
@@ -2311,14 +2311,15 @@ onException action (Stream step state) = Stream step' state
2311-- exception.
2311-- exception.
2312{-# INLINE finally #-}
2312{-# INLINE finally #-}
2313finally :: MonadCatch m => m b -> Stream m a -> Stream m a
2313finally :: MonadCatch m => m b -> Stream m a -> Stream m a
2314−finally action xs = after action $ onException action xs
2314+-- finally action xs = after action $ onException action xs
2315+finally action xs = bracket (return ()) (\_ -> action) (const xs)
2315
2316
2316-- XXX bracket is like concatMap, it generates a stream and then flattens it.
2317-- XXX bracket is like concatMap, it generates a stream and then flattens it.
2317-- Like concatMap it has 10x worse performance compared to linear fused
2318-- Like concatMap it has 10x worse performance compared to linear fused
2318-- compositions.
2319-- compositions.
2319--
2320--
2320-- | Run the first action before the stream starts and remember its output,
2321-- | Run the first action before the stream starts and remember its output,
2321−-- generate a stream using the output, run the second action using the
2322+-- generate a stream using the output, run the second action providing the
2322-- remembered value as an argument whenever the stream ends normally or due to
2323-- remembered value as an argument whenever the stream ends normally or due to
2323-- an exception.
2324-- an exception.
2324{-# INLINE_NORMAL bracket #-}
2325{-# INLINE_NORMAL bracket #-}