4 ms·
Maybe is already a monad.
by Munksgaard 9y ago
Maybe is already a monad.
- dllthomas 9y agoAs I read it, the question was "I have `... -> Maybe a` and I want to use it in a context where I am chaining together some unspecified monad. Will that be awkward?" And the monad interface doesn't help us with that. But the answer is that you have a Maybe, you can do any Maybe thing with it.
- tom_mellior 9y agoYes. That would allow me to use a getData returning a Maybe in the Maybe monad. But I wouldn't be able to use it in the list monad, or the others mentioned in the article.
- dllthomas 9y agoYou could use it in code that's operating in the list monad, you just wouldn't be able to stitch the computation in using the monad interface. But you could run it and get a Maybe back, and look at the Maybe, and turn it into something you can stitch in. `maybe (fail "got back a Nothing") pure` is probably the most generic way, but it may or may not be what you want.
- marcosdumay 9y agoWell, yes. You will need to convert your type into something else to use in another monad. As an example, it's common to track failure with an `Either Text` monad, but functions that can fail on a single way usually have an `Maybe a` return type. That means you will probably have an `Text -> Maybe a -> Either Text a` function around and write code like this: toEither "Error message" $ functionInMaybe a b
- leshow 9y agoYou could use the getData function just fine from anywhere. It's just that you wouldn't be able to use both the list monad instance and the maybe monad instance at the same time without a library like mtl.