on master help sign in
Add the native function module and transform the list library to use it.
9eddb7aevancz
core/Native/Function.jsnew file+104
@@ -0,0 +1,104 @@
1+/*
2+module Native.Function where
3+*/
4+
5+function F2(fun) {
6+ function wrapper(a) { return function(b) { return fun(a,b) } }
7+ wrapper.arity = 2;
8+ wrapper.func = fun;
9+ return wrapper;
10+}
11+
12+function F3(fun) {
13+ function wrapper(a) {
14+ return function(b) { return function(c) { return fun(a,b,c) }}
15+ }
16+ wrapper.arity = 3;
17+ wrapper.func = fun;
18+ return wrapper;
19+}
20+
21+function F4(fun) {
22+ function wrapper(a) { return function(b) { return function(c) {
23+ return function(d) { return fun(a,b,c,d) }}}
24+ }
25+ wrapper.arity = 4;
26+ wrapper.func = fun;
27+ return wrapper;
28+}
29+
30+function F5(fun) {
31+ function wrapper(a) { return function(b) { return function(c) {
32+ return function(d) { return function(e) { return fun(a,b,c,d,e) }}}}
33+ }
34+ wrapper.arity = 5;
35+ wrapper.func = fun;
36+ return wrapper;
37+}
38+
39+function F6(fun) {
40+ function wrapper(a) { return function(b) { return function(c) {
41+ return function(d) { return function(e) { return function(f) {
42+ return fun(a,b,c,d,e,f) }}}}}
43+ }
44+ wrapper.arity = 6;
45+ wrapper.func = fun;
46+ return wrapper;
47+}
48+
49+function F7(fun) {
50+ function wrapper(a) { return function(b) { return function(c) {
51+ return function(d) { return function(e) { return function(f) {
52+ return function(g) { return fun(a,b,c,d,e,f,g) }}}}}}
53+ }
54+ wrapper.arity = 7;
55+ wrapper.func = fun;
56+ return wrapper;
57+}
58+
59+function F8(fun) {
60+ function wrapper(a) { return function(b) { return function(c) {
61+ return function(d) { return function(e) { return function(f) {
62+ return function(g) { return function(h) {return fun(a,b,c,d,e,f,g,h)}}}}}}}
63+ }
64+ wrapper.arity = 8;
65+ wrapper.func = fun;
66+ return wrapper;
67+}
68+
69+function F9(fun) {
70+ function wrapper(a) { return function(b) { return function(c) {
71+ return function(d) { return function(e) { return function(f) {
72+ return function(g) { return function(h) { return function(i) {
73+ return fun(a,b,c,d,e,f,g,h,i) }}}}}}}}
74+ }
75+ wrapper.arity = 9;
76+ wrapper.func = fun;
77+ return wrapper;
78+}
79+
80+function A2(fun,a,b) {
81+ return fun.arity === 2 ? fun.func(a,b) : fun(a)(b);
82+}
83+function A3(fun,a,b,c) {
84+ return fun.arity === 3 ? fun.func(a,b,c) : fun(a)(b)(c);
85+}
86+function A4(fun,a,b,c,d) {
87+ return fun.arity === 4 ? fun.func(a,b,c,d) : fun(a)(b)(c)(d);
88+}
89+function A5(fun,a,b,c,d,e) {
90+ return fun.arity === 5 ? fun.func(a,b,c,d,e) : fun(a)(b)(c)(d)(e);
91+}
92+function A6(fun,a,b,c,d,e,f) {
93+ return fun.arity === 6 ? fun.func(a,b,c,d,e,f) : fun(a)(b)(c)(d)(e)(f);
94+}
95+function A7(fun,a,b,c,d,e,f,g) {
96+ return fun.arity === 7 ? fun.func(a,b,c,d,e,f,g) : fun(a)(b)(c)(d)(e)(f)(g);
97+}
98+function A8(fun,a,b,c,d,e,f,g,h) {
99+ return fun.arity === 8 ? fun.func(a,b,c,d,e,f,g,h) : fun(a)(b)(c)(d)(e)(f)(g)(h);
100+}
101+function A9(fun,a,b,c,d,e,f,g,h,i) {
102+ return fun.arity === 9 ? fun.func(a,b,c,d,e,f,g,h,i)
103+ : fun(a)(b)(c)(d)(e)(f)(g)(h)(i);
104+}
core/Native/List.js+58−72
@@ -1,5 +1,7 @@
1 /**
1 /**
2 module Native.List where
2 module Native.List where
3+
4+import Native.Function
3 **/
5 **/
4
6
5(function() {
7(function() {
@@ -65,79 +67,68 @@
65 return out;
67 return out;
66 }
68 }
67
69
68− function map(f) { return function(xs) {
70+ function map(f, xs) {
69 var arr = [];
71 var arr = [];
70 while (xs.ctor === "Cons") {
72 while (xs.ctor === "Cons") {
71 arr.push(f(xs._0));
73 arr.push(f(xs._0));
72 xs = xs._1;
74 xs = xs._1;
73 }
75 }
74 return fromArray(arr);
76 return fromArray(arr);
75− }
76 }
77 }
77
78
78− function foldl(f) { return function(b) { return function(xs) {
79+ function foldl(f, b, xs) {
79− var acc = b;
80+ var acc = b;
80− while (xs.ctor === "Cons") {
81+ while (xs.ctor === "Cons") {
81− acc = f(xs._0)(acc);
82+ acc = f(xs._0)(acc);
82− xs = xs._1;
83+ xs = xs._1;
83− }
84− return acc;
85 }
84 }
86− }
85+ return acc;
87 }
86 }
88
87
89− function foldr(f) { return function(b) { return function(xs) {
88+ function foldr(f, b, xs) {
90− var arr = toArray(xs);
89+ var arr = toArray(xs);
91− var acc = b;
90+ var acc = b;
92− for (var i = arr.length; i--; ) {
91+ for (var i = arr.length; i--; ) {
93− acc = f(arr[i])(acc);
92+ acc = f(arr[i])(acc);
94− }
95− return acc;
96 }
93 }
97− }
94+ return acc;
98 }
95 }
99
96
100− function foldl1(f) { return function(xs) {
97+ function foldl1(f, xs) {
101− xs.ctor === "Nil" ? throwError('foldl1') : return foldl(f)(xs._0)(xs._1);
98+ xs.ctor === "Nil" ? throwError('foldl1') : return foldl(f, xs._0, xs._1);
102− }
103 }
99 }
104
100
105− function foldr1(f) { return function(xs) {
101+ function foldr1(f, xs) {
106 if (xs.ctor === "Nil") { throwError('foldr1'); }
102 if (xs.ctor === "Nil") { throwError('foldr1'); }
107 var arr = toArray(xs);
103 var arr = toArray(xs);
108 var acc = arr.pop();
104 var acc = arr.pop();
109 for (var i = arr.length; i--; ) {
105 for (var i = arr.length; i--; ) {
110− acc = f(arr[i])(acc);
106+ acc = A2(f, arr[i], acc);
111 }
107 }
112 return acc;
108 return acc;
113− }
114 }
109 }
115
110
116− function scanl(f) { return function(b) { return function(xs) {
111+ function scanl(f, b, xs) {
117− var arr = toArray(xs);
112+ var arr = toArray(xs);
118− arr.unshift(b);
113+ arr.unshift(b);
119− var len = arr.length;
114+ var len = arr.length;
120− for (var i = 1; i < len; ++i) {
115+ for (var i = 1; i < len; ++i) {
121− arr[i] = f(arr[i])(arr[i-1]);
116+ arr[i] = A2(f, arr[i], arr[i-1]);
122− }
123− return fromArray(arr);
124 }
117 }
125− }
118+ return fromArray(arr);
126 }
119 }
127
120
128− function scanl1(f) { return function(xs) {
121+ function scanl1(f, xs) {
129− xs.ctor === "Nil" ? throwError('scanl1') : return scanl(f)(xs._0)(xs._1);
122+ xs.ctor === "Nil" ? throwError('scanl1') : return scanl(f, xs._0, xs._1);
130− }
131 }
123 }
132
124
133− function filter(pred) { return function(xs) {
125+ function filter(pred, xs) {
134 var arr = [];
126 var arr = [];
135 while (xs.ctor === "Cons") {
127 while (xs.ctor === "Cons") {
136 if (pred(xs._0)) { arr.push(xs._0); }
128 if (pred(xs._0)) { arr.push(xs._0); }
137 xs = xs._1;
129 xs = xs._1;
138 }
130 }
139 return fromArray(arr);
131 return fromArray(arr);
140− }
141 }
132 }
142
133
143 function length(xs) {
134 function length(xs) {
@@ -151,52 +142,47 @@
151
142
152 function reverse(xs) { return fromArray(toArray(xs).reverse()); }
143 function reverse(xs) { return fromArray(toArray(xs).reverse()); }
153
144
154− function all(pred) { return function(xs) {
145+ function all(pred, xs) {
155 while (xs.ctor === "Cons") {
146 while (xs.ctor === "Cons") {
156 if (!pred(xs._0)) return false;
147 if (!pred(xs._0)) return false;
157 xs = xs._1;
148 xs = xs._1;
158 }
149 }
159 return true;
150 return true;
160− }
161 }
151 }
162
152
163− function any(pred) { return function(xs) {
153+ function any(pred, xs) {
164 while (xs.ctor === "Cons") {
154 while (xs.ctor === "Cons") {
165 if (pred(xs._0)) return true;
155 if (pred(xs._0)) return true;
166 xs = xs._1;
156 xs = xs._1;
167 }
157 }
168 return false;
158 return false;
169− }
170 }
159 }
171
160
172− function zipWith(f) { return function(xs) { return function(ys) {
161+ function zipWith(f, xs, ys) {
173− var arr = [];
162+ var arr = [];
174− while (xs.ctor === "Cons" && ys.ctor === "Cons") {
163+ while (xs.ctor === "Cons" && ys.ctor === "Cons") {
175− arr.push(f(xs._0)(ys._0));
164+ arr.push(A2(f, xs._0, ys._0));
176− xs = xs._1;
165+ xs = xs._1;
177− ys = ys._1;
166+ ys = ys._1;
178− }
179− return fromArray(arr);
180 }
167 }
181− }
168+ return fromArray(arr);
182 }
169 }
183
170
184− function zip(xs) { return function(ys) {
171+ function zip(xs, ys) {
185 var arr = [];
172 var arr = [];
186 while (xs.ctor === "Cons" && ys.ctor === "Cons") {
173 while (xs.ctor === "Cons" && ys.ctor === "Cons") {
187− arr.push(["Tuple2",xs._0,ys._0]);
174+ arr.push({ ctor:'Tuple2', _0:xs._0, _1:ys._0 });
188 xs = xs._1;
175 xs = xs._1;
189 ys = ys._1;
176 ys = ys._1;
190 }
177 }
191 return fromArray(arr);
178 return fromArray(arr);
192− }
193 }
179 }
194
180
195 function sort(xs) {
181 function sort(xs) {
196 return fromArray(toArray(xs).sort(function(a,b) { return a - b}));
182 return fromArray(toArray(xs).sort(function(a,b) { return a - b}));
197 }
183 }
198
184
199− function take(n) { return function(xs) {
185+ function take(n, xs) {
200 var arr = [];
186 var arr = [];
201 while (xs.ctor === "Cons" && n > 0) {
187 while (xs.ctor === "Cons" && n > 0) {
202 arr.push(xs._0);
188 arr.push(xs._0);
@@ -204,16 +190,14 @@
204 --n;
190 --n;
205 }
191 }
206 return fromArray(arr);
192 return fromArray(arr);
207− }
208 }
193 }
209
194
210− function drop(n) { return function(xs) {
195+ function drop(n, xs) {
211 while (xs.ctor === "Cons" && n > 0) {
196 while (xs.ctor === "Cons" && n > 0) {
212 xs = xs._1;
197 xs = xs._1;
213 --n;
198 --n;
214 }
199 }
215 return xs;
200 return xs;
216− }
217 }
201 }
218
202
219 Elm.Native.List = {
203 Elm.Native.List = {
@@ -228,23 +212,25 @@
228 tail:tail,
212 tail:tail,
229 last:last,
213 last:last,
230
214
231− map:map,
215+ map:F2(map),
232− foldl:foldl,
216+ foldl:F3(foldl),
233− foldr:foldr,
217+ foldr:F3(foldr),
234
218
235− foldl1:foldl1,
219+ foldl1:F2(foldl1),
236− foldr1:foldr1,
220+ foldr1:F2(foldr1),
237− scanl:scanl,
221+ scanl:F3(scanl),
238− scanl1:scanl1,
222+ scanl1:F2(scanl1),
239− filter:filter,
223+ filter:F2(filter),
240 length:length,
224 length:length,
241 reverse:reverse,
225 reverse:reverse,
242
226
243− all:all,
227+ all:F2(all),
244− any:any,
228+ any:F2(any),
245− zipWith:zipWith,
229+ zipWith:F3(zipWith),
246− zip:zip,
230+ zip:F2(zip),
247 sort:sort,
231 sort:sort,
248− take:take,
232+ take:F2(take),
249− drop:drop};
233+ drop:F2(drop)
234+ };
235+
250}());
236}());