3 ms·
I don't use monads to split out effects. I use monads because they're a design pattern that leads to usable, reasonably well-factored APIs, like LINQ and promis
by barrkel 12y ago
I don't use monads to split out effects. I use monads because they're a design pattern that leads to usable, reasonably well-factored APIs, like LINQ and promises.
Splitting out effects is a different argument. Monads can be (and are in Haskell) used to evaluate a stateful program from stateless components, but they're only an implementation detail. But anything with the right shape - stateless functional holes inserted into the stream between input and output - would look monadic from the right angle, so I'm not sure there's any escape from that.
Similarly for using an Option monads for exceptions - and that's what the Option monad (or closer, Either) ends up approximating, the entire computation evaluates to None once a bad value is hit, much like throwing an exception causes the whole computation to evaluate to the exception at the point of the catch.
So IMO this article isn't really about monads at all.
- benjaminy 12y agoYour first paragraph confuses me. Most APIs that I know for promises/futures are effectful. For example, there is a procedure for checking whether a promise has been fulfilled yet or not. The return value of this function obviously depends implicitly on the state of the promise object. Making this abstraction purely functional requires making the interfaces of these API functions more complicated. And the monadic style helps reduce that complexity. My point is that promises seem very much connected with effects to me, and I'm curious why you draw a line between them? (I understand that there are implementations of the monad abstraction that are not connected with "state" in the conventional sense.)