on master help sign in
Uncomplicate keepWhen implementation
9dc1549Max New
libraries/Signal.elm+2−12
@@ -32,7 +32,7 @@ the [`Time`](/docs/Signal/Time.elm) library.
32import Native.Signal
32import Native.Signal
33import List (foldr)
33import List (foldr)
34
34
35−import Basics (otherwise, not)
35+import Basics (fst, snd, not)
36import Native.Error
36import Native.Error
37import Maybe as M
37import Maybe as M
38
38
@@ -127,17 +127,7 @@ does not allow undefined signals, so a base case must be provided in case the
127first signal is not true initially. -}
127first signal is not true initially. -}
128keepWhen : Signal Bool -> a -> Signal a -> Signal a
128keepWhen : Signal Bool -> a -> Signal a -> Signal a
129keepWhen bs def sig =
129keepWhen bs def sig =
130− let toMaybe : Bool -> a -> M.Maybe a
131− toMaybe b x = if b
132− then M.Just x
133− else M.Nothing
134−
135− fromJust : M.Maybe a -> a
136− fromJust m = case m of
137− M.Just x -> x
138− M.Nothing -> Native.Error.raise "Internal bug in keepWhen! Please report at https://github.com/evancz/Elm/issues?state=open"
139− in
140− fromJust <~ (keepIf M.isJust (M.Just def) (toMaybe <~ (sampleOn sig bs) ~ sig))
130+ snd <~ (keepIf fst (False, def) ((,) <~ (sampleOn sig bs) ~ sig))
141
131
142{-| Drop events when the first signal is true. When the first signal becomes
132{-| Drop events when the first signal is true. When the first signal becomes
143false, the most recent value of the second signal will be propagated. Until the
133false, the most recent value of the second signal will be propagated. Until the