Skip to content

Instantly share code, notes, and snippets.

@buzzdecafe
Last active August 29, 2015 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save buzzdecafe/db39615536be4c9b7c3f to your computer and use it in GitHub Desktop.
Save buzzdecafe/db39615536be4c9b7c3f to your computer and use it in GitHub Desktop.
// fizzbuzz ramda-style
var R = require('ramda');
R.forEach(R.cond(
[R.pipe(R.modulo(R.__, 15), R.eq(0)), function() { console.log('FizzBuzz'); }],
[R.pipe(R.modulo(R.__, 3), R.eq(0)), function() { console.log('Fizz'); }],
[R.pipe(R.modulo(R.__, 5), R.eq(0)), function() { console.log('Buzz'); }],
[R.alwaysTrue, function(){}]
), R.range(1, 101));
// with helpers
function log(x) {
return function() {
console.log(x);
};
}
function divisibleBy(x) {
return function(y) {
return y % x === 0;
};
}
function otherwise(op) {
return [R.alwaysTrue, op];
}
function noop(){}
R.forEach(R.cond(
[divisibleBy(15), log('FizzBuzz')],
[divisibleBy(3), log('Fizz')],
[divisibleBy(5), log('Buzz')],
otherwise(noop)
), R.range(1, 101));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment