on cd4b4e2 help sign in
335 lines 8.6 kB history raw
1Elm.Native.List = {};
2Elm.Native.List.make = function(elm) {
3 elm.Native = elm.Native || {};
4 elm.Native.List = elm.Native.List || {};
5 if (elm.Native.List.values) return elm.Native.List.values;
6 if ('values' in Elm.Native.List)
7 return elm.Native.List.values = Elm.Native.List.values;
9 var Utils = Elm.Native.Utils.make(elm);
11 // TODO: Improve Nil handling
12 // We can change places like: if (xs.ctor === '[]') ... to if (xs === Nil) ...
13 // but only if we're confident Nil can only be defined once.
14 // Currently (27Mar2013) each module can have different instantiations, so multiple Nil objects can exist
15 // (and if they're used interchangeably then direct object comparison fails where ctor doesn't).
16 // So, this can only be fixed when modules initialisation is also fixed.
17 // The performance overhead of the .ctor calls is 5-10% according to jsperf (depending on fn + list size)
18 // (on firefox 19)
20 var Nil = { ctor:'[]' };
22 // using freeze for every cons would be nice but is a huge (9x on firefox 19)
23 // performance penalty
24 function Cons(hd,tl) { return { ctor:"::", _0:hd, _1:tl }; }
26 function throwError(f) {
27 throw new Error("Function '" + f + "' expects a non-empty list!");
28 }
30 function toArray(xs) {
31 var out = [];
32 while (xs.ctor !== '[]') {
33 out.push(xs._0);
34 xs = xs._1;
35 }
36 return out;
37 }
39 function fromArray(arr) {
40 var out = Nil;
41 for (var i = arr.length; i--; ) {
42 out = Cons(arr[i], out);
43 }
44 return out;
45 }
47 function range(lo,hi) {
48 var lst = Nil;
49 if (lo <= hi) {
50 do { lst = Cons(hi,lst) } while (hi-->lo);
51 }
52 return lst
53 }
55 function append(xs,ys) {
56 // append Text
57 if (xs.text || ys.text) {
58 return Utils.txt(Utils.makeText('',xs) + Utils.makeText('',ys));
59 }
61 // append Strings
62 if (typeof xs === "string") return xs + ys;
64 // append Lists
65 if (xs.ctor === '[]') { return ys; }
66 var root = Cons(xs._0, Nil);
67 var curr = root;
68 xs = xs._1;
69 while (xs.ctor !== '[]') {
70 curr._1 = Cons(xs._0, Nil);
71 xs = xs._1;
72 curr = curr._1;
73 }
74 curr._1 = ys;
75 return root;
76 }
78 function head(v) { return v.ctor === '[]' ? throwError('head') : v._0; }
79 function tail(v) { return v.ctor === '[]' ? throwError('tail') : v._1; }
81 function last(xs) {
82 if (xs.ctor === '[]') { throwError('last'); }
83 var out = xs._0;
84 while (xs.ctor !== '[]') {
85 out = xs._0;
86 xs = xs._1;
87 }
88 return out;
89 }
91 function map(f, xs) {
92 var arr = [];
93 while (xs.ctor !== '[]') {
94 arr.push(f(xs._0));
95 xs = xs._1;
96 }
97 return fromArray(arr);
98 }
100 // f defined similarly for both foldl and foldr (NB: different from Haskell)
101 // ie, foldl : (a -> b -> b) -> b -> [a] -> b
102 function foldl(f, b, xs) {
103 var acc = b;
104 while (xs.ctor !== '[]') {
105 acc = A2(f, xs._0, acc);
106 xs = xs._1;
107 }
108 return acc;
109 }
111 function foldr(f, b, xs) {
112 var arr = toArray(xs);
113 var acc = b;
114 for (var i = arr.length; i--; ) {
115 acc = A2(f, arr[i], acc);
116 }
117 return acc;
118 }
120 function foldl1(f, xs) {
121 return xs.ctor === '[]' ? throwError('foldl1') : foldl(f, xs._0, xs._1);
122 }
124 function foldr1(f, xs) {
125 if (xs.ctor === '[]') { throwError('foldr1'); }
126 var arr = toArray(xs);
127 var acc = arr.pop();
128 for (var i = arr.length; i--; ) {
129 acc = A2(f, arr[i], acc);
130 }
131 return acc;
132 }
134 function scanl(f, b, xs) {
135 var arr = toArray(xs);
136 arr.unshift(b);
137 var len = arr.length;
138 for (var i = 1; i < len; ++i) {
139 arr[i] = A2(f, arr[i], arr[i-1]);
140 }
141 return fromArray(arr);
142 }
144 function scanl1(f, xs) {
145 return xs.ctor === '[]' ? throwError('scanl1') : scanl(f, xs._0, xs._1);
146 }
148 function filter(pred, xs) {
149 var arr = [];
150 while (xs.ctor !== '[]') {
151 if (pred(xs._0)) { arr.push(xs._0); }
152 xs = xs._1;
153 }
154 return fromArray(arr);
155 }
157 function length(xs) {
158 var out = 0;
159 while (xs.ctor !== '[]') {
160 out += 1;
161 xs = xs._1;
162 }
163 return out;
164 }
166 function member(x, xs) {
167 while (xs.ctor !== '[]') {
168 if (Utils.eq(x,xs._0)) return true;
169 xs = xs._1;
170 }
171 return false;
172 }
174 function reverse(xs) { return fromArray(toArray(xs).reverse()); }
176 function concat(xss) {
177 if (xss.ctor === '[]') return xss;
178 var arr = toArray(xss);
179 var xs = arr[arr.length-1];
180 for (var i = arr.length-1; i--; ) {
181 xs = append(arr[i], xs);
182 }
183 return xs;
184 }
186 function all(pred, xs) {
187 while (xs.ctor !== '[]') {
188 if (!pred(xs._0)) return false;
189 xs = xs._1;
190 }
191 return true;
192 }
194 function any(pred, xs) {
195 while (xs.ctor !== '[]') {
196 if (pred(xs._0)) return true;
197 xs = xs._1;
198 }
199 return false;
200 }
202 function zipWith(f, xs, ys) {
203 var arr = [];
204 while (xs.ctor !== '[]' && ys.ctor !== '[]') {
205 arr.push(A2(f, xs._0, ys._0));
206 xs = xs._1;
207 ys = ys._1;
208 }
209 return fromArray(arr);
210 }
212 function zip(xs, ys) {
213 var arr = [];
214 while (xs.ctor !== '[]' && ys.ctor !== '[]') {
215 arr.push(Utils.Tuple2(xs._0, ys._0));
216 xs = xs._1;
217 ys = ys._1;
218 }
219 return fromArray(arr);
220 }
222 function sort(xs) {
223 return fromArray(toArray(xs).sort(Utils.cmp));
224 }
226 function sortBy(f, xs) {
227 return fromArray(toArray(xs).sort(function(a,b){
228 return Utils.cmp(f(a), f(b));
229 }));
230 }
232 function sortWith(f, xs) {
233 return fromArray(toArray(xs).sort(function(a,b){
234 var ord = f(a)(b).ctor;
235 return ord === 'EQ' ? 0 : ord === 'LT' ? -1 : 1;
236 }));
237 }
239 function nth(xs, n) {
240 return toArray(xs)[n];
241 }
243 function take(n, xs) {
244 var arr = [];
245 while (xs.ctor !== '[]' && n > 0) {
246 arr.push(xs._0);
247 xs = xs._1;
248 --n;
249 }
250 return fromArray(arr);
251 }
253 function drop(n, xs) {
254 while (xs.ctor !== '[]' && n > 0) {
255 xs = xs._1;
256 --n;
257 }
258 return xs;
259 }
261 function repeat(n, x) {
262 var arr = [];
263 var pattern = [x];
264 while (n > 0) {
265 if (n & 1) arr = arr.concat(pattern);
266 n >>= 1, pattern = pattern.concat(pattern);
267 }
268 return fromArray(arr);
269 }
271 function join(sep, xss) {
272 if (sep.text) {
273 sep = Utils.makeText('',sep);
274 xss = toArray(xss);
275 for (var i = xss.length; i--; ) {
276 xss[i] = Utils.makeText('',xss[i]);
277 }
278 return Utils.txt(xss.join(sep));
279 }
280 if (typeof sep === 'string') return toArray(xss).join(sep);
281 if (xss.ctor === '[]') return Nil;
282 var s = toArray(sep);
283 var out = toArray(xss._0);
284 xss = xss._1;
285 while (xss.ctor !== '[]') {
286 out = out.concat(s, toArray(xss._0));
287 xss = xss._1;
288 }
289 return fromArray(out);
290 }
292 Elm.Native.List.values = {
293 Nil:Nil,
294 Cons:Cons,
295 cons:F2(Cons),
296 toArray:toArray,
297 fromArray:fromArray,
298 range:range,
299 append:append,
301 head:head,
302 tail:tail,
303 last:last,
305 map:F2(map),
306 foldl:F3(foldl),
307 foldr:F3(foldr),
309 foldl1:F2(foldl1),
310 foldr1:F2(foldr1),
311 scanl:F3(scanl),
312 scanl1:F2(scanl1),
313 filter:F2(filter),
314 length:length,
315 member:F2(member),
316 reverse:reverse,
317 concat:concat,
319 all:F2(all),
320 any:F2(any),
321 zipWith:F3(zipWith),
322 zip:F2(zip),
323 sort:sort,
324 sortBy:F2(sortBy),
325 sortWith:F2(sortWith),
326 nth:F2(nth),
327 take:F2(take),
328 drop:F2(drop),
329 repeat:F2(repeat),
331 join:F2(join)
332 };
333 return elm.Native.List.values = Elm.Native.List.values;
335};