Skip to content

Instantly share code, notes, and snippets.

@enfiskutensykkel
Last active December 31, 2017 14:07
Show Gist options
  • Save enfiskutensykkel/3066ec23cb9dbd34519795a6206f1246 to your computer and use it in GitHub Desktop.
Save enfiskutensykkel/3066ec23cb9dbd34519795a6206f1246 to your computer and use it in GitHub Desktop.
Functional FizzBuzz in JavaScript
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