[Haskell-cafe] nested maybes

J. Garrett Morris trevion at gmail.com
Sun Feb 4 15:46:57 EST 2007


On 2/4/07, Udo Stenzel <u.stenzel at web.de> wrote:
> J. Garrett Morris wrote:
> Small improvement (Data.Maybe is underappreciated):
>
> > exists str wmap = isJust exists'
> >    where exists' =
> >              do x <- Map.lookup (sort str) wmap
> >                 find (== str) (snd x)

This is true.  Some time ago I swore off the use of fromRight and
fromLeft in favor of maybe, and have been forgetting about the other
functions in Data.Maybe ever since.

> and maybe another improvement, though this is dependent on your tastes:
>
> > exists s wmap = isJust $ Map.lookup (sort s) wmap >>= find (== s) . snd

If you're going to write it all on one line, I prefer to keep things
going the same direction:

exists s wmap = isJust $ find (==s) . snd =<< Map.lookup (sort s) wmap

Normally, from there I would be tempted to look for a points-free
implementation, but in this case I have a strong suspicion that would
simply be unreadable.

 /g

-- 
It is myself I have never met, whose face is pasted on the underside of my mind.


More information about the Haskell-Cafe mailing list