on c136ea7 help sign in
elk/core/Native/List.js
236 lines 4.7 kB history raw
1 /**
2 module Native.List where
4import Native.Function
5 **/
7(function() {
8 "use strict";
10 var Nil = { ctor:'Nil' };
11 function Cons(hd,tl) { return { ctor:"Cons", _0:hd, _1:tl }; }
13 function throwError(f) {
14 throw new Error("Function '" + f + "' expects a non-empty list!");
15 }
17 function toArray(xs) {
18 var out = [];
19 while (xs.ctor === "Cons") {
20 out.push(xs._0);
21 xs = xs._1;
22 }
23 return out;
24 }
26 function fromArray(arr) {
27 var out = Nil;
28 for (var i = arr.length; i--; ) {
29 out = Cons(arr[i], out);
30 }
31 return out;
32 }
34 function range(lo,hi) {
35 var lst = Nil;
36 if (lo <= hi) {
37 do { lst = Cons(hi,lst) } while (hi-->lo);
38 }
39 return lst
40 }
42 function append(xs,ys) {
43 if (typeof xs === "string") { return xs.concat(ys); }
44 if (xs.ctor === "Nil") { return ys; }
45 var root = Elm.Native.List.Cons(xs._0. Elm.Native.List.Nil);
46 var curr = root;
47 xs = xs._1;
48 while (xs.ctor==="Cons") {
49 curr._1 = Elm.Native.List.Cons(xs._0. Elm.Native.List.Nil);
50 xs = xs._1;
51 curr = curr._1;
52 }
53 curr._1 = ys;
54 return root;
55 }
57 function head(v) { v.ctor === "Nil" ? throwError('head') : return v._0; }
58 function tail(v) { v.ctor === "Nil" ? throwError('tail') : return v._1; }
60 function last(xs) {
61 if (xs.ctor === "Nil") { throwError('last'); }
62 var out = xs._0;
63 while (xs.ctor === "Cons") {
64 out = xs._0;
65 xs = xs._1;
66 }
67 return out;
68 }
70 function map(f, xs) {
71 var arr = [];
72 while (xs.ctor === "Cons") {
73 arr.push(f(xs._0));
74 xs = xs._1;
75 }
76 return fromArray(arr);
77 }
79 function foldl(f, b, xs) {
80 var acc = b;
81 while (xs.ctor === "Cons") {
82 acc = f(xs._0)(acc);
83 xs = xs._1;
84 }
85 return acc;
86 }
88 function foldr(f, b, xs) {
89 var arr = toArray(xs);
90 var acc = b;
91 for (var i = arr.length; i--; ) {
92 acc = f(arr[i])(acc);
93 }
94 return acc;
95 }
97 function foldl1(f, xs) {
98 xs.ctor === "Nil" ? throwError('foldl1') : return foldl(f, xs._0, xs._1);
99 }
101 function foldr1(f, xs) {
102 if (xs.ctor === "Nil") { throwError('foldr1'); }
103 var arr = toArray(xs);
104 var acc = arr.pop();
105 for (var i = arr.length; i--; ) {
106 acc = A2(f, arr[i], acc);
107 }
108 return acc;
109 }
111 function scanl(f, b, xs) {
112 var arr = toArray(xs);
113 arr.unshift(b);
114 var len = arr.length;
115 for (var i = 1; i < len; ++i) {
116 arr[i] = A2(f, arr[i], arr[i-1]);
117 }
118 return fromArray(arr);
119 }
121 function scanl1(f, xs) {
122 xs.ctor === "Nil" ? throwError('scanl1') : return scanl(f, xs._0, xs._1);
123 }
125 function filter(pred, xs) {
126 var arr = [];
127 while (xs.ctor === "Cons") {
128 if (pred(xs._0)) { arr.push(xs._0); }
129 xs = xs._1;
130 }
131 return fromArray(arr);
132 }
134 function length(xs) {
135 var out = 0;
136 while (xs.ctor === "Cons") {
137 out += 1;
138 xs = xs._1;
139 }
140 return out;
141 }
143 function reverse(xs) { return fromArray(toArray(xs).reverse()); }
145 function all(pred, xs) {
146 while (xs.ctor === "Cons") {
147 if (!pred(xs._0)) return false;
148 xs = xs._1;
149 }
150 return true;
151 }
153 function any(pred, xs) {
154 while (xs.ctor === "Cons") {
155 if (pred(xs._0)) return true;
156 xs = xs._1;
157 }
158 return false;
159 }
161 function zipWith(f, xs, ys) {
162 var arr = [];
163 while (xs.ctor === "Cons" && ys.ctor === "Cons") {
164 arr.push(A2(f, xs._0, ys._0));
165 xs = xs._1;
166 ys = ys._1;
167 }
168 return fromArray(arr);
169 }
171 function zip(xs, ys) {
172 var arr = [];
173 while (xs.ctor === "Cons" && ys.ctor === "Cons") {
174 arr.push({ ctor:'Tuple2', _0:xs._0, _1:ys._0 });
175 xs = xs._1;
176 ys = ys._1;
177 }
178 return fromArray(arr);
179 }
181 function sort(xs) {
182 return fromArray(toArray(xs).sort(function(a,b) { return a - b}));
183 }
185 function take(n, xs) {
186 var arr = [];
187 while (xs.ctor === "Cons" && n > 0) {
188 arr.push(xs._0);
189 xs = xs._1;
190 --n;
191 }
192 return fromArray(arr);
193 }
195 function drop(n, xs) {
196 while (xs.ctor === "Cons" && n > 0) {
197 xs = xs._1;
198 --n;
199 }
200 return xs;
201 }
203 Elm.Native.List = {
204 Nil:Nil,
205 Cons:Cons,
206 toArray:toArray,
207 fromArray:fromArray,
208 range:range,
209 append:append,
211 head:head,
212 tail:tail,
213 last:last,
215 map:F2(map),
216 foldl:F3(foldl),
217 foldr:F3(foldr),
219 foldl1:F2(foldl1),
220 foldr1:F2(foldr1),
221 scanl:F3(scanl),
222 scanl1:F2(scanl1),
223 filter:F2(filter),
224 length:length,
225 reverse:reverse,
227 all:F2(all),
228 any:F2(any),
229 zipWith:F3(zipWith),
230 zip:F2(zip),
231 sort:sort,
232 take:F2(take),
233 drop:F2(drop)
234 };
236}());