- About Scala
- Documentation
- Code Examples
- Software
- Scala Developers
For-Do Loops: A Modest Proposal to make imperative for loops with lots of generators a tiny bit nicer
Thu, 2011-04-14, 23:29
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:
Wouldn't it look much nicer like this?
Hey, it's already a reserved word! :)
-0xe1a
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
Thu, 2011-04-14, 23:47
#2
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:
One nice aspect of it though is it's used only for imperative loops. :)
I wouldn't suggest or condone removing the cuddly syntax, just adding a new one. :)
-0xe1a
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
Thu, 2011-04-14, 23:47
#3
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:
Interesting thought, but you'd produce a lot of instant garbage and risk OOMing on constructing a result that's useless by definition. :)
-0xe1a
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
Fri, 2011-04-15, 00:07
#4
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:
--
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
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.obligatoryBippyv3 <- 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
Fri, 2011-04-15, 00:37
#5
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
Fri, 2011-04-15, 00:57
#6
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:
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.
My proposal is *much* less grandiose than that. :)
-0xe1a
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
Fri, 2011-04-15, 07:57
#7
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:
--
----------------------------------------------
Martin Odersky
Prof., EPFL and CEO, Scala Solutions
PSED, 1015 Lausanne, Switzerland
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
Fri, 2011-04-15, 16:37
#8
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:
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
Fri, 2011-04-15, 18:57
#9
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:
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:
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
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 {Which would indeed have, at best, potential for confusion, even if the parsing and semantics could be made solid.
x
} do {
y
} while (cond) {
z
}
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
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: