Enhancing foreach

bearophile bearophileHUGS at lycos.com
Wed Jan 9 18:51:34 PST 2013


Jonathan M Davis:

> I almost never use iota.

Then a problem is in the difference in usage frequency. My first 
answer is that you probably should use iota more, because it's 
handy in many situations :-)

If I grep my code I count almost 120 usages of iota. I think D 
programmers that have some Python experience learn to use iota 
quickly, also because the semantics was designed to be the same 
as python3 range().


> It's useful upon occasion, but in my experience, it's
> _far_ more common to iterate over ranges of items rather than 
> just over a list of numbers.

Iterating on ranges is common, but numbers are items :-)
There are lot of legitimate uses of iota in D code.

One of the tens of legitimate usages of iota() is to replace the 
missing enumerate of Phobos, see Issue 5550.


> Clearly, you're doing things very differently than I am if you 
> use iota that much.

I am using ranges (including iota ranges) a lot in D code.


> Regardless, I don't think that loss to clarity is worth the 
> characters saved when providing only one argument to iota.

As I have said, clarity can't be considered ignoring the usage 
frequency. Why don't you use "add(,)" instead of "+"? Add or 
addition is more clear, but the usage of a symbol is acceptable 
here because it's both a very common operation and because 
because most people know the meaning of that symbol from math 
studies.

Another fact to consider is that functional style D code doesn't 
ask for the same compactness as non-functional D code. In 
"regular" D code adding few more chars is not a problem. In 
functional-style code you usually want to write more compact 
code, to keep a logical chunk of code in a line (or in a group of 
small lines if you add newlines before the dots). Functional 
languages are terse not just for arbitrary reasons, but because 
terseness seems to go well with that kind of coding. When you use 
iota terseness is more important than "normal" D code.


More information about the Digitalmars-d-learn mailing list