monad-loops
monad-loops copied to clipboard
Provide breakable loops
This can be done with EitherT or EitherRT (or similar internal functionality) :
loop f = runEitherT (forever f) >>= (\(Left x) -> return x)
break = left
For this purpose I actually really liked the "loop-while" library on Hackage - it appears not to build any more, but as simple as it is I suspect it just needs its dependency versions loosened. It provides a very nice monad transformer (LoopWhileT) that does exactly this, and gives more informative types while it's at it. The newer loops package looks more complex but is probably also a good alternative.