For-comprehension implementation on Scala?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

For-comprehension implementation on Scala?

Daryoush Mehrtash-2

I like to know if the  for-comprehension construct in Scala is  itself implemented on scala or not.  If so where can I find the actual implementation?

thanks,

Daryoush
dpp
Reply | Threaded
Open this post in threaded view
|

Re: For-comprehension implementation on Scala?

dpp
for comprehensions are re-writes:

for (x <- stuff; y <- otherStuff) println(x+" "+y)
becomes:
stuff.foreach(x => otherStuff.foreach(y => println(x+" "+y)))

for (x <- stuff; y <- otherStuff) yield x * y
becomes
stuff.flatMap(x => otherStuff.map(y => x * y))

If there's an 'if' clause, insert a "filter" into the mix:

for (x <- stuff if x > 0; y <- otherStuff) yield x * y
becomes
stuff.filter(_ > 0).flatMap(x => otherStuff.map(y => x * y))

The beauty, is that any classes with map, flatMap, filter, and foreach can be used in for compehensions.



On 1/13/08, Daryoush Mehrtash <[hidden email]> wrote:

I like to know if the  for-comprehension construct in Scala is  itself implemented on scala or not.  If so where can I find the actual implementation?

thanks,

Daryoush



--
lift, the secure, simple, powerful web framework http://liftweb.net
Collaborative Task Management http://much4.us
dpp
Reply | Threaded
Open this post in threaded view
|

Re: For-comprehension implementation on Scala?

dpp


On 1/13/08, Daryoush Mehrtash <[hidden email]> wrote:
I understand the concept; what do you mean by "re-write"?  Specifically, where does the re-write happen? Inside the compiler, or is there a piece of scala library code that does the conversion.

The re-write happens in the compiler.

The compiler has many phases of re-writing your code into simplifications (e.g., applying implicits, converting for comprehensions to map/flatMap/foreach/filter, etc.)

To see the re-written output of your program, type:

scalac -print MyProgram.scala

thanks....


On Jan 13, 2008 4:08 PM, David Pollak <[hidden email] > wrote:
for comprehensions are re-writes:

for (x <- stuff; y <- otherStuff) println(x+" "+y)
becomes:
stuff.foreach(x => otherStuff.foreach(y => println(x+" "+y)))

for (x <- stuff; y <- otherStuff) yield x * y
becomes
stuff.flatMap(x => otherStuff.map(y => x * y))

If there's an 'if' clause, insert a "filter" into the mix:

for (x <- stuff if x > 0; y <- otherStuff) yield x * y
becomes
stuff.filter(_ > 0).flatMap(x => otherStuff.map(y => x * y))

The beauty, is that any classes with map, flatMap, filter, and foreach can be used in for compehensions.




On 1/13/08, Daryoush Mehrtash <[hidden email]> wrote:

I like to know if the  for-comprehension construct in Scala is  itself implemented on scala or not.  If so where can I find the actual implementation?

thanks,

Daryoush



--
lift, the secure, simple, powerful web framework <a href="http://liftweb.net" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)"> http://liftweb.net
Collaborative Task Management <a href="http://much4.us" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)">http://much4.us



--
Daryoush

Weblog:  <a href="http://perlustration.blogspot.com/" target="_blank" onclick="return top.js.OpenExtLink(window,event,this)"> http://perlustration.blogspot.com/



--
lift, the secure, simple, powerful web framework http://liftweb.net
Collaborative Task Management http://much4.us
Reply | Threaded
Open this post in threaded view
|

Re: For-comprehension implementation on Scala?

James Iry
In reply to this post by Daryoush Mehrtash-2
Daryoush,

The complete expansion rules on "for" are in the language spec starting on page 83.   I covered them somewhat in http://james-iry.blogspot.com/2007/10/monads-are-elephants-part-2.html but ignored some of the complexities having to do with pattern matching.

---- David Pollak <[hidden email]> wrote:

> On 1/13/08, Daryoush Mehrtash <[hidden email]> wrote:
> >
> > I understand the concept; what do you mean by "re-write"?  Specifically,
> > where does the re-write happen? Inside the compiler, or is there a piece of
> > scala library code that does the conversion.
>
>
> The re-write happens in the compiler.
>
> The compiler has many phases of re-writing your code into simplifications (
> e.g., applying implicits, converting for comprehensions to
> map/flatMap/foreach/filter, etc.)
>
> To see the re-written output of your program, type:
>
> scalac -print MyProgram.scala
>
> thanks....
> >
> > On Jan 13, 2008 4:08 PM, David Pollak <[hidden email]>
> > wrote:
> >
> > > for comprehensions are re-writes:
> > >
> > > for (x <- stuff; y <- otherStuff) println(x+" "+y)
> > > becomes:
> > > stuff.foreach(x => otherStuff.foreach(y => println(x+" "+y)))
> > >
> > > for (x <- stuff; y <- otherStuff) yield x * y
> > > becomes
> > > stuff.flatMap(x => otherStuff.map(y => x * y))
> > >
> > > If there's an 'if' clause, insert a "filter" into the mix:
> > >
> > > for (x <- stuff if x > 0; y <- otherStuff) yield x * y
> > > becomes
> > > stuff.filter(_ > 0).flatMap(x => otherStuff.map(y => x * y))
> > >
> > > The beauty, is that any classes with map, flatMap, filter, and foreach
> > > can be used in for compehensions.
> > >
> > >
> > >
> > >
> > > On 1/13/08, Daryoush Mehrtash <[hidden email]> wrote:
> > > >
> > > >
> > > > I like to know if the  for-comprehension construct in Scala is  itself
> > > > implemented on scala or not.  If so where can I find the actual
> > > > implementation?
> > > >
> > > > thanks,
> > > >
> > > > Daryoush
> > > >
> > >
> > >
> > >
> > > --
> > > lift, the secure, simple, powerful web framework http://liftweb.net
> > > Collaborative Task Management http://much4.us
> >
> >
> >
> >
> > --
> > Daryoush
> >
> > Weblog:  http://perlustration.blogspot.com/
>
>
>
>
> --
> lift, the secure, simple, powerful web framework http://liftweb.net
> Collaborative Task Management http://much4.us