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 timestamp(a) {
131 function update() { return Utils.Tuple2(Date.now(), a.value); }
132 return new LiftN(update, [a]);
135 function SampleOn(s1,s2) {
136 this.id = Utils.guid();
137 this.value = s2.value;
141 var isChanged = false;
143 this.recv = function(timestep, changed, parentID) {
144 if (parentID === s1.id) isChanged = changed;
147 if (isChanged) { this.value = s2.value; }
148 send(this, timestep, isChanged);
157 function sampleOn(s1,s2) { return new SampleOn(s1,s2); }
159 function delay(t,s) {
160 var delayed = new Input(s.value);
161 var firstEvent = true;
163 if (firstEvent) { firstEvent = false; return; }
164 setTimeout(function() { elm.notify(delayed.id, v); }, t);
166 function first(a,b) { return a; }
167 return new SampleOn(delayed, lift2(F2(first), delayed, lift(update,s)));
170 function Merge(s1,s2) {
171 this.id = Utils.guid();
172 this.value = s1.value;
177 var isChanged = false;
179 this.recv = function(timestep, changed, parentID) {
183 if (parentID == s2.id && next === null) { next = s2.value; }
184 if (parentID == s1.id) { next = s1.value; }
188 if (isChanged) { this.value = next; next = null; }
189 send(this, timestep, isChanged);
198 function merge(s1,s2) { return new Merge(s1,s2); }
199 function merges(ss) { return A2(foldr1, F2(merge), ss); }
201 return elm.Native.Signal.values = {
202 constant : function(v) { return new Input(v); },
215 count : function(s) { return foldp(F2(function(_,c) { return c+1; }), 0, s); },
216 countIf : F2(function(pred,s) {
217 return foldp(F2(function(x,c){
218 return pred(x) ? c+1 : c; }), 0, s)}),
219 keepIf : F3(function(pred,base,sig) {
220 return new DropIf(function(x) {return !pred(x);},base,sig); }),
221 dropIf : F3(function(pred,base,sig) { return new DropIf(pred,base,sig); }),
222 dropRepeats : function(s) { return new DropRepeats(s);},
223 sampleOn : F2(sampleOn),
224 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