Making the controllers more linear
Basically, the idea of `(oneBranch <> secondBranch)` instead of ``` do x <- foo case x of ... ```
This commit is contained in:
1 parent
617bd255ff
commit
15e50322c4
4 files changed
+51
-17
No files matched your search
+27
-3
@@ -1,4 +1,6 @@
|
||||
{-# LANGUAGE LambdaCase #-}
|
||||
{-# LANGUAGE Arrows #-}
|
||||
{-# LANGUAGE TypeOperators #-}
|
||||
|
||||
module AFRP
|
||||
( Mealy(..)
|
||||
@@ -18,8 +20,10 @@ module AFRP
|
||||
, changes
|
||||
, whenA
|
||||
, filterA
|
||||
, filterEvent
|
||||
, thenA
|
||||
, (>>|)
|
||||
, (>>||)
|
||||
, toEvent
|
||||
, lMerge
|
||||
, Pair(..)
|
||||
@@ -271,7 +275,7 @@ data Mealy eff a b = Mealy
|
||||
|
||||
-- Provide a name
|
||||
named :: Text -> Mealy eff a b -> Mealy eff a b
|
||||
named n f = f{runDiagramC = \run -> run (Step n (emptyDiagram run))}
|
||||
named n f = f{runDiagramC = \run -> run (Step n (runDiagramC f run))}
|
||||
|
||||
instance Semigroup b => Semigroup (Mealy eff a b) where
|
||||
Mealy ast ad af <> Mealy bst bd bf = Mealy (ast <> bst) (combineDiagram Merge ad bd) $ \nt -> af nt <> bf nt
|
||||
@@ -290,15 +294,19 @@ eff f = Mealy mempty emptyDiagram $ \nt -> do
|
||||
instance Category (Mealy eff) where
|
||||
{- HLINT ignore "Use const" -}
|
||||
id = Mealy mempty emptyDiagram $ \_nt -> id
|
||||
(Mealy ast ad f) . (Mealy bst bd g) = Mealy (ast <> bst) (combineDiagram Then ad bd) $ \nt -> do
|
||||
(Mealy ast ad f) . (Mealy bst bd g) = Mealy (ast <> bst) (combineDiagram Then bd ad) $ \nt -> do
|
||||
f nt . g nt
|
||||
|
||||
instance Arrow (Mealy eff) where
|
||||
arr f = Mealy mempty emptyDiagram $ \_nt -> arr f
|
||||
first (Mealy st d f) = Mealy st d $ \nt -> first (f nt)
|
||||
(Mealy ast ad f) &&& (Mealy bst bd g) = Mealy (ast <> bst) (combineDiagram Both ad bd) $ \nt -> do
|
||||
f nt &&& g nt
|
||||
|
||||
instance ArrowChoice (Mealy eff) where
|
||||
left (Mealy st d f) = Mealy st d $ \nt -> left (f nt)
|
||||
(Mealy ast ad f) +++ (Mealy bst bd g) = Mealy (ast <> bst) (combineDiagram Branch ad bd) $ \nt -> do
|
||||
f nt +++ g nt
|
||||
|
||||
|
||||
instance Functor (Mealy eff a) where
|
||||
@@ -486,13 +494,29 @@ whenA predicate auto = arr (\a -> if predicate a then Left a else Right ()) >>>
|
||||
filterA :: (a -> Bool) -> Mealy eff a (Either () a)
|
||||
filterA f = arr $ \a -> bool (Left ()) (Right a) (f a)
|
||||
|
||||
-- filters events through if they match the predicate
|
||||
filterEvent :: (ArrowChoice cat, Arrow cat) => (a -> Bool) -> Event a `cat` Event a
|
||||
filterEvent predicate = proc ev -> do
|
||||
case ev of
|
||||
Event a | predicate a -> id -< ev
|
||||
_ -> arr (const Tick) -< ()
|
||||
|
||||
thenA :: (ArrowChoice cat, Arrow cat) => cat a (Either b1 c) -> cat c (Either b1 b2) -> cat a (Either b1 b2)
|
||||
thenA f g = f >>> arr Left ||| g
|
||||
|
||||
(>>|) :: (ArrowChoice cat, Arrow cat) => cat a (Either b1 c) -> cat c (Either b1 b2) -> cat a (Either b1 b2)
|
||||
(>>|) = thenA
|
||||
|
||||
infixl 2 >>|
|
||||
infixl 2 >>||
|
||||
|
||||
(>>||) :: (ArrowChoice cat, Arrow cat) => cat a (Event b) -> cat b c -> cat a (Event c)
|
||||
f >>|| g = proc x -> do
|
||||
e <- f -< x
|
||||
case e of
|
||||
Event e' -> g >>> arr Event -< e'
|
||||
Tick -> arr (const Tick) -< ()
|
||||
|
||||
infixr 3 >>|
|
||||
|
||||
toEvent :: Mealy eff (Either () a) (Event a)
|
||||
toEvent = arr (either (const Tick) Event)
|
||||
|
||||
Reference in new issue
Block a user