Skip to content

Instantly share code, notes, and snippets.

@micahbf
Created April 21, 2016 00:34
Show Gist options
  • Save micahbf/7ca9e9b90cf8288ff4ebc91db9b6fbc7 to your computer and use it in GitHub Desktop.
Save micahbf/7ca9e9b90cf8288ff4ebc91db9b6fbc7 to your computer and use it in GitHub Desktop.
transducers prezzo source
inc_map = function (ary) {
return ary.reduce(
function(result, input) {
return result.concat(input + 1);
}, []);
};
inc_map([1, 2, 3]);
map = function(transform, ary) {
return ary.reduce(function(result, input) {
return result.concat(transform(input));
}, []);
};
double = function(x) { return x * 2; };
map(double, [1, 2, 3]);
filter = function(predicate, ary) {
return ary.reduce(function(result, input) {
if(predicate(input)) {
return result.concat(input);
} else {
return result;
}
}, []);
}
is_even = function(x) { return x % 2 === 0; };
filter(is_even, [1, 2, 3, 4]);
map(double, filter(is_even, [1, 2, 3, 4]));
concat = function(a, b) { return a.concat([b]) };
concat([1, 2, 3], 4)
mapper = function(transform) {
return function(result, input) {
return concat(result, transform(input));
};
};
[1, 2, 3, 4].reduce(mapper(double), [])
filterer = function(predicate) {
return function(result, input) {
if(predicate(input)) {
return concat(result, input);
} else {
return result;
}
};
};
[1, 2, 3, 4].reduce(filterer(is_even), []);
identity = function(x) { return x; };
[1, 2, 3].reduce(mapper(identity), []);
filterer = function(predicate) {
return function(result, input) {
if(predicate(input)) {
return mapper(double)(result, input);
} else {
return result;
}
};
};
[1, 2, 3, 4].reduce(filterer(is_even), []);
mapping = function(transform) {
return function(reduce) {
return function(result, input) {
return reduce(result, transform(input));
};
};
};
filtering = function(predicate) {
return function(reduce) {
return function(result, input) {
if(predicate(input)) {
return reduce(result, input);
} else {
return result;
}
};
};
};
[1, 2, 3, 4].reduce(mapping(double)(concat), []);
filter_and_double = filtering(is_even)(mapping(double)(concat));
[1, 2, 3, 4].reduce(filter_and_double, []);
key_adder = function(result, input) { result[input] = true; return result };
filter_and_double = filtering(is_even)(mapping(double)(key_adder));
[1, 2, 3, 4].reduce(filter_and_double, {});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment