It feels like anything is mowed down on the internet. I’ve been a dev for a long time too, and I never feel sure when I chose a stack for a new toy project (in my day job I rarely get to chose, so that’s a non issue there)

Haskell, because nobody knows haskell

Unfortunately, no one can be told what a monad is. You have to see it for yourself (then you won’t be able to explain it to anyone)

The Codeless Code : Monolith http://thecodelesscode.com/case/143

On the topic of Monads: http://thecodelesscode.com/topics/monads (though the other one really needs you to read the rest of the site to get a better understanding of the monk Djishin and the Java Master Banzen … and remember there’s css hidden topics along with image mouse overs… in case you read the first hundred and then realize that there’s more to each one and have to go back and read them all again).

Ohhhh, this site is a great find. Exploring all the articles right now. Thanks!

Isn’t a monad just a monoid in the category of endofunctors?

The problem is people constantly try to explain it using some kind of real world comparison to make it easier to visualize (“it’s a value in a context”, “it encodes side effects”, “it’s a way to do I/O”, “it’s just flatmap”, “it’s a burrito”), when all it really is is an abstraction. A very, very general abstraction that still turns out to be really useful, which is why we gave it the cryptic name “monad” because it’s difficult to find a name for it that can be linked to something concrete simply because of how abstract it is. It really is just an interface with 2 key functions: (for a monad M)

- wrap: (x: T) => M[T] // wraps a value
- bind: (f: (y: T) => M[U], x: M[T]) => M[U] // unwraps the value in x, potentially doing something with it in the process, passes it to f which should return a wrapped value again somehow, and returns what f returns

Anything that you can possibly find a set of functions for that fits this interface and adheres to the rules described by someone else in this thread is a monad. And it’s useful because, just like any other abstraction, if you identify that this pattern can apply to your type M and you implement the interface, then suddenly a ton of operations that work for any monad will also work for your type. One example is the coroutine transformation (async/await) that is an extremely popular solution to the Node.JS “callback hell” problem that used to exist, and which we call do-notation in Haskell:

// instead of
const getPostAuthorName = foo => getPost(foo).then(post => getUser(post.authorId)).then(user => user.username)

// you can do this
const getPostAuthorName = async foo => {
  const post = await getPost(foo)
  const user = await getUser(post.authorId)
  return user.username
}

This is a transformation you can actually do with any monad. In this case Promise.resolve is an implementation of wrap, and then is an implementation of bind (more or less, it slightly degenerate due to accepting unwrapped return values from f). Sadly it was not implemented generally in JS and they only implemented the transform specifically for Promises. It’s sad because many people say they hate monads because they’re complex, but then heap praise on Promises and async/await which is just one limited implementation of a monad. You may have noticed that generators with yield syntax are very similar to async/await. That’s because it’s the exact same transformation for another specific monad, namely generators. List comprehensions are another common implementation where this transform is useful:

// instead of
const results = []
for (const x of xs) {
  for (const y of ys) {
    results.push({ x, y })
  }
}

// you could have
const results = do {
  const x = yield xs
  const y = yield ys
  return wrap({ x, y })
}

Another (slightly broken) implementation of monads and the coroutine transform people use without knowing it is “hooks” in the React framework (though they refuse to admit it in order to not confuse beginners).

Fuck… I actually just wanted to write a short reply to the parent comment and devolved into writing a Monad Tutorial…

@alr@programming.dev
link
fedilink
1
edit-2
1Y

If you use JavaScript, you’ve probably seen a monad, since Promise is a monad. Unit is Promise.resolve(), bind is Promise.then(). As required, Promise.resolve(x).then(y) === y(x) (unit forms a left identity of bind), y.then(Promise.resolve) === y (unit forms a right identity of bind), and x.then(y.then(z)) === x.then(y).then(z) (bind is essentially associative).

You even have the equivalent of Haskell’s fancy do-notation (a form of syntactic sugar to save writing unit and bind all over the place) in the form of async/await. It’s just not generalized the way it is in Haskell.

JackbyDev
link
fedilink
English
31Y

Monads 👁️👄👁️

Create a post

Welcome to the main community in programming.dev! Feel free to post anything relating to programming here!

Cross posting is strongly encouraged in the instance. If you feel your post or another person’s post makes sense in another community cross post into it.

Hope you enjoy the instance!

Rules

Rules

  • Follow the programming.dev instance rules
  • Keep content related to programming in some way
  • If you’re posting long videos try to add in some form of tldr for those who don’t want to watch videos

Wormhole

Follow the wormhole through a path of communities !webdev@programming.dev



  • 1 user online
  • 1 user / day
  • 1 user / week
  • 1 user / month
  • 1 user / 6 months
  • 1 subscriber
  • 1.21K Posts
  • 17.8K Comments
  • Modlog