-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathindex.js
More file actions
40 lines (32 loc) · 1018 Bytes
/
index.js
File metadata and controls
40 lines (32 loc) · 1018 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
// Hey! Check out my super indepth blog post about what possessed me to make
// FizzBuzz this complicated! https://jonlaing.github.io/2017/11/07/over-engineered-fizz-buzz.html
'use strict';
// some convenience functions for readability
const when = (pred, f) => (n, text) => pred(n) ? f(text) : text;
const isMultiple = x => y => y % x === 0;
const concatString = s1 => s0 => s0 + s1;
const range = (min, max) =>
Array.from(Array(max - min + 1).keys(), x => x + min);
// implementing the Functor
const Fizzer = (n, text) => ({
map: f => Fizzer(n, f(n, text || '')),
done: () => text && text.length > 0 ? text : n // get out of the functor
});
const fizzIt = n =>
Fizzer(n)
.map(when(isMultiple(3), concatString("Fizz")))
.map(when(isMultiple(5), concatString("Buzz")))
.done();
const fizzBuzz = (min, max) =>
range(min, max)
.map(fizzIt)
.map(x => console.log(x));
module.exports = {
when,
isMultiple,
concatString,
range,
Fizzer,
fizzIt,
fizzBuzz
};