functional programming - How do I get keepWhen behaviour in Elm 0.15? -


the keepwhen function earlier versions of elm removed. have ported elm application 0.14, i'm stuck @ trying 1 part of work, it's using keepwhen.

so i'm looking function like

keepwhen : signal bool -> -> signal -> signal 

i have found

filter : (a -> bool) -> -> signal -> signal 

but it's not quite same thing, , haven't figured out how work.

answer: import utility package

the easiest way use signal.extra.keepwhen signal-extra package.
(full disclosure: i'm author)

important implementation detail

notice implementation isn't trivial. implementation in package (the signal module imported unqualified):

keepwhen : signal bool -> -> signal -> signal keepwhen boolsig asig =   zip boolsig asig   |> sampleon asig   |> keepif fst (true, a)   |> map snd 

the important difference version in kqr's answer sampleon keeps output of keepwhen updating when boolean input changes. difference between 2 filters keepwhen 0.14 filters update events a input , doesn't sample when boolean input becomes true.

the other implementation in signal-extra under name samplewhen.

diagrams

if know little marble diagrams, maybe these old diagrams may help. i'll post screenshot of relevant ones below.

the way read these diagrams:

  1. time flows left right.
  2. a line signal.
  3. the block function takes 2 signals above , produces signal below.
  4. the shape @ left of each line initial value.
  5. a filled shape event on signal.
  6. an outlined shape when signal doesn't change.
  7. i've used shapes represent types.
  8. i've used colours represent different values.

keepwhen , older keepwhen+sample marble diagrams

note second diagram, labeled old behaviour, matches behaviour of code in kqr's answer.


Comments

Popular posts from this blog

asp.net mvc - SSO between MVCForum and Umbraco7 -

Python Tkinter keyboard using bind -

ubuntu - Selenium Node Not Connecting to Hub, Not Opening Port -