Skip to content

Commit 3d90cd5

Browse files
committed
Add lead/follow combinators
1 parent 93184c5 commit 3d90cd5

File tree

1 file changed

+15
-0
lines changed

1 file changed

+15
-0
lines changed

src/Reflex/Workflow.hs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ module Reflex.Workflow (
2424
, stop
2525
, replay
2626
, roundRobin
27+
, lead
28+
, follow
2729

2830
, breadthFirst
2931
, depthFirst
@@ -102,6 +104,12 @@ braidFree = curry $ \case
102104
(Pure a, b) -> fmap (a,) b
103105
(a, Pure b) -> fmap (,b) a
104106

107+
interleaveF :: Functor f => Bool -> f () -> F f a -> F f a
108+
interleaveF separatorAfter s = foldF $ \f ->
109+
if separatorAfter
110+
then liftF f <* liftF s
111+
else liftF s *> liftF f
112+
105113
append :: (Adjustable t m, MonadHold t m, PostBuild t m) => Event t (Step t m a) -> m (Event t a)
106114
append ev = do
107115
(h,t) <- headTailE ev
@@ -129,6 +137,9 @@ mkM = M . wrap . fmap pure . Compose
129137
braidM :: (Functor m, Reflex t) => M t m a -> M t m b -> M t m (a,b)
130138
braidM (M ma) (M mb) = M $ toF $ braidFree (fromF ma) (fromF mb)
131139

140+
interleaveM :: (Functor m, Reflex t) => Bool -> m (Event t ()) -> M t m a -> M t m a
141+
interleaveM separatorAfter s = M . interleaveF separatorAfter (Compose s) . unM
142+
132143
bottomUp
133144
:: forall t m a. PostBuild t m
134145
=> (forall x. Step t m (Step t m x) -> Step t m x)
@@ -208,6 +219,10 @@ replay = callCC $ pure . fix
208219
roundRobin :: (Functor m, Reflex t) => Machine t m a -> Machine t m b -> Machine t m (a,b)
209220
roundRobin a b = Machine $ lift $ braidM (runMachine id a) (runMachine id b)
210221

222+
lead, follow :: (Functor m, Reflex t) => m (Event t ()) -> Machine t m a -> Machine t m a
223+
lead s m = Machine $ lift $ interleaveM False s $ runMachine id m
224+
follow s m = Machine $ lift $ interleaveM True s $ runMachine id m
225+
211226
--------------------------------------------------------------------------------
212227
-- Runners
213228
--------------------------------------------------------------------------------

0 commit comments

Comments
 (0)