Forum > Beginners

for loop with custom step width (esp. ≠ 1)

<< < (21/22) > >>

Thaddy:
Generic reverse:

--- Code: Delphi  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program arrayhelp;{$mode delphi}type  TIntArray = TArray<integer>;  TStringArray = TArray<string>;  procedure swap<T>(var left,right:T);inline;  var    temp:T;  begin    temp:=left;    left:=right;    right:=temp;  end;   procedure Reverse<T>(var Value:array of T);  var    i: Integer = 0;  begin    if Length(Value) > 0 then      for i := Low(Value) to High(Value) div 2 do        Swap<T>(Value[i],Value[High(Value) - i]);  end; var  i:integer;  s:string;  si:TintArray = [1,2,3,4,5,6,7,8,9,10];  ss:TStringArray = ['there', 'is', 'more', 'Horatio'];begin  reverse<integer>(si);  for i in si do write(i:2);  writeln;  reverse<string>(ss);  for s in ss do writeln(s);end.Note I couldn't get it to work in {$mode objfpc} but that is probably an oversight and laziness from me.
Note that altough the mode is Delphi it won't work in Delphi.

simone:
Thanks Thaddy for the nice piece of code.

This is my version for {$mode ObjFpc}:


--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---program arrayhelp;{$mode objfpc}type  TIntArray = specialize TArray<integer>;  TStringArray = specialize TArray<string>;  generic procedure swap<T>(var left,right:T);inline;  var    temp:T;  begin    temp:=left;    left:=right;    right:=temp;  end;   generic procedure Reverse<T>(var Value:array of T);  var    i: Integer = 0;  begin    if Length(Value) > 0 then      for i := Low(Value) to High(Value) div 2 do        specialize Swap<T>(Value[i],Value[High(Value) - i]);  end; var  i:integer;  s:string;  si:TintArray = (1,2,3,4,5,6,7,8,9,10);  ss:TStringArray = ('there', 'is', 'more', 'Horatio');begin  specialize reverse<integer>(si);  for i in si do write(i:2);  writeln;  specialize reverse<string>(ss);  for s in ss do writeln(s);end.
In addition to usage of generic/specialize keywords, note the round brackets in place of the square brackets to initialize dynamic arrays.

avk:
I couldn't resist and added a reverse enumerator to the array helpers.

munair:

--- Quote from: winni on December 12, 2021, 04:25:43 pm ---Hi!

There are two kinds of loop with any step you like.

They are while and repeat.

So we don't have to mess Pascal with any C syntax.

Keep it simple!

Winni

--- End quote ---

I very much agree that things should be kept simple. But almost any language I know of has at least two or even three different loop statements adding more keywords and rules to a language (= not simple). I must admit that when designing a new language it is very tempting to copy existing constructs rather than to break away from tradition and do one's own thinking. Of course, this does not apply to FreePascal or any other language set about to maintain compatibility.

In the last couple of months I've been struggling with loop constructs and the idea was quickly born to have just one loop flexible enough to cover everything. This morning I might have succeeded in doing just that. Amazingly, it cuts out quite some compiler code and complexity (especially when supporting jumps) while providing a very flexible loop construct. For those interested, here are a few examples: https://sharpbasic.com/forum/viewtopic.php?t=40

I agree with 440bx that a for-loop -- IF implemented (with or without a step or by clause) -- should maintain its traditional form. My attempts to redesign it never gave a truly satisfying result, until the introduction of well-designed shortcut operators, which can be applied consistently in a language.

munair:
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?

Navigation

[0] Message Index

[#] Next page

[*] Previous page

Go to full version