1Elm.Native.List = function(elm) {
4 elm.Native = elm.Native || {};
5 if (elm.Native.List) return elm.Native.List;
7 var Utils = Elm.Native.Utils(elm);
9 // TODO: Improve Nil handling
10 // We can change places like: if (xs.ctor === 'Nil') ... to if (xs === Nil) ...
11 // but only if we're confident Nil can only be defined once.
12 // Currently (27Mar2013) each module can have different instantiations, so multiple Nil objects can exist
13 // (and if they're used interchangeably then direct object comparison fails where ctor doesn't).
14 // So, this can only be fixed when modules initialisation is also fixed.
15 // The performance overhead of the .ctor calls is 5-10% according to jsperf (depending on fn + list size)
18 // freeze is universally supported and as a singleton introduces little performance penalty
19 // for a small amount of object safety
20 var Nil = Object.freeze({ ctor:'Nil' });
22 // using freeze for every cons would be nice but is a huge (9x on firefox 19) performance penalty
23 function Cons(hd,tl) { return { ctor:"Cons", _0:hd, _1:tl }; }
25 function throwError(f) {
26 throw new Error("Function '" + f + "' expects a non-empty list!");
29 function toArray(xs) {
31 while (xs.ctor !== 'Nil') {
38 function fromArray(arr) {
40 for (var i = arr.length; i--; ) {
41 out = Cons(arr[i], out);
46 function range(lo,hi) {
49 do { lst = Cons(hi,lst) } while (hi-->lo);
54 function append(xs,ys) {
55 if (typeof xs === "string") { return xs.concat(ys); }
56 if (xs.ctor === 'Nil') { return ys; }
57 var root = Cons(xs._0, Nil);
60 while (xs.ctor !== 'Nil') {
61 curr._1 = Cons(xs._0, Nil);
69 function head(v) { return v.ctor === 'Nil' ? throwError('head') : v._0; }
70 function tail(v) { return v.ctor === 'Nil' ? throwError('tail') : v._1; }
73 if (xs.ctor === 'Nil') { throwError('last'); }
75 while (xs.ctor !== 'Nil') {
84 while (xs.ctor !== 'Nil') {
88 return fromArray(arr);
91 // f defined similarly for both foldl and foldr (NB: different from Haskell)
92 // ie, foldl :: (a -> b -> b) -> b -> [a] -> b
93 function foldl(f, b, xs) {
95 while (xs.ctor !== 'Nil') {
96 acc = A2(f, xs._0, acc);
102 function foldr(f, b, xs) {
103 var arr = toArray(xs);
105 for (var i = arr.length; i--; ) {
106 acc = A2(f, arr[i], acc);
111 function foldl1(f, xs) {
112 return xs.ctor === 'Nil' ? throwError('foldl1') : foldl(f, xs._0, xs._1);
115 function foldr1(f, xs) {
116 if (xs.ctor === 'Nil') { throwError('foldr1'); }
117 var arr = toArray(xs);
119 for (var i = arr.length; i--; ) {
120 acc = A2(f, arr[i], acc);
125 function scanl(f, b, xs) {
126 var arr = toArray(xs);
128 var len = arr.length;
129 for (var i = 1; i < len; ++i) {
130 arr[i] = A2(f, arr[i], arr[i-1]);
132 return fromArray(arr);
135 function scanl1(f, xs) {
136 return xs.ctor === 'Nil' ? throwError('scanl1') : scanl(f, xs._0, xs._1);
139 function filter(pred, xs) {
141 while (xs.ctor !== 'Nil') {
142 if (pred(xs._0)) { arr.push(xs._0); }
145 return fromArray(arr);
148 function length(xs) {
150 while (xs.ctor !== 'Nil') {
157 function member(x, xs) {
158 while (xs.ctor !== 'Nil') {
159 if (Utils.eq(x,xs._0)) return true;
165 function reverse(xs) { return fromArray(toArray(xs).reverse()); }
167 function concat(xss) {
168 if (xss.ctor === 'Nil') return xss;
169 var arr = toArray(xss);
170 var xs = arr[arr.length-1];
171 for (var i = arr.length-1; i--; ) {
172 xs = append(arr[i], xs);
177 function all(pred, xs) {
178 while (xs.ctor !== 'Nil') {
179 if (!pred(xs._0)) return false;
185 function any(pred, xs) {
186 while (xs.ctor !== 'Nil') {
187 if (pred(xs._0)) return true;
193 function zipWith(f, xs, ys) {
195 while (xs.ctor !== 'Nil' && ys.ctor !== 'Nil') {
196 arr.push(A2(f, xs._0, ys._0));
200 return fromArray(arr);
203 function zip(xs, ys) {
205 while (xs.ctor !== 'Nil' && ys.ctor !== 'Nil') {
206 arr.push(Utils.Tuple2(xs._0, ys._0));
210 return fromArray(arr);
215 var ord = Utils.compare(a,b).ctor;
216 return ord=== 'EQ' ? 0 : ord === 'LT' ? -1 : 1;
218 return fromArray(toArray(xs).sort(cmp));
221 function take(n, xs) {
223 while (xs.ctor !== 'Nil' && n > 0) {
228 return fromArray(arr);
231 function drop(n, xs) {
232 while (xs.ctor !== 'Nil' && n > 0) {
239 function join(sep, xss) {
240 if (xss.ctor === 'Nil') return Nil;
241 var s = toArray(sep);
242 var out = toArray(xss._0);
244 while (xss.ctor !== 'Nil') {
245 out = out.concat(s, toArray(xss._0));
248 return fromArray(out);
251 function split(sep, xs) {
253 var array = toArray(xs);
254 var s = toArray(sep);
257 for (var i = array.length; i--; ) {
258 out = Cons(Cons(array[i],Nil), out);
263 for (var i = array.length - slen; i >= 0; --i) {
265 for (var j = slen; j--; ) {
266 if (!Utils.eq(array[i+j], s[j])) { match = false; break; }
269 out = Cons(temp,out);
273 temp = Cons(array[i+j], temp);
276 for (var j = slen-1; j--; ) {
277 temp = Cons(array[j], temp);
279 out = Cons(temp, out);
283 return elm.Native.List = {
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