FizzBuzz functional implementation I was playing around with ES6 arrow functions, currying and partial application and I decided to try a functional implementation of the FizzBuzz problem:

function* range(start, stop) {
  for (var i = start; i &lt; stop; i++)
    yield i
}

var map = fn =&gt; res =&gt; x =&gt; fn(x) ? res : null
var isDividedBy = dividend =&gt; divisor =&gt; divisor % dividend === 0
var mapDividedBy = x =&gt; map(isDividedBy(x))
var mappers = [
  mapDividedBy(3)('Fizz'),
  mapDividedBy(5)('Buzz')
]
var applyMappers = mappers =&gt; x =&gt; mappers.map(fn =&gt; fn(x)).join('') || x

var result = Array.from(range(1, 100)).map(applyMappers(mappers))

console.log(result)


My main focus was to be able to easily add any other case like "print Zazz for numbers lower than 10" by simply adding map(x =&gt; x &lt; 10)('Zazz') to the mappers array.

What do you think?
map vs if

var map = fn =&gt; res =&gt; x =&gt; fn(x) ? res : null


I would rename this to something more like ifThen or when -- calling this function map is confusing, to me at least, given the normal usage of map. This is an if where the else is always null.

In a larger program you may find that providing the true/false results as functions instead of constants would be more powerful, but here that would just add noise.

const

Most of your vars could be consts. Doesn't make much difference here but since you're deliberately being functional const would help declare your intent.

without mapDividedBy

I don't think you need this specialized function. Just pass the predicate and true-result into your when/if:

const when = pred =&gt; trueRes =&gt; x =&gt; pred(x) ? trueRes : null
const isDividedBy = dividend =&gt; divisor =&gt; divisor % dividend === 0
const mappers = [
  when(isDividedBy(3))('Fizz'),
  when(isDividedBy(5))('Buzz'),
]


naming

I'm not sure mappers is the most descriptive name. I can't think of anything better at present, so that's not very helpful, but any (pure) function is just a "mapper" from inputs to output(s). Maybe tests? Hmm.

In any event, since this is a FizzBuzz I suggest providing a final function named as such, for convenience and readability:

const fizzbuzz = applyMappers(mappers)
const result = Array.from(range(1, 100)).map(fizzbuzz)


misc

I like your currying approach.

Clojure has a function juxt which does the same thing as mappers.map(fn =&gt; fn(x)) but looks a little cleaner, being a separate function:

const fizzbuzz = x =&gt; juxt(mappers)(x).join('') || x


But whether this is more readable or more obscure depends on whether you know Clojure, so I've left this point to the end.