1Elm.Native.List = function(elm) {
4 elm.Native = elm.Native || {};
5 if (elm.Native.List) return elm.Native.List;
6 if ('values' in Elm.Native.List)
7 return elm.Native.List = Elm.Native.List.values;
9 var Utils = Elm.Native.Utils(elm);
11 // TODO: Improve Nil handling
12 // We can change places like: if (xs.ctor === 'Nil') ... 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)
20 // freeze is universally supported and as a singleton introduces little performance penalty
21 // for a small amount of object safety
22 var Nil = Object.freeze({ ctor:'Nil' });
24 // using freeze for every cons would be nice but is a huge (9x on firefox 19) performance penalty
25 function Cons(hd,tl) { return { ctor:"Cons", _0:hd, _1:tl }; }
27 function throwError(f) {
28 throw new Error("Function '" + f + "' expects a non-empty list!");
31 function toArray(xs) {
33 while (xs.ctor !== 'Nil') {
40 function fromArray(arr) {
42 for (var i = arr.length; i--; ) {
43 out = Cons(arr[i], out);
48 function range(lo,hi) {
51 do { lst = Cons(hi,lst) } while (hi-->lo);
56 function append(xs,ys) {
57 if (typeof xs === "string") { return xs.concat(ys); }
58 if (xs.ctor === 'Nil') { return ys; }
59 var root = Cons(xs._0, Nil);
62 while (xs.ctor !== 'Nil') {
63 curr._1 = Cons(xs._0, Nil);
71 function head(v) { return v.ctor === 'Nil' ? throwError('head') : v._0; }
72 function tail(v) { return v.ctor === 'Nil' ? throwError('tail') : v._1; }
75 if (xs.ctor === 'Nil') { throwError('last'); }
77 while (xs.ctor !== 'Nil') {
86 while (xs.ctor !== 'Nil') {
90 return fromArray(arr);
93 // f defined similarly for both foldl and foldr (NB: different from Haskell)
94 // ie, foldl :: (a -> b -> b) -> b -> [a] -> b
95 function foldl(f, b, xs) {
97 while (xs.ctor !== 'Nil') {
98 acc = A2(f, xs._0, acc);
104 function foldr(f, b, xs) {
105 var arr = toArray(xs);
107 for (var i = arr.length; i--; ) {
108 acc = A2(f, arr[i], acc);
113 function foldl1(f, xs) {
114 return xs.ctor === 'Nil' ? throwError('foldl1') : foldl(f, xs._0, xs._1);
117 function foldr1(f, xs) {
118 if (xs.ctor === 'Nil') { throwError('foldr1'); }
119 var arr = toArray(xs);
121 for (var i = arr.length; i--; ) {
122 acc = A2(f, arr[i], acc);
127 function scanl(f, b, xs) {
128 var arr = toArray(xs);
130 var len = arr.length;
131 for (var i = 1; i < len; ++i) {
132 arr[i] = A2(f, arr[i], arr[i-1]);
134 return fromArray(arr);
137 function scanl1(f, xs) {
138 return xs.ctor === 'Nil' ? throwError('scanl1') : scanl(f, xs._0, xs._1);
141 function filter(pred, xs) {
143 while (xs.ctor !== 'Nil') {
144 if (pred(xs._0)) { arr.push(xs._0); }
147 return fromArray(arr);
150 function length(xs) {
152 while (xs.ctor !== 'Nil') {
159 function member(x, xs) {
160 while (xs.ctor !== 'Nil') {
161 if (Utils.eq(x,xs._0)) return true;
167 function reverse(xs) { return fromArray(toArray(xs).reverse()); }
169 function concat(xss) {
170 if (xss.ctor === 'Nil') return xss;
171 var arr = toArray(xss);
172 var xs = arr[arr.length-1];
173 for (var i = arr.length-1; i--; ) {
174 xs = append(arr[i], xs);
179 function all(pred, xs) {
180 while (xs.ctor !== 'Nil') {
181 if (!pred(xs._0)) return false;
187 function any(pred, xs) {
188 while (xs.ctor !== 'Nil') {
189 if (pred(xs._0)) return true;
195 function zipWith(f, xs, ys) {
197 while (xs.ctor !== 'Nil' && ys.ctor !== 'Nil') {
198 arr.push(A2(f, xs._0, ys._0));
202 return fromArray(arr);
205 function zip(xs, ys) {
207 while (xs.ctor !== 'Nil' && ys.ctor !== 'Nil') {
208 arr.push(Utils.Tuple2(xs._0, ys._0));
212 return fromArray(arr);
217 var ord = Utils.compare(a,b).ctor;
218 return ord=== 'EQ' ? 0 : ord === 'LT' ? -1 : 1;
220 return fromArray(toArray(xs).sort(cmp));
223 function take(n, xs) {
225 while (xs.ctor !== 'Nil' && n > 0) {
230 return fromArray(arr);
233 function drop(n, xs) {
234 while (xs.ctor !== 'Nil' && n > 0) {
241 function join(sep, xss) {
242 if (typeof sep === 'string') return toArray(xss).join(sep);
243 if (xss.ctor === 'Nil') return Nil;
244 var s = toArray(sep);
245 var out = toArray(xss._0);
247 while (xss.ctor !== 'Nil') {
248 out = out.concat(s, toArray(xss._0));
251 return fromArray(out);
254 function split(sep, xs) {
256 var array = toArray(xs);
257 var s = toArray(sep);
260 for (var i = array.length; i--; ) {
261 out = Cons(Cons(array[i],Nil), out);
266 for (var i = array.length - slen; i >= 0; --i) {
268 for (var j = slen; j--; ) {
269 if (!Utils.eq(array[i+j], s[j])) { match = false; break; }
272 out = Cons(temp,out);
276 temp = Cons(array[i+j], temp);
279 for (var j = slen-1; j--; ) {
280 temp = Cons(array[j], temp);
282 out = Cons(temp, out);
286 Elm.Native.List.values = {
323 return elm.Native.List = Elm.Native.List.values;
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