var map = fn => res => x => fn(x) ? res : null
I would rename this to something more like
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
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.
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.
I don't think you need this specialized function. Just pass the predicate and true-result into your
const when = pred => trueRes => x => pred(x) ? trueRes : null
const isDividedBy = dividend => divisor => divisor % dividend === 0
const mappers = [
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
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)
I like your currying approach.
Clojure has a function
juxt which does the same thing as
mappers.map(fn => fn(x)) but looks a little cleaner, being a separate function:
const fizzbuzz = x => 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.