Created
August 3, 2017 16:31
-
-
Save oliverjam/33962a102a6a46979502935fe5c0e732 to your computer and use it in GitHub Desktop.
Functional Fun - Monad Madness created by oliverjam - https://repl.it/Jvn3/0
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
// inc :: Number -> (Number,String) | |
const inc = x => y => [y + x, `${inc.name}${x} was called.`]; | |
const inc1 = inc(1); | |
// dec :: Number -> (Number,String) | |
const dec = x => y => [y - x, `${dec.name}${x} was called.`]; | |
const dec1 = dec(1); | |
// unit :: Number -> (Number,String) | |
const unit = x => [x, '']; | |
// Calls provided fn on num in tuple, returns tuple of result and both strings combined | |
// e.g. `bind([3, 'yoyoyo'], inc1)` -> [ 4, 'yoyoyo inc1 was called.' ] | |
// bind :: (Number,String) -> (Number -> (Number,String)) -> (Number,String) | |
const bind = (tuple, fn) => [fn(tuple[0])[0], tuple[1] + ' ' + fn(tuple[0])[1]]; | |
// pipe :: [a] -> [a -> [b]] -> [b] | |
const pipe = (x, ...functions) => functions.reduce((acc, fn) => bind(acc, fn), x); | |
const double = x => [x * 2, `${double.name} was called.`]; | |
bind([3, 'yoyoyo'], inc1); | |
// pipe(unit(7), inc1, double, dec1); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment