Skip to content

Instantly share code, notes, and snippets.

@TGOlson
Created March 8, 2015 08:28
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 TGOlson/041679d57be79933bb2c to your computer and use it in GitHub Desktop.
Save TGOlson/041679d57be79933bb2c to your computer and use it in GitHub Desktop.
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

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