ekmett/lens — pull request #489 by danidiaz (2014-10-10 17:24:48+00:00)
Title: Monad, MonadReader and MonadPlus instances for ReifiedMonadicFold

Example with the Monad instance:

{-# LANGUAGE FlexibleContexts #-}

import Control.Lens
import Control.Monad

widgets :: [(Char,Int,Int)]
widgets = [ ('a',10,100), ('a',77,80), ('b',12,50), ('b',70,120) ]

bsInsideAs :: MonadicFold IO [(Char,Int,Int)] (Char,Int,Int)
bsInsideAs = runMonadicFold $ do
(c,x1,x2) <- MonadicFold $ folded . act (\x -> print x >> return x)
guard $ c == 'a'
r@(c',x1',x2') <- MonadicFold $ folded
guard $ c' == 'b'
guard $ x1' >= x1 && x2' <= x2
return r

Executing the fold:

*Main> widgets ^!! bsInsideAs
('a',10,100)
('a',77,80)
('b',12,50)
('b',70,120)
[('b',12,50)]