The implementation of ifB in a reaction-banana

I am reading Conall Elliott's document, “Declarative Event-Oriented Programming,” in which his examples are written using the Fran library, which is now deprecated.

Since I am learning FRP, I am trying to implement these examples using reactive banana. It seems I have no problem with this (but I need to think a lot :)). The only thing that I do not understand is the correct translation of Fran ifB.

It seems like a signature of this type:

ifB :: Behavior Bool -> Behavior a -> Behavior a -> Behavior a

But for reactive banana only switchBmatters.

Currently, I have managed to do this by adding an additional trigger event, which will be used to switch behavior, for example:

ifB :: MonadMoment m => Event b -> BoolB -> Behavior a -> Behavior a -> m (Behavior a)
ifB trigger condB b1 b2 = switchB b2 (switcherB <@ trigger)
  where switcherB = (\x -> if x then b1 else b2) <$> condB

, . ? , ifB:

editCPoint :: User -> S.Point2 -> CPoint
editCPoint u p0 = (pos, closeEnough)
  where
    pos, lastRelease :: Point2B
    --    vvv this is the ifB in question!
    pos = ifB grabbing (mouse u) lastRelease
    lastRelease = stepper p0 (release ‘snapshot_‘ pos)

    closeEnough, grabbing :: BoolB
    closeEnough = distance2 pos (mouse u) <* grabDistance
    grabbing = stepper False (grab -=> True .|. release -=> False)

    grab, release :: Event ()
    grab = lbp u ‘whenE‘ closeEnough
    release = lbr u ‘whenE‘ grabbing

grabDistance :: RealB
grabDistance = 2 * pointSize

, , ifB cursor move, . , ...

? , ?

+4
1

Behavior Applicative, ifB :

ifB :: Behavior Bool -> Behavior a -> Behavior a -> Behavior a
ifB bB bT bF = (\b t f -> if b then t else f) <$> bB <*> bT <*> bF

, bool Data.Bool:

ifB :: Behavior Bool -> Behavior a -> Behavior a -> Behavior a
ifB bB bT bF = bool <$> bF <*> bT <*> bB
+4

Source: https://habr.com/ru/post/1670220/


All Articles