bind :: Monad m => (a -> m b) -> m a -> m b

Andreas Abel andreas.abel at ifi.lmu.de
Thu Dec 11 22:27:50 UTC 2014


Well, in this case I'd use the actual bind (>>=) and write

   view sectors >>= do
     traverse $ \ s ->
       case s of
         Sector{...} -> liftIO $ do
           sectorDrawWalls
           ... more code ...
         ... maybe more cases ...

Killed the long-ranging parentheses!  Haskell rules! ;-)

Cheers,
Andreas

On 11.12.2014 11:59, Oliver Charles wrote:
> Many people seem to be suggesting that this isn't a useful function to
> have, but I just found myself wanting it for a pattern that I write a
> lot. The code in question is:
>
>    traverse
>          (\s ->
>             case s of
>               Sector{..} ->
>                 liftIO (do sectorDrawWalls
>                            sectorDrawFloor
>                            sectorDrawCeiling))
>          =<< view sectors
>
> That is, I want to traverse some sort of structure, and the structure
> that I want to traverse itself comes from performing a monadic
> action. Imo, this would be more readable as
>
>    bind (traverse (\s ->
>                      case s of
>                        Sector{..} ->
>                          liftIO (do sectorDrawWalls
>                                     sectorDrawFloor
>                                     sectorDrawCeiling)))
>         (view sectors)
>
> Whatever we call it, I do feel it has use -- `traverse f =<< m` comes up
> a lot, but with a complex f, using =<< or >>= leads to less
> readability. Maybe I spend too much time with Chris. ;)
>
> -- ocharles
>
>
>
> _______________________________________________
> Libraries mailing list
> Libraries at haskell.org
> http://www.haskell.org/mailman/listinfo/libraries
>


-- 
Andreas Abel  <><      Du bist der geliebte Mensch.

Department of Computer Science and Engineering
Chalmers and Gothenburg University, Sweden

andreas.abel at gu.se
http://www2.tcs.ifi.lmu.de/~abel/


More information about the Libraries mailing list