Skip to content

Instantly share code, notes, and snippets.

@isnifer
Created September 4, 2015 12:48
Show Gist options
  • Save isnifer/22761d03ffafc1fb1554 to your computer and use it in GitHub Desktop.
Save isnifer/22761d03ffafc1fb1554 to your computer and use it in GitHub Desktop.
var a = [1, 2, 3, 4, 5];
function add (result, item, index) {
result[index] = item * 2;
return result;
}
function filter (result, item) {
if (item % 3 === 0) {
result.push(item);
}
return result;
}
function transducer () {
var reducers = [].slice.call(arguments, 0, -2);
var arrays = [].slice.call(arguments, -2);
var startPoint = arrays[0];
var source = arrays[1];
return reducers.reduce(function (result, reducer) {
return result.reduce(reducer, []);
}, source);
}
console.log(transducer(add, filter, [], a));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment