Skip to content

Instantly share code, notes, and snippets.

@acquitelol
Created April 21, 2025 02:00
Show Gist options
  • Select an option

  • Save acquitelol/d57fcca15ccc14dccc178d2da16c1c12 to your computer and use it in GitHub Desktop.

Select an option

Save acquitelol/d57fcca15ccc14dccc178d2da16c1c12 to your computer and use it in GitHub Desktop.
Fizzbuzz but in purely functional JavaScript
const pair = a => b => f => f (a) (b)
const head = p => p (a => b => a)
const tail = p => p (a => b => b)
const range = low => high =>
low > high ? null :
pair (low) (range (low + 1) (high))
const map = f => xs =>
xs === null ? null :
pair (f (head (xs))) (map (f) (tail (xs)))
const fizzBuzz = x =>
x % 15 === 0 ? "FizzBuzz" :
x % 3 === 0 ? "Fizz" :
x % 5 === 0 ? "Buzz" :
x
// To make it printable in the console
const listToArray = xs =>
xs === null ? [] :
[head (xs), ...listToArray (tail (xs))]
console.log(listToArray (map (fizzBuzz) (range (1) (20))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment