Skip to content

Instantly share code, notes, and snippets.

@alexeyraspopov
Created May 6, 2014 13:23
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexeyraspopov/6387a694a7dea6aeaa80 to your computer and use it in GitHub Desktop.
Save alexeyraspopov/6387a694a7dea6aeaa80 to your computer and use it in GitHub Desktop.
function mapReduce(map, reduce, zero){
return function loop(from, to){
if(from > to){
return zero;
}
return reduce(map(from), loop(from + 1, to));
};
}
function mult(a, b){
return a * b;
}
function id(a){
return a;
}
function fact(n){
return mapReduce(id, mult, 1)(1, n);
}
fact(5);
function gcd(a, b){
return b === 0 ? a : gcd(a, b % a);
}
gcd(2, 4)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment