Created
April 21, 2016 00:34
-
-
Save micahbf/7ca9e9b90cf8288ff4ebc91db9b6fbc7 to your computer and use it in GitHub Desktop.
transducers prezzo source
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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