Last active
April 22, 2024 06:33
-
-
Save MeetMartin/db1530642a0279f95ab9e64a370dfb3d to your computer and use it in GitHub Desktop.
JavaScript Monad in under 60 seconds.
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
// JavaScript functional programming | |
// Monad under 60 seconds | |
const Functor = { | |
of: value => ({ | |
value: value, | |
inspect: () => console.log(`Functor(${value})`), | |
map: fn => Functor.of(fn(value)) | |
}) | |
}; | |
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
You have a stray "s" on line 10, col 3.