Skip to content

Commit c87fdee

Browse files
authored
Merge pull request #127 from matthewleon/nonempty
NonEmpty arrays
2 parents ebcfcc3 + badad04 commit c87fdee

File tree

5 files changed

+860
-1
lines changed

5 files changed

+860
-1
lines changed

bower.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@
1616
"package.json"
1717
],
1818
"dependencies": {
19-
"purescript-foldable-traversable": "^3.0.0",
19+
"purescript-foldable-traversable": "^3.3.0",
2020
"purescript-nonempty": "^4.0.0",
2121
"purescript-partial": "^1.2.0",
2222
"purescript-st": "^3.0.0",

src/Data/Array/NonEmpty.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
"use strict";
2+
3+
exports.fold1Impl = function (f) {
4+
return function (xs) {
5+
var acc = xs[0];
6+
var len = xs.length;
7+
for (var i = 1; i < len; i++) {
8+
acc = f(acc)(xs[i]);
9+
}
10+
return acc;
11+
};
12+
};
13+
14+
exports.traverse1Impl = function () {
15+
function Cont(fn) {
16+
this.fn = fn;
17+
}
18+
19+
var emptyList = {};
20+
21+
var ConsCell = function (head, tail) {
22+
this.head = head;
23+
this.tail = tail;
24+
};
25+
26+
function finalCell(head) {
27+
return new ConsCell(head, emptyList);
28+
}
29+
30+
function consList(x) {
31+
return function (xs) {
32+
return new ConsCell(x, xs);
33+
};
34+
}
35+
36+
function listToArray(list) {
37+
var arr = [];
38+
var xs = list;
39+
while (xs !== emptyList) {
40+
arr.push(xs.head);
41+
xs = xs.tail;
42+
}
43+
return arr;
44+
}
45+
46+
return function (apply) {
47+
return function (map) {
48+
return function (f) {
49+
var buildFrom = function (x, ys) {
50+
return apply(map(consList)(f(x)))(ys);
51+
};
52+
53+
var go = function (acc, currentLen, xs) {
54+
if (currentLen === 0) {
55+
return acc;
56+
} else {
57+
var last = xs[currentLen - 1];
58+
return new Cont(function () {
59+
var built = go(buildFrom(last, acc), currentLen - 1, xs);
60+
return built;
61+
});
62+
}
63+
};
64+
65+
return function (array) {
66+
var acc = map(finalCell)(f(array[array.length - 1]));
67+
var result = go(acc, array.length - 1, array);
68+
while (result instanceof Cont) {
69+
result = result.fn();
70+
}
71+
72+
return map(listToArray)(result);
73+
};
74+
};
75+
};
76+
};
77+
}();

0 commit comments

Comments
 (0)