How to get keepWhen behavior in Elm 0.15?

Feature keepWhenfrom earlier versions of Elm has been removed. I ported the Elm application with 0.14, but I was stuck trying to get one part of it to work where it uses keepWhen.

So basically I am looking for a function like

keepWhen : Signal Bool -> a -> Signal a -> Signal a

I found

filter : (a -> Bool) -> a -> Signal a -> Signal a

but this is not exactly the same, and I did not understand how to make it work.

+4
source share
2 answers

Answer: import it from the service pack

The easiest way is to use Signal.Extra.keepWhenfrom signal-extra .
(full disclosure: I am the author)

Important Implementation Information

, . ( Signal ):

keepWhen : Signal Bool -> a -> Signal a -> Signal a
keepWhen boolSig a aSig =
  zip boolSig aSig
  |> sampleOn aSig
  |> keepIf fst (True, a)
  |> map snd

kqr answer sampleOn, keepWhen . , keepWhen 0.14 a , True.

sampleWhen.

, . .

:

  • .
  • - .
  • - , .
  • - .
  • .
  • , .
  • .
  • .

keepWhen and older keepWhen + sample marble diagrams

, , , kqr.

+4

keepWhen Signal.filter Elm 0.15. , Signal.filter , , , (Signal.map2 (,)).

Signal Bool, Signal a, , Signal (a, Bool), Signal.filter Bool, , .

:

keepWhen : Signal Bool -> a -> Signal a -> Signal a
keepWhen cond def val =
  let combined = Signal.map2 (,) val cond
      filtered = Signal.filter snd (def, False) combined
      final    = Signal.map fst filtered
  in  final

"" Bool, Signal (a, Bool). Bool , , , Bool "" .

, : http://share-elm.com/sprout/553c01afe4b06aacf0e8985a. (0,0), , , .

+2

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


All Articles