Project

General

Profile

Feature #1472

Idea for for loops

Added by John Abbott almost 4 years ago. Updated 2 months ago.

Status:
In Progress
Priority:
Low
Assignee:
-
Category:
New Function
Target version:
Start date:
23 Jul 2020
Due date:
% Done:

10%

Estimated time:
Spent time:

Description

Victor Shoup told me that he has written a little class in NTL which allows him to write

for (long j: range(n)) {...}

instead of the old style
for (long j=0; j < n; ++j) {...}

Do we want something similar in CoCoALib?
I thought C++(14) already something like but did not find after a quick search on internet.


Related issues

Related to CoCoALib - Feature #312: LongRange(a,b) returning vector of long a..b (included)Closed2013-02-14

Related to CoCoALib - Design #601: LongRange: in which file should it be declared/defined?In Progress2014-07-31

Related to CoCoALib - Design #1346: C++14: use the new for loop syntax where appropriate (like cocoa's foreach)In Progress2019-10-21

History

#1 Updated by John Abbott almost 4 years ago

While most loops in C++ do start from 0, I think I would like to have the ability to say whether to start from 0 or 1.
For example we could have:

for (long j: from0to(n)) {...}
for (long j: from1to(n)) {...}

Ahhh! Now I see a slight problem. The from0to range would normally stop at n-1 rather then n. Hmmmm

#2 Updated by John Abbott almost 4 years ago

  • Related to Feature #312: LongRange(a,b) returning vector of long a..b (included) added

#3 Updated by John Abbott almost 4 years ago

  • Related to Design #601: LongRange: in which file should it be declared/defined? added

#4 Updated by Anna Maria Bigatti almost 4 years ago

range(0,n) ?

#5 Updated by John Abbott over 3 years ago

  • Status changed from New to In Progress
  • Priority changed from Normal to Low
  • Target version changed from CoCoALib-0.99800 to CoCoALib-0.99850
  • % Done changed from 0 to 10

Anna's suggestion of range(1,n) etc. seems simple, clear, flexible.
The intention is that both values are included, right?

We might need to be careful if the max value is the largest possible long.

To use the new "range for" syntax, range(a,b) has to produce a sort of iterator; I wonder how fast that is compared to a classical for loop. Maybe this should not matter; if it is measurably slower (for almost empty loops) then we can always use a classical for loop where needed for speed.

It would be nice to eliminate the existing LongRange function, or replace it with this new iterator. Perhaps there could be automatic conversion from this new iterator type to vector<long>?

I have marked this as "low" priority, and postponed to 0.99750 as it is not urgent... rather just "icing on the cake".

#6 Updated by John Abbott over 3 years ago

  • Related to Design #1346: C++14: use the new for loop syntax where appropriate (like cocoa's foreach) added

#7 Updated by Anna Maria Bigatti over 3 years ago

just wondering: (I know it's low priority) is this any better than the standard syntax?

#8 Updated by John Abbott 2 months ago

  • Target version changed from CoCoALib-0.99850 to CoCoALib-0.99900

While it can be difficult to say much positive about Julia, it does offer a convenient way of using "iterators" (and also a very compact notation). Perhaps we can take some inspiration from there?

Also available in: Atom PDF