This page is no longer maintained — Please continue to the home page at www.scala-lang.org

For-Do Loops: A Modest Proposal to make imperative for loops with lots of generators a tiny bit nicer

9 replies
Alex Cruise
Joined: 2008-12-17,
User offline. Last seen 2 years 26 weeks ago.
Sometimes, when I'm feeling imperative (e.g. because I'm in an actor's receive method) and I need a lot of generators to get the iteration I want, I wind up writing stuff like this:
for {
  v1 <- gen1
  v2 <- v1.obligatoryBippy
  v3 <- v2.yadda 
} {
  doStuff(v3)
}

Wouldn't it look much nicer like this? 
for {
  v1 <- gen1
  v2 <- v1.something
} do {
  doStuff(v2)
}

Hey, it's already a reserved word! :)
-0xe1a
daniel
Joined: 2008-08-20,
User offline. Last seen 44 weeks 15 hours ago.
Re: For-Do Loops: A Modest Proposal to make imperative for loop
That does look kinda nice.  On a philosophical level though, I'm not sure that the overloading of the do keyword is the best idea.  It makes logical sense, but then so does Java's overloading of the synchronized keyword (and we all know how well that turned out).

On a practical note, this would probably stomp all over source compatibility.

Daniel

On Thu, Apr 14, 2011 at 5:29 PM, Alex Cruise <alex@cluonflux.com> wrote:
Sometimes, when I'm feeling imperative (e.g. because I'm in an actor's receive method) and I need a lot of generators to get the iteration I want, I wind up writing stuff like this:
for {
  v1 <- gen1
  v2 <- v1.obligatoryBippy
  v3 <- v2.yadda 
} {
  doStuff(v3)
}

Wouldn't it look much nicer like this? 
for {
  v1 <- gen1
  v2 <- v1.something
} do {
  doStuff(v2)
}

Hey, it's already a reserved word! :)
-0xe1a

Alex Cruise
Joined: 2008-12-17,
User offline. Last seen 2 years 26 weeks ago.
Re: For-Do Loops: A Modest Proposal to make imperative for loop
On Thu, Apr 14, 2011 at 3:35 PM, Daniel Spiewak <djspiewak@gmail.com> wrote:
That does look kinda nice.  On a philosophical level though, I'm not sure that the overloading of the do keyword is the best idea.  It makes logical sense, but then so does Java's overloading of the synchronized keyword (and we all know how well that turned out).

One nice aspect of it though is it's used only for imperative loops. :) 
On a practical note, this would probably stomp all over source compatibility.

I wouldn't suggest or condone removing the cuddly syntax, just adding a new one. :)  
-0xe1a
Alex Cruise
Joined: 2008-12-17,
User offline. Last seen 2 years 26 weeks ago.
Re: For-Do Loops: A Modest Proposal to make imperative for loop
On Thu, Apr 14, 2011 at 3:40 PM, Kevin Wright <kev.lee.wright@gmail.com> wrote:
Then stop feeling imperative :P
I guess yield could also be used here, and you ultimately yield a bunch of units that then aren't assigned to anything.  Not sure of any performance implications though.

Interesting thought, but you'd produce a lot of instant garbage and risk OOMing on constructing a result that's useless by definition. :)
-0xe1a
Kevin Wright 2
Joined: 2010-05-30,
User offline. Last seen 26 weeks 4 days ago.
Re: For-Do Loops: A Modest Proposal to make imperative for loop
Then stop feeling imperative :P
I guess yield could also be used here, and you ultimately yield a bunch of units that then aren't assigned to anything.  Not sure of any performance implications though.

On 14 April 2011 23:35, Daniel Spiewak <djspiewak@gmail.com> wrote:
That does look kinda nice.  On a philosophical level though, I'm not sure that the overloading of the do keyword is the best idea.  It makes logical sense, but then so does Java's overloading of the synchronized keyword (and we all know how well that turned out).

On a practical note, this would probably stomp all over source compatibility.

Daniel

On Thu, Apr 14, 2011 at 5:29 PM, Alex Cruise <alex@cluonflux.com> wrote:
Sometimes, when I'm feeling imperative (e.g. because I'm in an actor's receive method) and I need a lot of generators to get the iteration I want, I wind up writing stuff like this:
for {
  v1 <- gen1
  v2 <- v1.obligatoryBippy
  v3 <- v2.yadda 
} {
  doStuff(v3)
}

Wouldn't it look much nicer like this? 
for {
  v1 <- gen1
  v2 <- v1.something
} do {
  doStuff(v2)
}

Hey, it's already a reserved word! :)
-0xe1a




--
Kevin Wright

gtalk / msn : kev.lee.wright@gmail.comkev.lee.wright@gmail.commail: kevin.wright@scalatechnology.com
vibe / skype: kev.lee.wrightquora: http://www.quora.com/Kevin-Wright
twitter: @thecoda

"My point today is that, if we wish to count lines of code, we should not regard them as "lines produced" but as "lines spent": the current conventional wisdom is so foolish as to book that count on the wrong side of the ledger" ~ Dijkstra
Sciss
Joined: 2008-12-17,
User offline. Last seen 28 weeks 5 days ago.
Re: For-Do Loops: A Modest Proposal to make imperative for loop

then as a consequence

while( x.predicate ) do { ... }

and

if( x.predicate ) do { } else { }

no way do'ed.

i really have no clue what's going on in scala-virtualized, but maybe your dream can come true one day??

On 14 Apr 2011, at 23:29, Alex Cruise wrote:

> Sometimes, when I'm feeling imperative (e.g. because I'm in an actor's receive method) and I need a lot of generators to get the iteration I want, I wind up writing stuff like this:
>
> for {
> v1 <- gen1
> v2 <- v1.obligatoryBippy
> v3 <- v2.yadda
> } {
> doStuff(v3)
> }
>
> Wouldn't it look much nicer like this?
>
> for {
> v1 <- gen1
> v2 <- v1.something
> } do {
> doStuff(v2)
> }
>
> Hey, it's already a reserved word! :)
>
> -0xe1a

Alex Cruise
Joined: 2008-12-17,
User offline. Last seen 2 years 26 weeks ago.
Re: For-Do Loops: A Modest Proposal to make imperative for loop
On Thu, Apr 14, 2011 at 4:26 PM, Sciss <contact@sciss.de> wrote:
then as a consequence

while( x.predicate ) do { ... }

and

if( x.predicate ) do { } else { }

no way do'ed.

heh.  Well, don't forget, `while`, `if`, `for`, `else` and indeed `do`, are keywords, not identifiers.  Their syntax is baked-in, so we don't have to worry about arbitrary combinations.  
i really have no clue what's going on in scala-virtualized, but maybe your dream can come true one day??

My proposal is *much* less grandiose than that. :)
-0xe1a
odersky
Joined: 2008-07-29,
User offline. Last seen 45 weeks 6 days ago.
Re: For-Do Loops: A Modest Proposal to make imperative for loop
How do you parse this then?

for { ... } do while (..) { ...}

 -- Martin





On Fri, Apr 15, 2011 at 1:52 AM, Alex Cruise <alex@cluonflux.com> wrote:
On Thu, Apr 14, 2011 at 4:26 PM, Sciss <contact@sciss.de> wrote:
then as a consequence

while( x.predicate ) do { ... }

and

if( x.predicate ) do { } else { }

no way do'ed.

heh.  Well, don't forget, `while`, `if`, `for`, `else` and indeed `do`, are keywords, not identifiers.  Their syntax is baked-in, so we don't have to worry about arbitrary combinations.  
i really have no clue what's going on in scala-virtualized, but maybe your dream can come true one day??

My proposal is *much* less grandiose than that. :)
-0xe1a



--
----------------------------------------------
Martin Odersky
Prof., EPFL and CEO, Scala Solutions
PSED, 1015 Lausanne, Switzerland


vpatryshev
Joined: 2009-02-16,
User offline. Last seen 1 year 24 weeks ago.
Re: For-Do Loops: A Modest Proposal to make imperative for loop
I remember implementing it in Forth.

for {...} do while (..) { ...} until {...}

All conditions apply in due time.

Thanks,
-Vlad


On Thu, Apr 14, 2011 at 11:51 PM, martin odersky <martin.odersky@epfl.ch> wrote:
How do you parse this then?

for { ... } do while (..) { ...}

 -- Martin





On Fri, Apr 15, 2011 at 1:52 AM, Alex Cruise <alex@cluonflux.com> wrote:
On Thu, Apr 14, 2011 at 4:26 PM, Sciss <contact@sciss.de> wrote:
then as a consequence

while( x.predicate ) do { ... }

and

if( x.predicate ) do { } else { }

no way do'ed.

heh.  Well, don't forget, `while`, `if`, `for`, `else` and indeed `do`, are keywords, not identifiers.  Their syntax is baked-in, so we don't have to worry about arbitrary combinations.  
i really have no clue what's going on in scala-virtualized, but maybe your dream can come true one day??

My proposal is *much* less grandiose than that. :)
-0xe1a



--
----------------------------------------------
Martin Odersky
Prof., EPFL and CEO, Scala Solutions
PSED, 1015 Lausanne, Switzerland



Alex Cruise
Joined: 2008-12-17,
User offline. Last seen 2 years 26 weeks ago.
Re: For-Do Loops: A Modest Proposal to make imperative for loop
On Thu, Apr 14, 2011 at 11:51 PM, martin odersky <martin.odersky@epfl.ch> wrote:
How do you parse this then?

for { ... } do while (..) { ...}

Interesting!  I guess I would expect the while to be an inner loop.
I'm not sure there's much syntactic ambiguity there, since the juxtaposition "do while" doesn't have any preexisting meaning.  However, this springs to mind:
for {
  x
} do {
  y
} while (cond) {
  z
}
 
Which would indeed have, at best, potential for confusion, even if the parsing and semantics could be made solid.
It reminds me of the 4GL I cut my teeth on:
Done = 0loop  readnext ID else Done = 1until Done  read record from file, ID then doStuff(record) repeat   -0xe1a

Copyright © 2012 École Polytechnique Fédérale de Lausanne (EPFL), Lausanne, Switzerland