{-# LANGUAGE Rank2Types #-}
{-# LANGUAGE RecursiveDo #-}
{-# LANGUAGE MultiParamTypeClasses #-}
module Reactive.Banana.Combinators (
Event, Behavior,
interpret,
module Control.Applicative,
module Data.Semigroup,
never, unionWith, filterE,
apply,
Moment, MonadMoment(..),
accumE, stepper,
valueB, valueBLater, observeE, switchE, switchB,
(<@>), (<@), (@>),
filterJust, filterApply, whenE, split, once,
unions, accumB, mapAccum,
merge, mergeWith
) where
import Control.Applicative
import Data.Semigroup
import Data.These (These(..))
import qualified Reactive.Banana.Prim.High.Combinators as Prim
import Reactive.Banana.Types
interpret :: (Event a -> Moment (Event b)) -> [Maybe a] -> IO [Maybe b]
interpret :: forall a b.
(Event a -> Moment (Event b)) -> [Maybe a] -> IO [Maybe b]
interpret Event a -> Moment (Event b)
f [Maybe a]
xs = forall a b.
(Event a -> Moment (Event b)) -> [Maybe a] -> IO [Maybe b]
Prim.interpret (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Event a -> Event a
unE forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Moment a -> Moment a
unM forall b c a. (b -> c) -> (a -> b) -> a -> c
. Event a -> Moment (Event b)
f forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Event a -> Event a
E) [Maybe a]
xs
never :: Event a
never :: forall a. Event a
never = forall a. Event a -> Event a
E forall a. Event a
Prim.never
unionWith :: (a -> a -> a) -> Event a -> Event a -> Event a
unionWith :: forall a. (a -> a -> a) -> Event a -> Event a -> Event a
unionWith a -> a -> a
f = forall a c b.
(a -> c)
-> (b -> c) -> (a -> b -> c) -> Event a -> Event b -> Event c
mergeWith forall a. a -> a
id forall a. a -> a
id a -> a -> a
f
merge :: Event a -> Event b -> Event (These a b)
merge :: forall a b. Event a -> Event b -> Event (These a b)
merge = forall a c b.
(a -> c)
-> (b -> c) -> (a -> b -> c) -> Event a -> Event b -> Event c
mergeWith forall a b. a -> These a b
This forall a b. b -> These a b
That forall a b. a -> b -> These a b
These
mergeWith
:: (a -> c)
-> (b -> c)
-> (a -> b -> c)
-> Event a
-> Event b
-> Event c
mergeWith :: forall a c b.
(a -> c)
-> (b -> c) -> (a -> b -> c) -> Event a -> Event b -> Event c
mergeWith a -> c
f b -> c
g a -> b -> c
h Event a
e1 Event b
e2 = forall a. Event a -> Event a
E forall a b. (a -> b) -> a -> b
$ forall a c b.
(a -> c)
-> (b -> c) -> (a -> b -> c) -> Event a -> Event b -> Event c
Prim.mergeWith a -> c
f b -> c
g a -> b -> c
h (forall a. Event a -> Event a
unE Event a
e1) (forall a. Event a -> Event a
unE Event b
e2)
filterJust :: Event (Maybe a) -> Event a
filterJust :: forall a. Event (Maybe a) -> Event a
filterJust = forall a. Event a -> Event a
E forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Event (Maybe a) -> Event a
Prim.filterJust forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Event a -> Event a
unE
filterE :: (a -> Bool) -> Event a -> Event a
filterE :: forall a. (a -> Bool) -> Event a -> Event a
filterE a -> Bool
p = forall a. Event (Maybe a) -> Event a
filterJust forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap (\a
x -> if a -> Bool
p a
x then forall a. a -> Maybe a
Just a
x else forall a. Maybe a
Nothing)
apply :: Behavior (a -> b) -> Event a -> Event b
apply :: forall a b. Behavior (a -> b) -> Event a -> Event b
apply Behavior (a -> b)
bf Event a
ex = forall a. Event a -> Event a
E forall a b. (a -> b) -> a -> b
$ forall a b. Behavior (a -> b) -> Event a -> Event b
Prim.applyE (forall a. Behavior a -> Behavior a
unB Behavior (a -> b)
bf) (forall a. Event a -> Event a
unE Event a
ex)
stepper :: MonadMoment m => a -> Event a -> m (Behavior a)
stepper :: forall (m :: * -> *) a.
MonadMoment m =>
a -> Event a -> m (Behavior a)
stepper a
a = forall (m :: * -> *) a. MonadMoment m => Moment a -> m a
liftMoment forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Moment a -> Moment a
M forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Behavior a -> Behavior a
B forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> Event a -> Moment (Behavior a)
Prim.stepperB a
a forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Event a -> Event a
unE
accumE :: MonadMoment m => a -> Event (a -> a) -> m (Event a)
accumE :: forall (m :: * -> *) a.
MonadMoment m =>
a -> Event (a -> a) -> m (Event a)
accumE a
acc = forall (m :: * -> *) a. MonadMoment m => Moment a -> m a
liftMoment forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Moment a -> Moment a
M forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Event a -> Event a
E forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. a -> Event (a -> a) -> Moment (Event a)
Prim.accumE a
acc forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Event a -> Event a
unE
valueB :: MonadMoment m => Behavior a -> m a
valueB :: forall (m :: * -> *) a. MonadMoment m => Behavior a -> m a
valueB = forall (m :: * -> *) a. MonadMoment m => Moment a -> m a
liftMoment forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Moment a -> Moment a
M forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Behavior a -> Moment a
Prim.valueB forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Behavior a -> Behavior a
unB
valueBLater :: MonadMoment m => Behavior a -> m a
valueBLater :: forall (m :: * -> *) a. MonadMoment m => Behavior a -> m a
valueBLater = forall (m :: * -> *) a. MonadMoment m => Moment a -> m a
liftMoment forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Moment a -> Moment a
M forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Behavior a -> Moment a
Prim.initialBLater forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Behavior a -> Behavior a
unB
observeE :: Event (Moment a) -> Event a
observeE :: forall a. Event (Moment a) -> Event a
observeE = forall a. Event a -> Event a
E forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Event (Moment a) -> Event a
Prim.observeE forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> Event a -> Event b
Prim.mapE forall a. Moment a -> Moment a
unM forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Event a -> Event a
unE
switchE :: MonadMoment m => Event a -> Event (Event a) -> m (Event a)
switchE :: forall (m :: * -> *) a.
MonadMoment m =>
Event a -> Event (Event a) -> m (Event a)
switchE Event a
e Event (Event a)
ee = forall (m :: * -> *) a. MonadMoment m => Moment a -> m a
liftMoment (forall a. Moment a -> Moment a
M (forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Event a -> Event a
E (forall a. Event a -> Event (Event a) -> Moment (Event a)
Prim.switchE (forall a. Event a -> Event a
unE Event a
e) (forall a b. (a -> b) -> Event a -> Event b
Prim.mapE forall a. Event a -> Event a
unE (forall a. Event a -> Event a
unE Event (Event a)
ee)))))
switchB :: MonadMoment m => Behavior a -> Event (Behavior a) -> m (Behavior a)
switchB :: forall (m :: * -> *) a.
MonadMoment m =>
Behavior a -> Event (Behavior a) -> m (Behavior a)
switchB Behavior a
b = forall (m :: * -> *) a. MonadMoment m => Moment a -> m a
liftMoment forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Moment a -> Moment a
M forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a. Behavior a -> Behavior a
B forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Behavior a -> Event (Behavior a) -> Moment (Behavior a)
Prim.switchB (forall a. Behavior a -> Behavior a
unB Behavior a
b) forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. (a -> b) -> Event a -> Event b
Prim.mapE forall a. Behavior a -> Behavior a
unB forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. Event a -> Event a
unE
infixl 4 <@>, <@, @>
(<@>) :: Behavior (a -> b) -> Event a -> Event b
<@> :: forall a b. Behavior (a -> b) -> Event a -> Event b
(<@>) = forall a b. Behavior (a -> b) -> Event a -> Event b
apply
(<@) :: Behavior b -> Event a -> Event b
Behavior b
f <@ :: forall b a. Behavior b -> Event a -> Event b
<@ Event a
g = (forall a b. a -> b -> a
const forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Behavior b
f) forall a b. Behavior (a -> b) -> Event a -> Event b
<@> Event a
g
(@>) :: Event a -> Behavior b -> Event b
Event a
g @> :: forall a b. Event a -> Behavior b -> Event b
@> Behavior b
f = (forall a b. a -> b -> a
const forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Behavior b
f) forall a b. Behavior (a -> b) -> Event a -> Event b
<@> Event a
g
filterApply :: Behavior (a -> Bool) -> Event a -> Event a
filterApply :: forall a. Behavior (a -> Bool) -> Event a -> Event a
filterApply Behavior (a -> Bool)
bp = forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
fmap forall a b. (a, b) -> b
snd forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a. (a -> Bool) -> Event a -> Event a
filterE forall a b. (a, b) -> a
fst forall b c a. (b -> c) -> (a -> b) -> a -> c
. forall a b. Behavior (a -> b) -> Event a -> Event b
apply ((\a -> Bool
p a
a-> (a -> Bool
p a
a,a
a)) forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Behavior (a -> Bool)
bp)
whenE :: Behavior Bool -> Event a -> Event a
whenE :: forall a. Behavior Bool -> Event a -> Event a
whenE Behavior Bool
bf = forall a. Behavior (a -> Bool) -> Event a -> Event a
filterApply (forall a b. a -> b -> a
const forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Behavior Bool
bf)
split :: Event (Either a b) -> (Event a, Event b)
split :: forall a b. Event (Either a b) -> (Event a, Event b)
split Event (Either a b)
e = (forall a. Event (Maybe a) -> Event a
filterJust forall a b. (a -> b) -> a -> b
$ forall a b. Either a b -> Maybe a
fromLeft forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Event (Either a b)
e, forall a. Event (Maybe a) -> Event a
filterJust forall a b. (a -> b) -> a -> b
$ forall a b. Either a b -> Maybe b
fromRight forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Event (Either a b)
e)
where
fromLeft :: Either a b -> Maybe a
fromLeft :: forall a b. Either a b -> Maybe a
fromLeft (Left a
a) = forall a. a -> Maybe a
Just a
a
fromLeft (Right b
_) = forall a. Maybe a
Nothing
fromRight :: Either a b -> Maybe b
fromRight :: forall a b. Either a b -> Maybe b
fromRight (Left a
_) = forall a. Maybe a
Nothing
fromRight (Right b
b) = forall a. a -> Maybe a
Just b
b
once :: MonadMoment m => Event a -> m (Event a)
once :: forall (m :: * -> *) a. MonadMoment m => Event a -> m (Event a)
once Event a
e = mdo
Event a
e1 <- forall (m :: * -> *) a.
MonadMoment m =>
Event a -> Event (Event a) -> m (Event a)
switchE Event a
e (forall a. Event a
never forall (f :: * -> *) a b. Functor f => a -> f b -> f a
<$ Event a
e1)
forall (m :: * -> *) a. Monad m => a -> m a
return Event a
e1
unions :: [Event (a -> a)] -> Event (a -> a)
unions :: forall a. [Event (a -> a)] -> Event (a -> a)
unions [] = forall a. Event a
never
unions [Event (a -> a)]
xs = forall (t :: * -> *) a. Foldable t => (a -> a -> a) -> t a -> a
foldr1 (forall a. (a -> a -> a) -> Event a -> Event a -> Event a
unionWith forall b c a. (b -> c) -> (a -> b) -> a -> c
(.)) [Event (a -> a)]
xs
accumB :: MonadMoment m => a -> Event (a -> a) -> m (Behavior a)
accumB :: forall (m :: * -> *) a.
MonadMoment m =>
a -> Event (a -> a) -> m (Behavior a)
accumB a
acc Event (a -> a)
e = forall (m :: * -> *) a.
MonadMoment m =>
a -> Event a -> m (Behavior a)
stepper a
acc forall (m :: * -> *) a b. Monad m => (a -> m b) -> m a -> m b
=<< forall (m :: * -> *) a.
MonadMoment m =>
a -> Event (a -> a) -> m (Event a)
accumE a
acc Event (a -> a)
e
mapAccum :: MonadMoment m => acc -> Event (acc -> (x,acc)) -> m (Event x, Behavior acc)
mapAccum :: forall (m :: * -> *) acc x.
MonadMoment m =>
acc -> Event (acc -> (x, acc)) -> m (Event x, Behavior acc)
mapAccum acc
acc Event (acc -> (x, acc))
ef = do
Event (x, acc)
e <- forall (m :: * -> *) a.
MonadMoment m =>
a -> Event (a -> a) -> m (Event a)
accumE (forall a. HasCallStack => a
undefined,acc
acc) (forall {t} {b} {a}. (t -> b) -> (a, t) -> b
lift forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Event (acc -> (x, acc))
ef)
Behavior acc
b <- forall (m :: * -> *) a.
MonadMoment m =>
a -> Event a -> m (Behavior a)
stepper acc
acc (forall a b. (a, b) -> b
snd forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Event (x, acc)
e)
forall (m :: * -> *) a. Monad m => a -> m a
return (forall a b. (a, b) -> a
fst forall (f :: * -> *) a b. Functor f => (a -> b) -> f a -> f b
<$> Event (x, acc)
e, Behavior acc
b)
where
lift :: (t -> b) -> (a, t) -> b
lift t -> b
f (a
_,t
acc) = t
acc seq :: forall a b. a -> b -> b
`seq` t -> b
f t
acc