Re: 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 | <CALruUQKTnWHnyQ0Wfdm9b_6vQ0EbeMBmTsWbYVMOU7araOXh=w@mail.gmail.com> |
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.