Scientific Calculator
silentmatt/functional-examples
Example implementations of some the built-in functional... functions.
Tagged:
Map = function(f, a) {
var r = [];
var i = 0;
while (i < len a) -> (
r[i] = f(a[i]);
i += 1
);
r
};
Foldl = function(f, s, a) {
var i = 0;
(len @ < 3) and (a = s; s = a[0]; i = 1);
while (i < len a) -> (
s = f(s, a[i]);
i += 1
);
s
};
Foldr = function(f, s, a) {
var i;
(len @ < 3) ? (a = s; i = len a - 1; s = a[i]) : (i = len a);
while (i) -> (
i -= 1;
s = f(a[i], s)
);
s
};
Filter = function(p, a) {
var i = 0;
var r = [];
var x;
while (i < len a) -> (
x = a[i];
p(x) and (r[len r] = x);
i += 1
);
r
};
Every = function(p, a) {
apply(:and, map(p, a))
};
Some = function(p, a) {
apply(:or, map(p, a))
};
0 Comments
Sign in to leave a comment