Hello, everyone. I'm a novice on this forum, but I have some programming experience in C++, Object Pascal and C#.
I liked SIMBA: the idea, as well as the software, is really great . Unfortunately, I have not found foreach
operator in SIMBA, still, I enjoyed the experience of using it. I wanted to create one in SIMBA. Today I will tell you how I made a convenient analog of this operator for myself.
What you will need is:
1) Some imagination;
2) MSDN;
Well, let's begin-)
As stated in the MSDN:
“The foreach statement repeats a group of embedded statements for each element in an array or an object collection. The foreach statement is used to iterate through the collection to get the desired information, but should not be used to change the contents of the collection to avoid unpredictable side effects.”
In Delphi the analogue of the cycle (foreach) is "for in TList". However, in SIMBA we don't have "for in List" or foreach. How can we create it? The algorithm works as simple as that: we go through the collections doing something with each element or an element of a condition. Since we do not have collections, we use arrays. (I have not found an opportunity of overloading procedures, so I had to create two procedures with different names -( )
File foreach.simba:
Simba Code:
//procedure foreach for arrays
procedure foreach(Arr: array of Variant;func:function(p: Variant):integer);
var
i: integer;
begin
i:=0;
for i := 0 to high(Arr) do
if func(Arr[i]) <> 0 then exit;
end;
//procedure foreach_p for tpoint array
procedure foreach_p(Arr: array of TPoint;func:function(p: TPoint):integer);
var
i: integer;
begin
i:=0;
for i := 0 to high(Arr) do
if func(Arr[i]) <> 0 then exit;
end;
The presented procedure of implementing the algorithm foreach is meant for three types of arrays: array of integer, array of string, array of Tpoint. The first argument is the array itself, the second argument – the procedure for any action with an element of the array.
Test script:
Simba Code:
program foreach;
{$i srl/srl/misc/foreach.simba}
function Display(int: variant): integer;
begin
Writeln(ToStr(int));
if int=5 then result:=1 else result:=0;
end;
function Display1(point: TPoint): integer;
var
p: Tpoint;
begin
p.x:=162;
p.y:=244;
Writeln(ToStr(point));
if Point=p then result:=1 else result:=0;
end;
function Display2(s: variant): integer;
begin
Writeln(s);
result:=0;
end;
var
TestArray: array of variant;
PointArray: array of TPoint;
TestStrings: array of variant;
flag: integer;
begin
flag:=0;
TestArray:=[1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
PointArray:=[Point(24,39),Point(25,67),Point(162,244),Point(188,249)];
TestStrings:=['Metallica','Iron Maiden','MegaDeath'];
Writeln('INTEGER ARRAY');
foreach(TestArray,@Display);
Writeln('TPOINT ARRAY');
foreach_p(PointArray,@Display1);
Writeln('STRING ARRAY');
foreach(TestStrings,@Display2);
end.
Test output:
Simba Code:
Compiled successfully in 16 ms.
INTEGER ARRAY
1
2
3
4
5
TPOINT ARRAY
(24, 39)
(25, 67)
(162, 244)
STRING ARRAY
'Metallica'
'Iron Maiden'
'MegaDeath'
Successfully executed.
In conclusion I would like to say that there is nothing complicated or innovative, just a story about a missing implementation of the algorithm. I hope this material will be useful for someone. Thank you for your attention.
UPDATE: loop exit added.