This rewrite changes the order of execution. The old version did all of the putDirs and then all of the renameFile calls. The new one interleaves putDirs & renameFile calls. In general, ">>" is not commutative, though in this case you may be as happy with either order. - Conal
<br><br><div><span class="gmail_quote">On 12/12/06, <b class="gmail_sendername">Bryan Burgers</b> <<a href="mailto:bryan.burgers@gmail.com">bryan.burgers@gmail.com</a>> wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
On 12/12/06, Louis J Scoras <<a href="mailto:louis.j.scoras@gmail.com">louis.j.scoras@gmail.com</a>> wrote:<br>> I have some IO actions that I want to map over a list of pairs --<br>> these are just directory names and their down-cased versions. It
<br>> wasn't difficult to actually get the right behavior by just doing mapM<br>> twice.<br>><br>> -- putDirs just outputs something like "mv fst snd"<br>> mapM_ putDirs pairs<br>> mapM_ (uncurry renameFile) pairs
<br>><br>> This bothered me though, because I suspected that this could be done<br>> in one pass. Naively I proceeded to this.<br>><br>> mapM_ (putDirs >> (uncurry renameFile)) pairs<br>><br>> Which didn't work. I thought about it a little more before realizing
<br>> that putDirs wouldn't get any parameters this way: I needed some way<br>> to distribute the pair over both operations. Here's the higher-order<br>> function I needed:<br>><br>> foo h f g i = h (f i) (g i)
<br>><br>> which could then be curried and we get:<br>><br>> mapM_ (foo (>>) putDirs $ uncurry renameFile) pairs<br>><br>> Works great. So my question: is there a established name for foo?<br>
> What about foo partially applied to (>>)? This was a fun exercise,<br>> but I'd like to use the standard implementations if they exist.<br><br>Before we get too far down the obfuscation road, I'd like to offer
<br>what I think is more readable than a liftM2 solution:<br><br> mapM_ (\p -> putDirs p >> uncurry renameFile p) pairs<br><br>I haven't tested it, but I hope that does the same thing. To me, this<br>explicitely shows what each is doing, moreso than with a point-free
<br>'foo' combinator.<br><br>The way my mind worked to get to this solution:<br><br> mapM_ putDirs pairs<br> mapM_ (uncurry renameFile) pairs<br><br> ==><br><br> mapM_ (\p -> putDirs p) pairs<br> mapM_ (\p -> uncurry renameFile p) pairs
<br><br> ==><br><br> mapM_ (\p -> putDirs p >> uncurry renameFile p) pairs<br><br>Is that a reasonable solution?<br><br>Bryan Burgers<br>_______________________________________________<br>Haskell-Cafe mailing list
<br><a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br><a href="http://www.haskell.org/mailman/listinfo/haskell-cafe">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br></blockquote></div><br>