Skip to content

Commit 6f3f313

Browse files
authored
Merge pull request #72 from haskell-streaming/42-wrappEffect
add wrapEffect
2 parents 6851742 + ed6b3e0 commit 6f3f313

2 files changed

Lines changed: 17 additions & 2 deletions

File tree

changelog.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@
1919

2020
Generalise type signature of `toList_`.
2121

22+
Add `wrapEffect`.
23+
2224
- 0.2.1.0
2325

2426
Adding `Semigroup` instances for GHC 8.4.

src/Streaming/Prelude.hs

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -138,8 +138,8 @@ module Streaming.Prelude (
138138
, read
139139
, show
140140
, cons
141-
, slidingWindow
142-
141+
, slidingWindow
142+
, wrapEffect
143143

144144
-- * Splitting and inspecting streams of elements
145145
, next
@@ -770,6 +770,19 @@ effects = loop where
770770
Step (_ :> rest) -> loop rest
771771
{-# INLINABLE effects #-}
772772

773+
{-| Before evaluating the monadic action returning the next step in the 'Stream', @wrapEffect@
774+
extracts the value in a monadic computation @m a@ and passes it to a computation @a -> m y@.
775+
776+
-}
777+
wrapEffect :: (Monad m, Functor f) => m a -> (a -> m y) -> Stream f m r -> Stream f m r
778+
wrapEffect m f = loop where
779+
loop stream = do
780+
x <- lift m
781+
step <- lift $ inspect stream
782+
_ <- lift $ f x
783+
either pure loop' step
784+
loop' stream = wrap (fmap loop stream)
785+
773786
{-| Exhaust a stream remembering only whether @a@ was an element.
774787
775788
-}

0 commit comments

Comments
 (0)