3Elm.Native.Signal.make = function(elm) {
5 elm.Native = elm.Native || {};
6 elm.Native.Signal = elm.Native.Signal || {};
7 if (elm.Native.Signal.values) return elm.Native.Signal.values;
9 var Utils = Elm.Native.Utils.make(elm);
10 var foldr1 = Elm.List.make(elm).foldr1;
12 function send(node, timestep, changed) {
14 for (var i = kids.length; i--; ) {
15 kids[i].recv(timestep, changed, node.id);
19 function Input(base) {
20 this.id = Utils.guid();
23 this.defaultNumberOfKids = 0;
24 this.recv = function(timestep, eid, v) {
25 var changed = eid === this.id;
26 if (changed) { this.value = v; }
27 send(this, timestep, changed);
30 elm.inputs.push(this);
33 function LiftN(update, args) {
34 this.id = Utils.guid();
35 this.value = update();
40 var isChanged = false;
42 this.recv = function(timestep, changed, parentID) {
44 if (changed) { isChanged = true; }
46 if (isChanged) { this.value = update(); }
47 send(this, timestep, isChanged);
52 for (var i = n; i--; ) { args[i].kids.push(this); }
55 function lift(func, a) {
56 function update() { return func(a.value); }
57 return new LiftN(update, [a]);
59 function lift2(func, a, b) {
60 function update() { return A2( func, a.value, b.value ); }
61 return new LiftN(update, [a,b]);
63 function lift3(func, a, b, c) {
64 function update() { return A3( func, a.value, b.value, c.value ); }
65 return new LiftN(update, [a,b,c]);
67 function lift4(func, a, b, c, d) {
68 function update() { return A4( func, a.value, b.value, c.value, d.value ); }
69 return new LiftN(update, [a,b,c,d]);
71 function lift5(func, a, b, c, d, e) {
72 function update() { return A5( func, a.value, b.value, c.value, d.value, e.value ); }
73 return new LiftN(update, [a,b,c,d,e]);
75 function lift6(func, a, b, c, d, e, f) {
76 function update() { return A6( func, a.value, b.value, c.value, d.value, e.value, f.value ); }
77 return new LiftN(update, [a,b,c,d,e,f]);
79 function lift7(func, a, b, c, d, e, f, g) {
80 function update() { return A7( func, a.value, b.value, c.value, d.value, e.value, f.value, g.value ); }
81 return new LiftN(update, [a,b,c,d,e,f,g]);
83 function lift8(func, a, b, c, d, e, f, g, h) {
84 function update() { return A8( func, a.value, b.value, c.value, d.value, e.value, f.value, g.value, h.value ); }
85 return new LiftN(update, [a,b,c,d,e,f,g,h]);
88 function Foldp(step, state, input) {
89 this.id = Utils.guid();
93 this.recv = function(timestep, changed, parentID) {
95 this.value = A2( step, input.value, this.value );
97 send(this, timestep, changed);
99 input.kids.push(this);
102 function foldp(step, state, input) {
103 return new Foldp(step, state, input);
106 function DropIf(pred,base,input) {
107 this.id = Utils.guid();
108 this.value = pred(input.value) ? base : input.value;
110 this.recv = function(timestep, changed, parentID) {
111 var chng = changed && !pred(input.value);
112 if (chng) { this.value = input.value; }
113 send(this, timestep, chng);
115 input.kids.push(this);
118 function DropRepeats(input) {
119 this.id = Utils.guid();
120 this.value = input.value;
122 this.recv = function(timestep, changed, parentID) {
123 var chng = changed && !Utils.eq(this.value,input.value);
124 if (chng) { this.value = input.value; }
125 send(this, timestep, chng);
127 input.kids.push(this);
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);
136 function timestamp(a) {
137 function update() { return Utils.Tuple2(Date.now(), a.value); }
138 return new LiftN(update, [a]);
141 function SampleOn(s1,s2) {
142 this.id = Utils.guid();
143 this.value = s2.value;
147 var isChanged = false;
149 this.recv = function(timestep, changed, parentID) {
150 if (parentID === s1.id) isChanged = changed;
153 if (isChanged) { this.value = s2.value; }
154 send(this, timestep, isChanged);
163 function sampleOn(s1,s2) { return new SampleOn(s1,s2); }
165 function delay(t,s) {
166 var delayed = new Input(s.value);
167 var firstEvent = true;
169 if (firstEvent) { firstEvent = false; return; }
170 setTimeout(function() { elm.notify(delayed.id, v); }, t);
172 function first(a,b) { return a; }
173 return new SampleOn(delayed, lift2(F2(first), delayed, lift(update,s)));
176 function Merge(s1,s2) {
177 this.id = Utils.guid();
178 this.value = s1.value;
183 var isChanged = false;
185 this.recv = function(timestep, changed, parentID) {
189 if (parentID == s2.id && next === null) { next = s2.value; }
190 if (parentID == s1.id) { next = s1.value; }
194 if (isChanged) { this.value = next; next = null; }
195 send(this, timestep, isChanged);
204 function merge(s1,s2) { return new Merge(s1,s2); }
205 function merges(ss) { return A2(foldr1, F2(merge), ss); }
207 return elm.Native.Signal.values = {
208 constant : function(v) { return new Input(v); },
221 count : function(s) { return foldp(F2(function(_,c) { return c+1; }), 0, s); },
222 countIf : F2(function(pred,s) {
223 return foldp(F2(function(x,c){
224 return pred(x) ? c+1 : c; }), 0, s)}),
225 keepIf : F3(function(pred,base,sig) {
226 return new DropIf(function(x) {return !pred(x);},base,sig); }),
227 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);},
232 sampleOn : F2(sampleOn),
233 timestamp : timestamp
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list