Why does for(x <- c; y = ...) yield {...} become c.map(x => (x, ...)).map((x,y) => {...})

Haoyi Li <[email protected]>
Newsgroups gmane.comp.lang.scala
Message-ID <CALruUQKHrbAULetcaC2_ban8LtKJGnjC=GTsEorgu2HXUnJWGw@mail.gmail.com>
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.
lmpx.com only provides a reader for public news (NNTP) servers. It is not affiliated with the servers or forums shown here and is not responsible for the content of articles, which is written by their respective authors.