D - for (i < n)
- Piotr Fusik (23/23) Mar 13 2004 Here's another proposal for a syntactic sugar.
- Roel Mathys (14/42) Mar 13 2004 did you look for what that integer variable is used?
- Piotr Fusik (2/5) Mar 13 2004 Good point.
- Vathix (4/7) Mar 13 2004 I think I like it, but it doesn't show what's really going on. What
- Matthew (18/41) Mar 13 2004 One of the classes in the DTL - IntRange - achieves this for you via the
- Andy Friesen (3/15) Mar 13 2004 You could use a static IntRange.opCall to achieve that.
- Matthew (4/19) Mar 13 2004 Interesting. Let me have a think ...
- J Anderson (4/28) Mar 13 2004 This would be nice syntax sugar.
- Ben Robinson (12/20) Mar 18 2004 For sure, Piotr, I've wanted this one for years! (In fact, I always ...
- Matthew (10/31) Mar 19 2004 is
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
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
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
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
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
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
Interesting. Let me have a think ... "Andy Friesen" <andy ikagames.com> wrote in message news:c3004v$qv4$1 digitaldaemon.com...Matthew wrote:"new".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 theYou could use a static IntRange.opCall to achieve that. -- andy
Mar 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)This would be nice syntax sugar. -- -Anderson: http://badmama.com.au/~anderson/
Mar 13 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 ) ...
Mar 18 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...isHere's another proposal for a syntactic sugar. ... for (i < n) which implies that i is first initialized with zero and each iteration ithad a Cincremented 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 alwaysmacro for it, despite the dodginess of macros). There is a mathematical model of the Natural numbers which treats 0 as theemptyset, and n as the set {0,...,n-1} (ie recursively define n+1 = n U{n} ). Ina language allowing this interpretation, the standard loop could bewritten asforeach (int i;n) ... The suggestion already posted of using IntRange is close to this idea,but somebetter 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









Piotr Fusik <Piotr_member pathlink.com> 