generic filter through MonadPlus

It seems reasonable that filter should be generic. The underlying container need only support some kind of traversal, a zero element and a concatenation that eliminates zeroes. A `Monad` provides traversal via `join` and `fmap`; the remaining qualities are satisfied by `MonadPlus`. I've put together an implementation of `filter` for `MonadPlus` that works well enough for lists (though it presents somewhat greater obstacles to efficient compilation). I'm not sure whether `filter` is important enough to warrant its own generics package -- it's one of many collection operations that need to be generified. I'd like to know what folks think about the use of `MonadPlus` in this case. -- Jason Dusek |...an implementation of `filter`...| http://github.com/jsnx/genfil/blob/246026b975ec13587186681b7b346ae1e440d0c9/...

On May 22, 2009, at 4:13 AM, Jason Dusek wrote:
I'd like to know what folks think about the use of `MonadPlus` in this case.
The |guard| function is almost |filter|:
import Control.Monad ( MonadPlus, guard )
filter :: MonadPlus m => (a -> Bool) -> m a -> m a filter p m = do a <- m guard (p a) return a
Cheers, Sebastian -- Underestimating the novelty of the future is a time-honored tradition. (D.G.)
participants (2)
-
Jason Dusek
-
Sebastian Fischer