Skip to content

Commit 2dc730a

Browse files
committed
Add substFree
1 parent de660a5 commit 2dc730a

File tree

1 file changed

+11
-0
lines changed

1 file changed

+11
-0
lines changed

src/Control/Monad/Free.purs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ module Control.Monad.Free
66
, hoistFree
77
, injF
88
, foldFree
9+
, substFree
910
, runFree
1011
, runFreeM
1112
, resume
@@ -112,6 +113,16 @@ foldFree k = tailRecM go
112113
Return a -> Right <$> pure a
113114
Bind g i -> (Left <<< i) <$> k g
114115

116+
-- | Like `foldFree`, but for folding into some other Free monad without the
117+
-- | overhead that `MonadRec` incurs.
118+
substFree :: forall f g. (f ~> Free g) -> Free f ~> Free g
119+
substFree k = go
120+
where
121+
go :: Free f ~> Free g
122+
go f = case toView f of
123+
Return a -> pure a
124+
Bind g i -> k g >>= go <$> i
125+
115126
-- | Run a free monad with a function that unwraps a single layer of the functor
116127
-- | `f` at a time.
117128
runFree :: forall f a. Functor f => (f (Free f a) -> Free f a) -> Free f a -> a

0 commit comments

Comments
 (0)