Enhancing foreach

bearophile bearophileHUGS at lycos.com
Wed Jan 9 10:06:45 PST 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


More information about the Digitalmars-d-learn mailing list