www.digitalmars.com         C & C++   DMDScript  

D - for (i < n)

reply Piotr Fusik <Piotr_member pathlink.com> writes:
Here's another proposal for a syntactic sugar.

I checked hundreds of megabytes of C and Java sources and noticed that 40-50%
"for" loops are of form:
for (i = 0; i < n; i++)
where:
i is an integer variable (not neccessarily named "i", it can be "counter" for
example)
n is an expression

I think that this justifies introducing an alternate syntax:
for (i < n)
which implies that i is first initialized with zero and each iteration it is
incremented by one. The following should also work:
for (int i < n)  // for (int i = 0; i < n; ++i)
for (i <= n)
etc.
"<=" loops are much less common than "<", but I think that supporting their in a
short form is also a good idea.

This probably looks "exotic" to you, but think: about *a half* of "for" loops
could be written much shorter! The difference is better visible if the
variable's name is descriptive:
for (int counter = 0; counter < size; counter++)
vs.
for (int counter < size)
Mar 13 2004
next sibling parent reply Roel Mathys <roel.mathys yucom.be> writes:
Piotr Fusik wrote:
 Here's another proposal for a syntactic sugar.
 
 I checked hundreds of megabytes of C and Java sources and noticed that 40-50%
 "for" loops are of form:
 for (i = 0; i < n; i++)
 where:
 i is an integer variable (not neccessarily named "i", it can be "counter" for
 example)
 n is an expression
 
 I think that this justifies introducing an alternate syntax:
 for (i < n)
 which implies that i is first initialized with zero and each iteration it is
 incremented by one. The following should also work:
 for (int i < n)  // for (int i = 0; i < n; ++i)
 for (i <= n)
 etc.
 "<=" loops are much less common than "<", but I think that supporting their in
a
 short form is also a good idea.
 
 This probably looks "exotic" to you, but think: about *a half* of "for" loops
 could be written much shorter! The difference is better visible if the
 variable's name is descriptive:
 for (int counter = 0; counter < size; counter++)
 vs.
 for (int counter < size)
 
 
did you look for what that integer variable is used? one major use will be to access an element in a container, in D you can to that with foreach( char a; ch_array ) { ... } in python you could do for a in ch_array : ... and if you need the "indexer": for i,a in enumerate(ch_array): ...
Mar 13 2004
parent Piotr Fusik <Piotr_member pathlink.com> writes:
did you look for what that integer variable is used?
one major use will be to access an element in a container
 [...]
Good point. It's hard to check it automatically, but I'll try to verify that.
Mar 13 2004
prev sibling next sibling parent Vathix <vathix dprogramming.com> writes:
 for (int i < n)  // for (int i = 0; i < n; ++i)
 for (i <= n)
 etc.
I think I like it, but it doesn't show what's really going on. What about something like: for(i = 0++ < n) //init i to 0, increment by 1. Doesn't look as nice; oh well..
Mar 13 2004
prev sibling next sibling parent reply "Matthew" <matthew stlsoft.org> writes:
One of the classes in the DTL - IntRange - achieves this for you via the
foreach statement, as in:

    foreach(int i; new IntRange(0, 10, +1))
    {
        printf("%d ", i);
    }

The three parameters are initial-value, end-value (one past the post, of
course), and increment.

If structs had constructors, then this could be achieved without the "new".

Cheers

Matthew



"Piotr Fusik" <Piotr_member pathlink.com> wrote in message
news:c2v1cd$29h8$1 digitaldaemon.com...
 Here's another proposal for a syntactic sugar.

 I checked hundreds of megabytes of C and Java sources and noticed that
40-50%
 "for" loops are of form:
 for (i = 0; i < n; i++)
 where:
 i is an integer variable (not neccessarily named "i", it can be "counter"
for
 example)
 n is an expression

 I think that this justifies introducing an alternate syntax:
 for (i < n)
 which implies that i is first initialized with zero and each iteration it
is
 incremented by one. The following should also work:
 for (int i < n)  // for (int i = 0; i < n; ++i)
 for (i <= n)
 etc.
 "<=" loops are much less common than "<", but I think that supporting
their in a
 short form is also a good idea.

 This probably looks "exotic" to you, but think: about *a half* of "for"
loops
 could be written much shorter! The difference is better visible if the
 variable's name is descriptive:
 for (int counter = 0; counter < size; counter++)
 vs.
 for (int counter < size)
Mar 13 2004
parent reply Andy Friesen <andy ikagames.com> writes:
Matthew wrote:

 One of the classes in the DTL - IntRange - achieves this for you via the
 foreach statement, as in:
 
     foreach(int i; new IntRange(0, 10, +1))
     {
         printf("%d ", i);
     }
 
 The three parameters are initial-value, end-value (one past the post, of
 course), and increment.
 
 If structs had constructors, then this could be achieved without the "new".
You could use a static IntRange.opCall to achieve that. -- andy
Mar 13 2004
parent "Matthew" <matthew stlsoft.org> writes:
Interesting. Let me have a think ...

"Andy Friesen" <andy ikagames.com> wrote in message
news:c3004v$qv4$1 digitaldaemon.com...
 Matthew wrote:

 One of the classes in the DTL - IntRange - achieves this for you via the
 foreach statement, as in:

     foreach(int i; new IntRange(0, 10, +1))
     {
         printf("%d ", i);
     }

 The three parameters are initial-value, end-value (one past the post, of
 course), and increment.

 If structs had constructors, then this could be achieved without the
"new".
 You could use a static IntRange.opCall to achieve that.

   -- andy
Mar 13 2004
prev sibling next sibling parent J Anderson <REMOVEanderson badmama.com.au> writes:
Piotr Fusik wrote:

Here's another proposal for a syntactic sugar.

I checked hundreds of megabytes of C and Java sources and noticed that 40-50%
"for" loops are of form:
for (i = 0; i < n; i++)
where:
i is an integer variable (not neccessarily named "i", it can be "counter" for
example)
n is an expression

I think that this justifies introducing an alternate syntax:
for (i < n)
which implies that i is first initialized with zero and each iteration it is
incremented by one. The following should also work:
for (int i < n)  // for (int i = 0; i < n; ++i)
for (i <= n)
etc.
"<=" loops are much less common than "<", but I think that supporting their in a
short form is also a good idea.

This probably looks "exotic" to you, but think: about *a half* of "for" loops
could be written much shorter! The difference is better visible if the
variable's name is descriptive:
for (int counter = 0; counter < size; counter++)
vs.
for (int counter < size)
  
This would be nice syntax sugar. -- -Anderson: http://badmama.com.au/~anderson/
Mar 13 2004
prev sibling parent reply Ben Robinson <Ben_member pathlink.com> writes:
In article <c2v1cd$29h8$1 digitaldaemon.com>, Piotr Fusik says...
Here's another proposal for a syntactic sugar.
 ...
for (i < n)
which implies that i is first initialized with zero and each iteration it is
incremented by one. The following should also work:
for (int i < n)  // for (int i = 0; i < n; ++i)
for (i <= n)
etc.
For sure, Piotr, I've wanted this one for years! (In fact, I always had a C macro for it, despite the dodginess of macros). There is a mathematical model of the Natural numbers which treats 0 as the empty set, and n as the set {0,...,n-1} (ie recursively define n+1 = n U {n} ). In a language allowing this interpretation, the standard loop could be written as foreach (int i;n) ... The suggestion already posted of using IntRange is close to this idea, but some better shorthand would be good - how about a unary operator < ... <n = IntRange(0,n) so we could type foreach (int i; <n ) ...
Mar 18 2004
parent "Matthew" <matthew stlsoft.org> writes:
"Ben Robinson" <Ben_member pathlink.com> wrote in message
news:c3dl62$2nm4$1 digitaldaemon.com...
 In article <c2v1cd$29h8$1 digitaldaemon.com>, Piotr Fusik says...
Here's another proposal for a syntactic sugar.
 ...
for (i < n)
which implies that i is first initialized with zero and each iteration it
is
incremented by one. The following should also work:
for (int i < n)  // for (int i = 0; i < n; ++i)
for (i <= n)
etc.
For sure, Piotr, I've wanted this one for years! (In fact, I always
had a C
 macro for it,  despite the dodginess of macros).

 There is a mathematical model of the Natural numbers which treats 0 as the
empty
 set,  and n as the set {0,...,n-1}  (ie recursively define n+1 = n U
{n} ). In
 a language allowing this interpretation,  the standard loop could be
written as
 foreach (int i;n) ...

 The suggestion already posted of using IntRange is close to this idea,
but some
 better shorthand would be good - how about a unary operator < ...

 <n = IntRange(0,n)

 so we could type

 foreach (int i; <n ) ...
That's feasible, or something a bit more generic. I'll bring it up with big-W in our DTL discussions.
Mar 19 2004