Element from Behavior (Functional Reactive Programming)

131 views Asked by At

I'm just trying to get started with FRP and threepenny-gui, and I'm not sure how to do even basic things.

Suppose I have a function defined as so

timesClicked :: Element -> Behavior Int
timesClicked elem = accumulate (+) 0 (1 <$ UI.click elem)

and I want to display the value of the Behavoir on the page.

I can do something like

setup :: Window -> UI ()
setup rootWindow = void $ do
  button <- UI.button #+ [ string "Clickity!" ]
  output <- UI.p
  getBody rootWindow #+
    map element [ button, output ]

  let clicks = timesClicked loginButton

Edit: Working full code here.

I don't know how to attach the behavior into the output.

1

There are 1 answers

0
Theo Belaire On BEST ANSWER

Aha, I've got it figured out now, and am leaving this as an example for anyone else.

timesClicked elem = accumB (0::Int) ( (+1) <$ UI.click elem)


setup :: Window -> UI ()
setup rootWindow = void $ do
  button <- UI.button #+ [ string "Clickity!" ]
  output <- UI.p
  getBody rootWindow #+
    map element [ button, output ]

  clicks <- timesClicked button
  -- sink :: ReadWriteAttr x i o -> Behavior i -> UI x -> UI x
  element output # sink text (show <$> clicks)

If anyone wants to follow along with my progress, I've put it on github.