Re: Why does for(x <- c; y = ...) yield {...} become c.map(x => (x, ...)).map((x,y) => {...})
Som Snytt <[email protected]>
| Newsgroups | gmane.comp.lang.scala |
|---|---|
| Message-ID | <CAPaMnL6xhhCoZ9wXjXcGubZubRzzuv5P1xtMS7w+zy93yjCf0g@mail.gmail.com> |
This SO answer is the only time I came close to touching a raw nerve in
terms of upvotes.
http://stackoverflow.com/q/17377526/1296806
It's due to the translation of mid-stream assignments.
Of course, -Xprint:typer is clarifying.
scala> for (x <- List(1) ; y = 2 * x) yield y
val res0: List[Int] = immutable.this.List.apply[Int](1).map[(Int, Int),
List[(Int, Int)]](((x: Int) => {
val y: Int = 2.*(x);
scala.Tuple2.apply[Int, Int](x, y)
}))(immutable.this.List.canBuildFrom[(Int, Int)]).map[Int,
List[Int]](((x$1: (Int, Int)) => (x$1: (Int, Int) @unchecked) match {
case (_1: Int, _2: Int)(Int, Int)((x @ _), (y @ _)) => y
}))(immutable.this.List.canBuildFrom[Int]);
Actually, you get a clearer picture from -Xprint:parser
val res0 = List(1).map(((x) => {
val y = 2.$times(x);
scala.Tuple2(x, y)
})).map(((x$1) => x$1: @scala.unchecked match {
case scala.Tuple2((x @ _), (y @ _)) => y
}))
http://www.scala-lang.org/files/archive/spec/2.11/06-expressions.html#for-comprehensions-and-for-loops
http://stackoverflow.com/questions/14087014/what-are-the-scoping-rules-for-vals-in-scala-for-comprehensions
I think I was looking at it for the issue about how to let the bound
variable be declared implicit.
On Sat, Jan 24, 2015 at 11:58 AM, Haoyi Li <[email protected]> wrote:
> Actually I think my translation is wrong, it should be
>
> for(x <- c; y = ...) yield {...}
>
> is translated into
>
> c.map{ x => val y = ...; {...} }
>
>
> because it's the last generator before the yield. Point still stands though
>
> > Just to have functoriality? (Otoh, unit and flatmap provide it)
>
> Yeah I know why we need map in general, I was just wondering why this
> particular translation for local-values. I had imagined that only the last
> generator in the chain needed map, but then got yelled at by the compiler.
>
> On Sat, Jan 24, 2015 at 11:49 AM, Vlad Patryshev <[email protected]>
> wrote:
>
>> 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.
>>
>
> --
> 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.