Last active
December 31, 2017 14:07
-
-
Save enfiskutensykkel/3066ec23cb9dbd34519795a6206f1246 to your computer and use it in GitHub Desktop.
Functional FizzBuzz in JavaScript
This file contains 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
Array.prototype.append = function (element) { | |
this.push(element); | |
return this; | |
}; | |
range = (length) => { | |
return (function range() { | |
if (this.length < length) { | |
this.push(this.length); | |
range.call(this); | |
} | |
return this; | |
}).call([]); | |
}; | |
shiftGenerator = (list => () => { | |
list.push(list.shift()); | |
return list[list.length-1]; | |
}); | |
select = (a, b) => a ? a : b; | |
printLine = (i) => print(i); | |
fizz = shiftGenerator(Array(2).fill("").append("fizz")); | |
buzz = shiftGenerator(Array(4).fill("").append("buzz")); | |
printLine = (i) => print(i); | |
range(100) | |
.map((i) => i + 1) | |
.map((i) => select(fizz() + buzz(), i)) | |
.forEach(printLine); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment