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:
- time flows left right.
- a line signal.
- the block function takes 2 signals above , produces signal below.
- the shape @ left of each line initial value.
- a filled shape event on signal.
- an outlined shape when signal doesn't change.
- i've used shapes represent types.
- i've used colours represent different values.

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