Jump to page: 1 2 3
Thread overview
Enhancing foreach
Jan 09, 2013
ixid
Jan 09, 2013
Jacob Carlborg
Jan 09, 2013
ixid
Jan 09, 2013
ixid
Jan 09, 2013
Ali Çehreli
Jan 09, 2013
bearophile
Jan 09, 2013
Ali Çehreli
Jan 09, 2013
ixid
Jan 09, 2013
Ali Çehreli
Jan 09, 2013
Jesse Phillips
Jan 09, 2013
Jonathan M Davis
Jan 10, 2013
ixid
Jan 10, 2013
Jonathan M Davis
Jan 10, 2013
Peter Summerland
Jan 10, 2013
monarch_dodra
Jan 10, 2013
Raphaël Jakse
Jan 15, 2013
Ary Borenszweig
Jan 15, 2013
Raphaël Jakse
Jan 10, 2013
monarch_dodra
Jan 10, 2013
bearophile
Jan 10, 2013
Jonathan M Davis
Jan 10, 2013
bearophile
Jan 10, 2013
Jonathan M Davis
Jan 10, 2013
bearophile
Jan 10, 2013
Jonathan M Davis
January 09, 2013
A very minor change that would be elegant and easy for beginners:

foreach(i;5)
    //stuff

Allowing just a single number to mean the end point and a default starting point of zero is assumed, just as with iota it's possible to write it iota(5) or even 5.iota, it assumes unless otherwise specified that you mean 0 to be the starting point. Would this be a reasonable enhancement request for me to make or does it collide with something?
January 09, 2013
On 2013-01-09 05:38, ixid wrote:
> A very minor change that would be elegant and easy for beginners:
>
> foreach(i;5)
>      //stuff
>
> Allowing just a single number to mean the end point and a default
> starting point of zero is assumed, just as with iota it's possible to
> write it iota(5) or even 5.iota, it assumes unless otherwise specified
> that you mean 0 to be the starting point. Would this be a reasonable
> enhancement request for me to make or does it collide with something?

I don't see what this would gain over:

foreach (i ; 0 .. 5)

-- 
/Jacob Carlborg
January 09, 2013
On Wednesday, 9 January 2013 at 08:01:05 UTC, Jacob Carlborg wrote:
> On 2013-01-09 05:38, ixid wrote:
>> A very minor change that would be elegant and easy for beginners:
>>
>> foreach(i;5)
>>     //stuff
>>
>> Allowing just a single number to mean the end point and a default
>> starting point of zero is assumed, just as with iota it's possible to
>> write it iota(5) or even 5.iota, it assumes unless otherwise specified
>> that you mean 0 to be the starting point. Would this be a reasonable
>> enhancement request for me to make or does it collide with something?
>
> I don't see what this would gain over:
>
> foreach (i ; 0 .. 5)

Just like the terser iota, it's more elegant and easier for newer users. The fewer symbols there are in code the easier it is to parse for the user (up to a point). It's also more consistent with iota. It also means that a foreach that iterates a function return value can be written in the same manner whether it returns an array or an integer, again more consistency.
January 09, 2013
> Just like the terser iota, it's more elegant and easier for newer users. The fewer symbols there are in code the easier it is to parse for the user (up to a point). It's also more consistent with iota. It also means that a foreach that iterates a function return value can be written in the same manner whether it returns an array or an integer, again more consistency.

It would be even nicer to be able to write for(i;5) and have it behave as foreach.
January 09, 2013
On 01/09/2013 09:07 AM, ixid wrote:
>> Just like the terser iota,

iota got it wrong. I would expect new users to be confused by iota(5). Is that "ends at 5" or "begins with 5"? The latter is more consistent with how the function parameters at the and can have default values. So it is likely to be perceived as the following. Ignoring that iota is a template:

  /* ... */ iota(size_t begin, size_t end = size_t.max);

>> it's more elegant and easier for newer
>> users.

I think having multiple ways of doing the same thing is confusing.

> It would be even nicer to be able to write for(i;5) and have it behave
> as foreach.

Maybe nicer but not helpful. What is the meaning of the following?

  for (i; ++j) {
      /* ... */
  }

Did I forget something or is it "from zero to the incremented value of j"?

Ali

January 09, 2013
Ali Çehreli:

> iota got it wrong. I would expect new users to be confused by iota(5). Is that "ends at 5" or "begins with 5"? The latter is more consistent with how the function parameters at the and can have default values. So it is likely to be perceived as the following. Ignoring that iota is a template:
>
>   /* ... */ iota(size_t begin, size_t end = size_t.max);

I was the one that asked for the iota(5) syntax. The name iota comes from APL, where a single argument is supported, with similar meaning. But the idea of a single argument comes from Python:

>>> range(5)
[0, 1, 2, 3, 4]

I have taught Python, and newbies understand/know that a range(5) means a range of five items. The only problems is knowing where it starts. But when you teach Python you say that indexes are 0-based, so they quickly remember that the range of five items starts from zero. In D iota does the same. And so far I have had no problems from using iota this way.

So I think iota got it right.

Instead, what I have had to tell friends reading my D code is what the heck "iota" means. The meaning of the word "range" in Python is simpler to guess.

Bye,
bearophile
January 09, 2013
I think you're rather a long way away from beginners to see how beginners would think. iota is behaving like the Python range syntax which beginners seem to find quite easy to use. A beginner thinks 'it goes up to that number', the thought process you outlined is that of someone learning their second or third language.

>   for (i; ++j) {
>       /* ... */
>   }
>
> Did I forget something or is it "from zero to the incremented value of j"?

Is that any different to the current for loops not doing what you meant if you don't type things? It doesn't seem subtle nor an unexpected behaviour, there's no shortage of cases where if you omit bits of code it'll do something else. If this was supposed to be a normal for loop the user's forgotten to give i a type and a boundary condition and the second semi-colon, that's three errors, the space of different behaviours you can get to in any given piece of code by making three errors is probably quite high.
January 09, 2013
On 01/09/2013 10:08 AM, ixid wrote:

>> for (i; ++j) {
>> /* ... */
>> }
>>
>> Did I forget something or is it "from zero to the incremented value of
>> j"?
>

> If this was supposed to be a normal for loop
> the user's forgotten to give i a type and a boundary condition and the
> second semi-colon, that's three errors, the space of different
> behaviours you can get to in any given piece of code by making three
> errors is probably quite high.

I think the name 'i' would make one think that there has been three mistakes. (I admit that I chose it that way. ;) ) In fact, there was a single mistake: a forgotten semicolon (and a space that comes with it for readability):

    for (; i; ++j) {
        /* ... */
    }

Ali

January 09, 2013
On 01/09/2013 10:06 AM, bearophile wrote:

> Instead, what I have had to tell friends reading my D code is what the
> heck "iota" means. The meaning of the word "range" in Python is simpler
> to guess.

Same here. :)

Ali

January 09, 2013
On Wednesday, 9 January 2013 at 17:07:50 UTC, ixid wrote:
> It would be even nicer to be able to write for(i;5) and have it behave as foreach.

I hated java's choice to use for as foreach. It does not prepare me for the semantics of what I'm about to read. And when I go to write it and don't remember, is it for(Type thing; range) or for(Type thing, range)? no it is for(Type thing : range)

I was already heavily using foreach in do before Java got one.
« First   ‹ Prev
1 2 3