on master help sign in
Merge pull request #409 from maxsnew/keepWhen
f636b75Evan Czaplicki
libraries/Native/Signal/Signal.js−9
@@ -127,12 +127,6 @@ Elm.Native.Signal.make = function(elm) {
127 input.kids.push(this);
127 input.kids.push(this);
128 }
128 }
129
129
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−
136 function timestamp(a) {
130 function timestamp(a) {
137 function update() { return Utils.Tuple2(Date.now(), a.value); }
131 function update() { return Utils.Tuple2(Date.now(), a.value); }
138 return new LiftN(update, [a]);
132 return new LiftN(update, [a]);
@@ -225,9 +219,6 @@ Elm.Native.Signal.make = function(elm) {
225 keepIf : F3(function(pred,base,sig) {
219 keepIf : F3(function(pred,base,sig) {
226 return new DropIf(function(x) {return !pred(x);},base,sig); }),
220 return new DropIf(function(x) {return !pred(x);},base,sig); }),
227 dropIf : F3(function(pred,base,sig) { return new DropIf(pred,base,sig); }),
221 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),
231 dropRepeats : function(s) { return new DropRepeats(s);},
222 dropRepeats : function(s) { return new DropRepeats(s);},
232 sampleOn : F2(sampleOn),
223 sampleOn : F2(sampleOn),
233 timestamp : timestamp
224 timestamp : timestamp
libraries/Signal.elm+7−2
@@ -32,6 +32,10 @@ the [`Time`](/docs/Signal/Time.elm) library.
32import Native.Signal
32import Native.Signal
33import List (foldr)
33import List (foldr)
34
34
35+import Basics (fst, snd, not)
36+import Native.Error
37+import Maybe as M
38+
35data Signal a = Signal
39data Signal a = Signal
36
40
37{-| Create a constant signal that never changes. -}
41{-| Create a constant signal that never changes. -}
@@ -122,7 +126,8 @@ Until the first signal becomes false again, all events will be propagated. Elm
122does not allow undefined signals, so a base case must be provided in case the
126does not allow undefined signals, so a base case must be provided in case the
123first signal is not true initially. -}
127first signal is not true initially. -}
124keepWhen : Signal Bool -> a -> Signal a -> Signal a
128keepWhen : 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))
126
131
127{-| 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
128false, 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
@@ -130,7 +135,7 @@ first signal becomes true again, all events will be propagated. Elm does not
130allow undefined signals, s oa base case must be provided in case the first
135allow undefined signals, s oa base case must be provided in case the first
131signal is true initially. -}
136signal is true initially. -}
132dropWhen : Signal Bool -> a -> Signal a -> Signal a
137dropWhen : Signal Bool -> a -> Signal a -> Signal a
133−dropWhen = Native.Signal.dropWhen
138+dropWhen bs = keepWhen (not <~ bs)
134
139
135{-| Drop updates that repeat the current value of the signal.
140{-| Drop updates that repeat the current value of the signal.
136
141