Non-trivial functions that operate on any monad
- by Strilanc
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?