Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Point-free JavaScript FizzBuzz Kata
var R = require('ramda');
var factorOf = R.curryN(2, R.compose(R.eq(0), R.flip(R.modulo)));
var getFizzBuzz = R.cond(
[R.and(factorOf(3), factorOf(5)), R.always('FizzBuzz')],
[factorOf(3), R.always('Fizz')],
[factorOf(5), R.always('Buzz')],
[R.T, R.identity]
);
var fizzBuzz = R.compose(R.map(getFizzBuzz), R.times(R.inc));
fizzBuzz(15);
// => [ 1, 2, 'Fizz', 4, 'Buzz', 'Fizz', 7, 8, 'Fizz', 'Buzz', 11, 'Fizz', 13, 14, 'FizzBuzz' ]
@StevenACoffman
Copy link

StevenACoffman commented Aug 9, 2015

For those who may be confused by the errors they receive running this. Ramda (as of 0.17.1) has had some API changes:

  • R.eq is now R.equals
  • R.cond no longer variadic, and instead is unary so it takes an array of arrays so you need to add extra square brackets inside the cond e.g. (R.cond([[R.T,R.idenity]]);

I tweaked the code to work here

Thanks @TGOlson for a beautiful implementation.
Btw, I'm curious why not just:

 var factorOf = R.compose(R.equals(0), R.flip(R.modulo));

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment