1//------------------------------------------------------------------------------
2// Array creation --------------------------------------------------------------
3//------------------------------------------------------------------------------
5export const rangeImpl = function (start, end) {
6 var step = start > end ? -1 : 1;
7 var result = new Array(step * (end - start) + 1);
17var replicateFill = function (count, value) {
21 var result = new Array(count);
22 return result.fill(value);
25var replicatePolyfill = function (count, value) {
28 for (var i = 0; i < count; i++) {
34// In browsers that have Array.prototype.fill we use it, as it's faster.
35export const replicateImpl = typeof Array.prototype.fill === "function" ? replicateFill : replicatePolyfill;
37export const fromFoldableImpl = (function () {
38 function Cons(head, tail) {
44 function curryCons(head) {
45 return function (tail) {
46 return new Cons(head, tail);
50 function listToArray(list) {
54 while (xs !== emptyList) {
55 result[count++] = xs.head;
61 return function (foldr, xs) {
62 return listToArray(foldr(curryCons)(emptyList)(xs));
66//------------------------------------------------------------------------------
67// Array size ------------------------------------------------------------------
68//------------------------------------------------------------------------------
70export const length = function (xs) {
74//------------------------------------------------------------------------------
75// Non-indexed reads -----------------------------------------------------------
76//------------------------------------------------------------------------------
78export const unconsImpl = function (empty, next, xs) {
79 return xs.length === 0 ? empty({}) : next(xs[0])(xs.slice(1));
82//------------------------------------------------------------------------------
83// Indexed operations ----------------------------------------------------------
84//------------------------------------------------------------------------------
86export const indexImpl = function (just, nothing, xs, i) {
87 return i < 0 || i >= xs.length ? nothing : just(xs[i]);
90export const findMapImpl = function (nothing, isJust, f, xs) {
91 for (var i = 0; i < xs.length; i++) {
92 var result = f(xs[i]);
93 if (isJust(result)) return result;
98export const findIndexImpl = function (just, nothing, f, xs) {
99 for (var i = 0, l = xs.length; i < l; i++) {
100 if (f(xs[i])) return just(i);
105export const findLastIndexImpl = function (just, nothing, f, xs) {
106 for (var i = xs.length - 1; i >= 0; i--) {
107 if (f(xs[i])) return just(i);
112export const _insertAt = function (just, nothing, i, a, l) {
113 if (i < 0 || i > l.length) return nothing;
119export const _deleteAt = function (just, nothing, i, l) {
120 if (i < 0 || i >= l.length) return nothing;
126export const _updateAt = function (just, nothing, i, a, l) {
127 if (i < 0 || i >= l.length) return nothing;
133//------------------------------------------------------------------------------
134// Transformations -------------------------------------------------------------
135//------------------------------------------------------------------------------
137export const reverse = function (l) {
138 return l.slice().reverse();
141export const concat = function (xss) {
142 if (xss.length <= 10000) {
143 // This method is faster, but it crashes on big arrays.
144 // So we use it when can and fallback to simple variant otherwise.
145 return Array.prototype.concat.apply([], xss);
149 for (var i = 0, l = xss.length; i < l; i++) {
151 for (var j = 0, m = xs.length; j < m; j++) {
158export const filterImpl = function (f, xs) {
162export const partitionImpl = function (f, xs) {
165 for (var i = 0; i < xs.length; i++) {
172 return { yes: yes, no: no };
175export const scanlImpl = function (f, b, xs) {
178 var out = new Array(len);
179 for (var i = 0; i < len; i++) {
186export const scanrImpl = function (f, b, xs) {
189 var out = new Array(len);
190 for (var i = len - 1; i >= 0; i--) {
197//------------------------------------------------------------------------------
198// Sorting ---------------------------------------------------------------------
199//------------------------------------------------------------------------------
201export const sortByImpl = (function () {
202 function mergeFromTo(compare, fromOrdering, xs1, xs2, from, to) {
211 mid = from + ((to - from) >> 1);
212 if (mid - from > 1) mergeFromTo(compare, fromOrdering, xs2, xs1, from, mid);
213 if (to - mid > 1) mergeFromTo(compare, fromOrdering, xs2, xs1, mid, to);
218 while (i < mid && j < to) {
221 c = fromOrdering(compare(x)(y));
239 return function (compare, fromOrdering, xs) {
242 if (xs.length < 2) return xs;
245 mergeFromTo(compare, fromOrdering, out, xs.slice(0), 0, xs.length);
251//------------------------------------------------------------------------------
252// Subarrays -------------------------------------------------------------------
253//------------------------------------------------------------------------------
255export const sliceImpl = function (s, e, l) {
256 return l.slice(s, e);
259//------------------------------------------------------------------------------
260// Zipping ---------------------------------------------------------------------
261//------------------------------------------------------------------------------
263export const zipWithImpl = function (f, xs, ys) {
264 var l = xs.length < ys.length ? xs.length : ys.length;
265 var result = new Array(l);
266 for (var i = 0; i < l; i++) {
267 result[i] = f(xs[i])(ys[i]);
272//------------------------------------------------------------------------------
273// Folding ---------------------------------------------------------------------
274//------------------------------------------------------------------------------
276export const anyImpl = function (p, xs) {
278 for (var i = 0; i < len; i++) {
279 if (p(xs[i])) return true;
284export const allImpl = function (p, xs) {
286 for (var i = 0; i < len; i++) {
287 if (!p(xs[i])) return false;
292//------------------------------------------------------------------------------
293// Partial ---------------------------------------------------------------------
294//------------------------------------------------------------------------------
296export const unsafeIndexImpl = function (xs, n) {
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