General FizzBuzz: Difference between revisions

→‎JS ES6: Got it – so the other fold is better understood as a map, with intercalated '\n'. Simplified here.
(→‎ES6: Undo revision 272490 by Hout (talk) - `range`, w/ inclusive lower and exclusive upper bound, is how nearly every language and JS library does it, and `min` and `max` are more semantically correct)
(→‎JS ES6: Got it – so the other fold is better understood as a map, with intercalated '\n'. Simplified here.)
Line 1,340:
'use strict';
 
// rangeenumFrom :: Int -> Int -> [Int]
const rangeenumFromTo = (minm, maxn) =>
Array.from({
length: max1 + n - minm
}, (_, i) => minm + i);
 
 
const defaultRules = Object.freeze([
Line 1,354 ⟶ 1,355:
// fizzBuzz :: Int -> [[Int, String]] -> String
const fizzBuzz = (max, rules = defaultRules) =>
rangeenumFromTo(1, max + 1).reducemap((result, n) =>
resultrules.reduce((words, +[factor, (word]) =>
rules.reduce((words, [+ (n % factor, ? '' : word]), =>''
) || words + (n % factor ? '' : word), ''
) || .join('\n');
) + '\n', ''
).slice(0, -1)
 
console.log(fizzBuzz(20));
9,659

edits