Created
April 21, 2025 02:00
-
-
Save acquitelol/d57fcca15ccc14dccc178d2da16c1c12 to your computer and use it in GitHub Desktop.
Fizzbuzz but in purely functional JavaScript
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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