Re: Why does for(x <- c; y = ...) yield {...} become c.map(x => (x, ...)).map((x,y) => {...})
Vlad Patryshev <[email protected]>
| Newsgroups | gmane.comp.lang.scala |
|---|---|
| Message-ID | <CAFhNTo95jU6GEAMocBnBZ87U22OgbUafhj-a2Ea=K_7OJn_3vg@mail.gmail.com> |
Just to have functoriality? (Otoh, unit and flatmap provide it) On Jan 24, 2015 11:07 AM, "Haoyi Li" <[email protected]> wrote: > I've been looking at http://docs.scala-lang.org/tutorials/FAQ/yield.html, > and found this > > for(x <- c; y = ...) yield {...} > > is translated into > > c.map(x => (x, ...)).map((x,y) => {...}) > > > This seems rather odd to me. Why not > > for(x <- c; y = ...) yield {...} > > is translated into > > c.flatMap{ x => val y = ...; {...} } > > > Isn't this basically the same, but much more straightforward? > > The reason I bumped into this is that I was trying to implement a bunch of > custom for-comprehension generators, and it kept asking for *.map*, and I > couldn't figure out why. These were pretty odd generators, e.g. a TryCatch > generator which you can put anywhere in your loop and which propagates a > Seq() back up if it catches an exception, and so don't really have > meaningful values to pass into *map* or *flatMap*s. > > -- > You received this message because you are subscribed to the Google Groups > "scala-language" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "scala-language" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. For more options, visit https://groups.google.com/d/optout.