<div dir="ltr">Many also use the Control.Applicative syntax for this sort of thing (most types that have a Monad instance also have an Applicative instance):<div><br></div><div>(,) <$> ma <*> mb</div><div><br></div>
<div>which is equivalent to:</div><div><br></div><div>do</div><div> a <- ma</div><div> b <- mb</div><div> return (a, b)</div><div><br></div></div><div class="gmail_extra"><br><br><div class="gmail_quote">On Fri, Sep 6, 2013 at 2:04 PM, Emmanuel Touzery <span dir="ltr"><<a href="mailto:etouzery@gmail.com" target="_blank">etouzery@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr"><div><div><div><div>Hmm I've been looking some more. Turns out I'm pretty sure... What I've been looking for is liftMx.<br>
<br></div>In this case, liftM2.<br><br></div>liftM2 ... doA doB<br><br></div>
where "..." represents the rest of the computation as it did in the example snippet I sent in my first email.<br><br></div>In the case of the Maybe monad, "..." will be called with the "Just" values and only if both doA and doB are not Nothing.<br>
<br>and for "n" operations, there is "ap", I'll look at that one too.<span class="HOEnZb"><font color="#888888"><br><br>emmanuel<br></font></span></div><div class="HOEnZb"><div class="h5"><div class="gmail_extra">
<br><br><div class="gmail_quote">On Fri, Sep 6, 2013 at 10:43 PM, Emmanuel Touzery <span dir="ltr"><<a href="mailto:etouzery@gmail.com" target="_blank">etouzery@gmail.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><p>Thank you (and the others) for tips. I'll check the suggestions but yes sequence requires that all items have the same type so that sequence2 would work better but feels un-idiomatic.<br>
I needed such code several times in several contexts, so i assumed the answer would be obvious. Maybe i'm structuring my code the wrong way for haskell, i'll rethink those functions. Thanks for now!</p><span><font color="#888888">
<p>Emmanuel</p></font></span><div><div>
<div class="gmail_quote">On Sep 6, 2013 10:37 PM, "Nicholas Vanderweit" <<a href="mailto:nick.vanderweit@gmail.com" target="_blank">nick.vanderweit@gmail.com</a>> wrote:<br type="attribution"><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div dir="ltr">It seems like he wants something like sequence, but for tuples. Say:<div><br></div><div>sequence2 :: Monad m => (m a, m b) -> m (a, b)</div><div>sequence2 (ma, mb) = ma >>= \a -> mb >>= \b -> return (a, b)</div>
<div><br></div><div>But without knowing the use case it's hard to know whether or not this could be done simply with "sequence" and mapM*</div><div><br></div><div><br></div><div>Nick</div></div><div class="gmail_extra">
<br><br><div class="gmail_quote">On Fri, Sep 6, 2013 at 2:28 PM, Michael Steele <span dir="ltr"><<a href="mailto:mikesteele81@gmail.com" target="_blank">mikesteele81@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
The `sequence`, `mapM`, and `mapM_` functions may be what you are looking for.<br>
<br>
let allTogether = sequence [doA, doB, doC, doZ]<br>
<div>case allTogether of<br>
Nothing -> ...<br>
</div> Just as -> ...<br>
<br>
<br>
Use `maybe` and `fromMaybe` to avoid case statements:<br>
<br>
fromMaybe "failure!" $ do<br>
as <- sequence [doA, doB, doC, doZ]<br>
return $ "Success: " ++ show (length as)<br>
<br>
The `catMaybes` function can be used to execute each computation in<br>
the list even if some of them return nothing.<br>
<div><div><br>
On Fri, Sep 6, 2013 at 12:49 PM, Emmanuel Touzery <<a href="mailto:etouzery@gmail.com" target="_blank">etouzery@gmail.com</a>> wrote:<br>
> Hello,<br>
><br>
> I'm often using the Maybe monad to combine Maybe computations one after the<br>
> other without conditionals.<br>
><br>
> But I'm not sure how to do it, when I have several operations which return<br>
> Maybe and I want all the indiviual values in the end, not a combination of<br>
> the values.<br>
><br>
> Currently I do:<br>
><br>
> let allTogether = do<br>
> ma <- doA<br>
> mb <- doB<br>
> return (ma, mb)<br>
> case allTogether of<br>
> Nothing -> ...<br>
> Just (a, b) -> ...<br>
><br>
> This way in this case I have one case instead of two, however I'm sure<br>
> there must be a nicer way to achieve this result?<br>
><br>
> Thank you!<br>
><br>
> Emmanuel<br>
><br>
</div></div><div>> _______________________________________________<br>
> Beginners mailing list<br>
> <a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
> <a href="http://www.haskell.org/mailman/listinfo/beginners" target="_blank">http://www.haskell.org/mailman/listinfo/beginners</a><br>
><br>
<br>
<br>
<br>
</div><span><font color="#888888">--<br>
-- Michael Steele<br>
</font></span><div><div><br>
_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/beginners" target="_blank">http://www.haskell.org/mailman/listinfo/beginners</a><br>
</div></div></blockquote></div><br></div>
<br>_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org" target="_blank">Beginners@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/beginners" target="_blank">http://www.haskell.org/mailman/listinfo/beginners</a><br>
<br></blockquote></div>
</div></div></blockquote></div><br></div>
</div></div><br>_______________________________________________<br>
Beginners mailing list<br>
<a href="mailto:Beginners@haskell.org">Beginners@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/beginners" target="_blank">http://www.haskell.org/mailman/listinfo/beginners</a><br>
<br></blockquote></div><br></div>