: sometimes you gotta manipulate and shuffle around some monads and functors/applicatives like a 4D Russian nesting doll.

`traverse` is a function that really shows that off. Picture related. Using `traverse` with `Either` and `Maybe` while inside a parser monad.

What's happening in the code:

`traverse` is a handy function, especially when used with `Either` to escape failing inside a Maybe monad.

@hyperrealgopher And this is why I prefer do notation over applicative.

But at least the body of that case can be extracted from the expression.

And with for instead of traverse `makeRenderAs` could be a \case in a final position.

(o .:? "renderAs" >>= renderAsP)

renderAsP = traverse \case
String "menu" ->
pure GopherTypeMenu
wtf ->
unexpected wtf

It is sometimes beneficial to try `for` instead of `traverse`.

@dpwiz I didn't want to have `makeRenderAs` handle the type plumbing of the `Maybe`, was my rationale. That is an interesting way of handling the issue, though (putting everything in `traverse`). Thank you for sharing!

@hyperrealgopher I regularly use Foldable, Traversable and other well-known instances of Maybe and Either. It is liberating to think in instance methods instead of making a fuss about some stuff being a Monad.

Sign in to participate in the conversation

Fosstodon is an English speaking Mastodon instance that is open to anyone who is interested in technology; particularly free & open source software.