-
-
Save jackyscript/62f026c54dddd6e7648382e90edcda73 to your computer and use it in GitHub Desktop.
JavaScript Monad in under 60 seconds.
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
// JavaScript functional programming | |
// Monad under 60 seconds | |
const Functor = { | |
of: value => ({ | |
value: value, | |
inspect: () => console.log(`Functor(${value})`), | |
map: fn => Functor.of(fn(value)) | |
}) | |
};s | |
const Monad = { | |
of: value => ({ | |
value: value, | |
inspect: () => console.log(`Monad(${value})`), | |
map: fn => Monad.of(fn(value)), | |
flatMap: fn => fn(value) | |
}) | |
}; | |
Monad.of(5).flatMap(a => Monad.of(a + 1)).inspect(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment