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)
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!");
30 function toArray(xs) {
32 while (xs.ctor !== '[]') {
39 function fromArray(arr) {
41 for (var i = arr.length; i--; ) {
42 out = Cons(arr[i], out);
47 function range(lo,hi) {
50 do { lst = Cons(hi,lst) } while (hi-->lo);
55 function append(xs,ys) {
56 if (xs.isText) return Utils.txt(xs.concat(ys));
57 if (typeof xs === "string") return xs.concat(ys);
58 if (xs.ctor === '[]') { return ys; }
59 var root = Cons(xs._0, Nil);
62 while (xs.ctor !== '[]') {
63 curr._1 = Cons(xs._0, Nil);
71 function head(v) { return v.ctor === '[]' ? throwError('head') : v._0; }
72 function tail(v) { return v.ctor === '[]' ? throwError('tail') : v._1; }
75 if (xs.ctor === '[]') { throwError('last'); }
77 while (xs.ctor !== '[]') {
86 while (xs.ctor !== '[]') {
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 !== '[]') {
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 === '[]' ? throwError('foldl1') : foldl(f, xs._0, xs._1);
117 function foldr1(f, xs) {
118 if (xs.ctor === '[]') { 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 === '[]' ? throwError('scanl1') : scanl(f, xs._0, xs._1);
141 function filter(pred, xs) {
143 while (xs.ctor !== '[]') {
144 if (pred(xs._0)) { arr.push(xs._0); }
147 return fromArray(arr);
150 function length(xs) {
152 while (xs.ctor !== '[]') {
159 function member(x, xs) {
160 while (xs.ctor !== '[]') {
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 === '[]') 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 !== '[]') {
181 if (!pred(xs._0)) return false;
187 function any(pred, xs) {
188 while (xs.ctor !== '[]') {
189 if (pred(xs._0)) return true;
195 function zipWith(f, xs, ys) {
197 while (xs.ctor !== '[]' && ys.ctor !== '[]') {
198 arr.push(A2(f, xs._0, ys._0));
202 return fromArray(arr);
205 function zip(xs, ys) {
207 while (xs.ctor !== '[]' && ys.ctor !== '[]') {
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 nth(xs, n) {
224 return toArray(xs)[n];
227 function take(n, xs) {
229 while (xs.ctor !== '[]' && n > 0) {
234 return fromArray(arr);
237 function drop(n, xs) {
238 while (xs.ctor !== '[]' && n > 0) {
245 function repeat(n, x) {
249 if (n & 1) arr = arr.concat(pattern);
250 n >>= 1, pattern = pattern.concat(pattern);
252 return fromArray(arr);
255 function join(sep, xss) {
256 if (sep.isText) return Utils.txt(toArray(xss).join(sep));
257 if (typeof sep === 'string') return toArray(xss).join(sep);
258 if (xss.ctor === '[]') return Nil;
259 var s = toArray(sep);
260 var out = toArray(xss._0);
262 while (xss.ctor !== '[]') {
263 out = out.concat(s, toArray(xss._0));
266 return fromArray(out);
269 Elm.Native.List.values = {
308 return elm.Native.List.values = 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