module FRP.NetWire.Switch
(
switch, dSwitch,
rSwitch, drSwitch,
parB,
rpSwitchB, drpSwitchB,
par,
rpSwitch, drpSwitch
)
where
import qualified Data.Traversable as T
import Data.Traversable (Traversable)
import FRP.NetWire.Wire
drpSwitch ::
Traversable f =>
(forall w. a -> f w -> f (b, w)) ->
f (Wire b c) ->
Wire (a, Event (f (Wire b c) -> f (Wire b c))) (f c)
drpSwitch route wires''' =
WGen $ \ws (x'', ev) -> do
let wires'' = route x'' wires'''
r <- T.sequenceA $ fmap (\(x', w') -> toGen w' ws x') wires''
let xs = T.sequenceA . fmap fst $ r
wires' = fmap snd r
wires = maybe id id ev wires'
return (xs, rpSwitch route wires)
drpSwitchB ::
forall a b f. Traversable f =>
f (Wire a b) ->
Wire (a, Event (f (Wire a b) -> f (Wire a b))) (f b)
drpSwitchB wires'' =
WGen $ \ws (x', ev) -> do
r <- T.sequenceA $ fmap (\w' -> toGen w' ws x') wires''
let xs = T.sequenceA . fmap fst $ r
wires' = fmap snd r
wires = maybe id id ev wires'
return (xs, rpSwitchB wires)
drSwitch :: Wire a b -> Wire (a, Event (Wire a b)) b
drSwitch w1' =
WGen $ \ws (x', swEv) -> do
(mx, w1) <- toGen w1' ws x'
let w = maybe w1 id swEv
w `seq` return (mx, drSwitch w)
dSwitch :: Wire a (b, Event c) -> (c -> Wire a b) -> Wire a b
dSwitch w1' f =
WGen $ \ws x' -> do
(m, w1) <- toGen w1' ws x'
case m of
Nothing -> return (Nothing, dSwitch w1 f)
Just (x, swEv) ->
case swEv of
Nothing -> return (Just x, dSwitch w1 f)
Just sw -> return (Just x, f sw)
par ::
Traversable f =>
(forall w. a -> f w -> f (b, w)) -> f (Wire b c) -> Wire a (f c)
par route wires'' =
WGen $ \ws x'' -> do
let wires' = route x'' wires''
r <- T.sequenceA $ fmap (\(x', w') -> toGen w' ws x') wires'
let xs = T.sequenceA . fmap fst $ r
wires = fmap snd r
return (xs, par route wires)
parB :: Traversable f => f (Wire a b) -> Wire a (f b)
parB wires' =
WGen $ \ws x' -> do
r <- T.sequenceA $ fmap (\w' -> toGen w' ws x') wires'
let xs = T.sequenceA . fmap fst $ r
wires = fmap snd r
return (xs, parB wires)
rpSwitch ::
Traversable f =>
(forall w. a -> f w -> f (b, w)) ->
f (Wire b c) ->
Wire (a, Event (f (Wire b c) -> f (Wire b c))) (f c)
rpSwitch route wires''' =
WGen $ \ws (x'', ev) -> do
let wires'' = maybe id id ev wires'''
wires' = route x'' wires''
r <- T.sequenceA $ fmap (\(x', w') -> toGen w' ws x') wires'
let xs = T.sequenceA . fmap fst $ r
wires = fmap snd r
return (xs, rpSwitch route wires)
rpSwitchB ::
Traversable f =>
f (Wire a b) -> Wire (a, Event (f (Wire a b) -> f (Wire a b))) (f b)
rpSwitchB wires'' =
WGen $ \ws (x', ev) -> do
let wires' = maybe id id ev wires''
r <- T.sequenceA $ fmap (\w' -> toGen w' ws x') wires'
let xs = T.sequenceA . fmap fst $ r
wires = fmap snd r
return (xs, rpSwitchB wires)
rSwitch :: Wire a b -> Wire (a, Event (Wire a b)) b
rSwitch w1 =
WGen $ \ws (x', swEv) -> do
let w' = maybe w1 id swEv
(mx, w) <- toGen w' ws x'
return (mx, rSwitch w)
switch :: Wire a (b, Event c) -> (c -> Wire a b) -> Wire a b
switch w1' f =
WGen $ \ws x' -> do
(m, w1) <- toGen w1' ws x'
case m of
Nothing -> return (Nothing, switch w1 f)
Just (x, swEv) ->
case swEv of
Nothing -> return (Just x, switch w1 f)
Just sw -> toGen (f sw) (ws { wsDTime = 0 }) x'