Thread overview
for (i < n)
Mar 13, 2004
Piotr Fusik
Mar 13, 2004
Roel Mathys
Mar 13, 2004
Piotr Fusik
Mar 13, 2004
Vathix
Mar 13, 2004
Matthew
Mar 13, 2004
Andy Friesen
Mar 13, 2004
Matthew
Mar 14, 2004
J Anderson
Mar 19, 2004
Ben Robinson
Mar 19, 2004
Matthew
March 13, 2004
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)


March 13, 2004
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):
		...
March 13, 2004
>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.


March 13, 2004
> 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..
March 13, 2004
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)
>
>


March 13, 2004
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
March 13, 2004
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


March 14, 2004
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/
March 19, 2004
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 ) ...


March 19, 2004
"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.