Skip to content

Instantly share code, notes, and snippets.

@abiodun0
Created July 16, 2018 06:02
Show Gist options
  • Save abiodun0/9eff1c9b7e2a3abe5dbc1e1b0146d9f3 to your computer and use it in GitHub Desktop.
Save abiodun0/9eff1c9b7e2a3abe5dbc1e1b0146d9f3 to your computer and use it in GitHub Desktop.
gist for first blog post
const makeMonad = (prevData, prevLog) => fn => {
const newData = fn(prevData).data;
const newLog = prevLog + fn(prevData).log;
return { data: newData,
log: newLog,
bind: makeMonad(newData, newLog)
};
};
const pure = x => ({
data: x,
log: "",
bind: makeMonad(x, "")
})
const withLog = (x, stuffToWrite) => pure(x).bind(x => ({data: x, log: stuffToWrite}))
const makeFizzBuzzQuxxPiece = (test, thingsToWrite) => (x) => test(x) ? withLog(x, thingsToWrite) : pure(x)
const theFizz = makeFizzBuzzQuxxPiece(x => x % 3 === 0, "Fizz")
const theBuzz = makeFizzBuzzQuxxPiece(x => x % 5 === 0, "Buzz")
const theQuxx = makeFizzBuzzQuxxPiece(x => x % 7 === 0, "Quxx")
const range = (low, high) => Array(high - low + 1).fill(0).map((_, i) => i + low);
const thePrimer = makeFizzBuzzQuxxPiece(x => x > 1 && range(2, x-1)
.every(divisor => x % divisor !== 0), "prime")
const fibExtract = ({data, log}) => log ? log: data;
range(1, 1000).forEach(x => {
console.log(fibExtract(pure(x)
.bind(thePrimer)
.bind(theFizz)
.bind(theBuzz)
.bind(theQuxx)
))})
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment