Skip to content

Instantly share code, notes, and snippets.

@MeetMartin
Last active April 22, 2024 06:33
Show Gist options
  • Save MeetMartin/db1530642a0279f95ab9e64a370dfb3d to your computer and use it in GitHub Desktop.
Save MeetMartin/db1530642a0279f95ab9e64a370dfb3d to your computer and use it in GitHub Desktop.
JavaScript Monad in under 60 seconds.
// 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();
@chasm
Copy link

chasm commented Mar 17, 2024

You have a stray "s" on line 10, col 3.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment