• nik9000@programming.dev
    link
    fedilink
    arrow-up
    2
    ·
    5 hours ago

    I’m not good at this but that’s never stopped me from making a fool of myself before.

    Iterators are monads because they have a flatMap on them. It takes each element and spits out a new iterator which is merged in to the result.

    Option is a monad too. Same reason. You can map the contents to another option. And you won’t get called if there’s nothing inside.

    Promises are monads too. You can map the result to another promise. The wrinkle here is that you don’t get to know when the map happen. Or it might not get called at all if the promise errors out.

    IO can be a monad because you can ask it for input and wait for the result. It’s just the same as a promise.

    See how these different things share a common behavior? That’s monad. Or, maybe it’s monoid. Names are hard and I’m busy making a fool of myself.

    Monads are nothing more than a useful abstraction. Haskell is famous for them because they couldn’t make Haskell do imperative stuff without them so they spread them all over the language.

    We all use them every day in regular programming. We just don’t think of them as a class of thing.