on master help sign in
Merge pull request #409 from maxsnew/keepWhen

Change semantics of keepWhen/dropWhen.

commitf636b7576f3856ee6105668af0449297cee1fc0e authorEvan Czaplicki <info@elm-lang.org> dateTue 28 Jan 2014, 06:18 (13 years ago) parent56c4fb950bc5a4
2 files changed+7−11
libraries/Native/Signal/Signal.js−9 libraries/Signal.elm+7−2
libraries/Native/Signal/Signal.js−9
@@ -127,12 +127,6 @@ Elm.Native.Signal.make = function(elm) {
127127 input.kids.push(this);
128128 }
129129
130− function dropWhen(s1,b,s2) {
131− var pairs = lift2( F2(function(x,y){return {x:x,y:y};}), s1, s2 );
132− var dropped = new DropIf(function(p){return p.x;},{x:true,y:b},pairs);
133− return lift(function(p){return p.y;}, dropped);
134− }
135−
136130 function timestamp(a) {
137131 function update() { return Utils.Tuple2(Date.now(), a.value); }
138132 return new LiftN(update, [a]);
@@ -225,9 +219,6 @@ Elm.Native.Signal.make = function(elm) {
225219 keepIf : F3(function(pred,base,sig) {
226220 return new DropIf(function(x) {return !pred(x);},base,sig); }),
227221 dropIf : F3(function(pred,base,sig) { return new DropIf(pred,base,sig); }),
228− keepWhen : F3(function(s1,b,s2) {
229− return dropWhen(lift(function(b){return !b;},s1), b, s2); }),
230− dropWhen : F3(dropWhen),
231222 dropRepeats : function(s) { return new DropRepeats(s);},
232223 sampleOn : F2(sampleOn),
233224 timestamp : timestamp
libraries/Signal.elm+7−2
@@ -32,6 +32,10 @@ the [`Time`](/docs/Signal/Time.elm) library.
3232import Native.Signal
3333import List (foldr)
3434
35+import Basics (fst, snd, not)
36+import Native.Error
37+import Maybe as M
38+
3539data Signal a = Signal
3640
3741{-| Create a constant signal that never changes. -}
@@ -122,7 +126,8 @@ Until the first signal becomes false again, all events will be propagated. Elm
122126does not allow undefined signals, so a base case must be provided in case the
123127first signal is not true initially. -}
124128keepWhen : Signal Bool -> a -> Signal a -> Signal a
125−keepWhen = Native.Signal.keepWhen
129+keepWhen bs def sig =
130+ snd <~ (keepIf fst (False, def) ((,) <~ (sampleOn sig bs) ~ sig))
126131
127132{-| Drop events when the first signal is true. When the first signal becomes
128133false, the most recent value of the second signal will be propagated. Until the
@@ -130,7 +135,7 @@ first signal becomes true again, all events will be propagated. Elm does not
130135allow undefined signals, s oa base case must be provided in case the first
131136signal is true initially. -}
132137dropWhen : Signal Bool -> a -> Signal a -> Signal a
133−dropWhen = Native.Signal.dropWhen
138+dropWhen bs = keepWhen (not <~ bs)
134139
135140{-| Drop updates that repeat the current value of the signal.
136141