General FizzBuzz: Difference between revisions

→‎ES6: Undo revision 272502 by Hout (talk) reduce used over map/join to reduce iterations, but fair. enumFromTo is uncommon, unclear (enums?), and semantically incorrect (through, not to)
(→‎JS ES6: Got it – so the other fold is better understood as a map, with intercalated '\n'. Simplified here.)
(→‎ES6: Undo revision 272502 by Hout (talk) reduce used over map/join to reduce iterations, but fair. enumFromTo is uncommon, unclear (enums?), and semantically incorrect (through, not to))
Line 1,340:
'use strict';
 
// enumFromrange :: Int -> Int -> [Int]
const enumFromTorange = (mmin, nmax) =>
Array.from({
length: 1 + nmax - mmin
}, (_, i) => mmin + i);
 
 
const defaultRules = Object.freeze([
Line 1,355 ⟶ 1,354:
// fizzBuzz :: Int -> [[Int, String]] -> String
const fizzBuzz = (max, rules = defaultRules) =>
enumFromTorange(1, max + 1).mapreduce((result, n) =>
rules.reduce((words,result [factor,+ word]) =>(
rules.reduce((words, + (n % [factor ? '' :, word]), ''=>
) || words + (n % factor ? '' : word), ''
).join('\ || n');
) + '\n', ''
).slice(0, -1)
 
console.log(fizzBuzz(20));