My mind wandered after browsing Alain Marty's recent post on the derivation of all things computationally useful from lambda. lambdaway
The appendix offered two dozen related links wherein I chose "whiteboard problems" that included Fizz Buzz written in lambda calculus. post
I wondered how I would do in a job interview that included live coding this only slightly tricky problem.
for (let i = 1; i <= 100; i++) { let words = [] if (!(i%3)) words.push('fizz') if (!(i%5)) words.push('buzz') if (words.length) console.log(words.join(' ')) else console.log(i) }
I made two mistakes. I guess wrong about the operator precedence between not and mod. And I forgot to convert the empty array to something recognizably false. I looked up published implementations fizz buzz to see what I might have missed. rosettacode
With functional abstraction and lazy-or I was able to reduce fizz buzz to a shorter and possibly more readable four lines using language features I use every day.
for (let i = 1; i <= 100; i++) { const div = (n, word) => i%n ? '' : word console.log(div(3,'fizz')+div(5,'buzz') || i) }