Uncomplicate keepWhen implementation

This commit is contained in:
Max New 2013-12-24 00:40:33 -06:00
parent 0a1c8990eb
commit 9dc1549431

View file

@ -32,7 +32,7 @@ the [`Time`](/docs/Signal/Time.elm) library.
import Native.Signal
import List (foldr)
import Basics (otherwise, not)
import Basics (fst, snd, not)
import Native.Error
import Maybe as M
@ -127,17 +127,7 @@ does not allow undefined signals, so a base case must be provided in case the
first signal is not true initially. -}
keepWhen : Signal Bool -> a -> Signal a -> Signal a
keepWhen bs def sig =
let toMaybe : Bool -> a -> M.Maybe a
toMaybe b x = if b
then M.Just x
else M.Nothing
fromJust : M.Maybe a -> a
fromJust m = case m of
M.Just x -> x
M.Nothing -> Native.Error.raise "Internal bug in keepWhen! Please report at https://github.com/evancz/Elm/issues?state=open"
in
fromJust <~ (keepIf M.isJust (M.Just def) (toMaybe <~ (sampleOn sig bs) ~ sig))
snd <~ (keepIf fst (False, def) ((,) <~ (sampleOn sig bs) ~ sig))
{-| Drop events when the first signal is true. When the first signal becomes
false, the most recent value of the second signal will be propagated. Until the