Recent

Poll

I want for-loops with custom step widths (plural!).

false
26 (41.9%)
true
29 (46.8%)
42
7 (11.3%)

Total Members Voted: 62

Author Topic: for loop with custom step width (esp. ≠ 1)  (Read 54000 times)

MarkMLl

  • Hero Member
  • *****
  • Posts: 7419
Re: for loop with custom step width (esp. ≠ 1)
« Reply #105 on: March 09, 2022, 08:49:09 pm »
A few days ago, when I looked at the poll, true and false were pretty even. Now false has gained quite a lead. I guess that means no stepping in the foreseeable future?

If you want it then see https://forum.lazarus.freepascal.org/index.php/topic,35141.msg427768.html#msg427768

MarkMLl
MT+86 & Turbo Pascal v1 on CCP/M-86, multitasking with LAN & graphics in 128Kb.
Logitech, TopSpeed & FTL Modula-2 on bare metal (Z80, '286 protected mode).
Pet hate: people who boast about the size and sophistication of their computer.
GitHub repositories: https://github.com/MarkMLl?tab=repositories

BubikolRamios

  • Sr. Member
  • ****
  • Posts: 290
Re: for loop with custom step width (esp. ≠ 1)
« Reply #106 on: June 03, 2024, 05:16:01 pm »
Hey,
Code: Pascal  [Select][+][-]
  1. for i := 0 step 2 until 10 do
  2. begin
  3.   writeln(i);
  4. end;
How do I make a for-loop iterate with a custom step width? So everything regarding iteration is in the top line. That'd be more readable than the corresponding Ersatz-statement. Thx for your answers!

just bumbed into that,  I think nobody invented this reverse logic (-:
step = 3 here

Code: Pascal  [Select][+][-]
  1. for i := 0 to x do
  2.  begin
  3.       if i mod 3 <> 0 then Continue;
  4.  
  5.      //do stuff ...
  6.  end;
  7.  

lazarus 3.2-fpc-3.2.2-win32/win64

Dzandaa

  • Sr. Member
  • ****
  • Posts: 349
  • From C# to Lazarus
Re: for loop with custom step width (esp. ≠ 1)
« Reply #107 on: June 03, 2024, 05:32:53 pm »
Hi,

also:

Code: Pascal  [Select][+][-]
  1. i := 0;
  2. while i < 20 do
  3. begin
  4.  // Do something
  5.  inc(i, 2);
  6. end;  
  7.  

B->
Regards,
Dzandaa

Warfley

  • Hero Member
  • *****
  • Posts: 1521
Re: for loop with custom step width (esp. ≠ 1)
« Reply #108 on: June 03, 2024, 05:42:59 pm »
If anything the syntax should be:
Code: Pascal  [Select][+][-]
  1. for i:=0 to 10 in steps of 2 do

But putting that aside, that is a very specific thing, but we already have the for ... in ... loop, for which you can do the following:
Code: Pascal  [Select][+][-]
  1. {$ModeSwitch advancedrecords}
  2.  
  3. type
  4.   TStepIterator = record
  5.     Start, Stop, Step: Integer;
  6.     property Current: Integer read Start;
  7.     function MoveNext: Boolean; inline;
  8.     function GetEnumerator: TStepIterator; inline;
  9.   end;
  10.  
  11. function TStepIterator.MoveNext: Boolean;
  12. begin
  13.   Inc(Start, Step);
  14.   Result := Start <= Stop;
  15. end;
  16.  
  17. function TStepIterator.GetEnumerator: TStepIterator;
  18. begin
  19.   Result := Self;
  20. end;
  21.  
  22. function Steps(Start, Stop, Step: Integer): TStepIterator; inline;
  23. begin
  24.   Result.Start := Start - Step;
  25.   Result.Stop := Stop;
  26.   Result.Step := Step;
  27. end;
  28.  
  29. var
  30.   i: Integer;
  31. begin
  32.   for i in Steps(0, 10, 2) do
  33.     WriteLn(i);
  34. end.

So there is absolutely no need for a dedicated syntax element for this. That said, what could be very useful is if ranges were dataobjects. So for example the 0..10 would not just be a type, but also a value that can be used for sets (if x in 0..10), for arrays (arr := [0..10]) or as lazy generator and enumerator (for x in 0..10). Maybe also with steps 0..10:2 to only have the even numbers, etc.
This would enable a lot more very cool features especially with custom enumerators.

 

TinyPortal © 2005-2018