Non-trivial functions that operate on any monad

Posted by Strilanc on Programmers See other posts from Programmers or by Strilanc
Published on 2013-11-01T03:03:19Z Indexed on 2013/11/01 4:15 UTC
Read the original article Hit count: 247

I'm looking for examples of interesting methods that take an arbitrary monad and do something useful with it.

Monads are extremely general, so methods that operate on monads are widely applicable. On the other hand, methods I know of that can apply to any monad tend to be... really, really trivial. Barely worth extracting into a function.

Here's a really boring example: joinTwice. It just flattens an m m m t into an m t:

join n = n >>= id
joinTwice n = (join . join) n

main = print (joinTwice [[[1],[2, 3]], [[4]]])
-- prints [1,2,3,4]

The only non-trivial method for monads that I know of is bindFold (see my answer below). Are there more?

© Programmers or respective owner

Related posts about functional-programming

Related posts about examples