Data commit

This commit is contained in:
Ingy döt Net 2023-07-01 11:58:00 -04:00
parent 7387c8f97b
commit cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions

View file

@ -0,0 +1,53 @@
// This example works on stuff as old as Delphi 5 (maybe older)
// Modern Delphi / Object Pascal has both
// • generic types
// • the ability to concatenate arrays with the '+' operator
// So I could just say:
// myarray := [1] + [2, 3];
// But if you do not have access to the latest/greatest, then:
{$apptype console}
type
// Array types must be declared in order to return them from functions
// They can also be used with open array parameters.
TArrayOfString = array of string;
function Concat( a, b : array of string ): TArrayOfString; overload;
{
Every array type needs its own 'Concat' function:
function Concat( a, b : array of integer ): TArrayOfInteger; overload;
function Concat( a, b : array of double ): TArrayOfDouble; overload;
etc
Also, dynamic and open array types ALWAYS start at 0. No need to complicate indexing here.
}
var
n : Integer;
begin
SetLength( result, Length(a)+Length(b) );
for n := 0 to High(a) do result[ n] := a[n];
for n := 0 to High(b) do result[Length(a)+n] := b[n]
end;
// Example time!
function Join( a : array of string; sep : string = ' ' ): string;
var
n : integer;
begin
if Length(a) > 0 then result := a[0];
for n := 1 to High(a) do result := result + sep + a[n]
end;
var
names : TArrayOfString;
begin
// Here we use the open array parameter constructor as a convenience
names := Concat( ['Korra', 'Asami'], ['Bolin', 'Mako'] );
WriteLn( Join(names) );
// Also convenient: open array parameters are assignment-compatible with our array type!
names := Concat( names, ['Varrick', 'Zhu Li'] );
WriteLn( #13#10, Join(names, ', ') );
names := Concat( ['Tenzin'], names );
Writeln( #13#10, Join(names, #13#10 ) );
end.

View file

@ -0,0 +1,38 @@
type
TReturnArray = array of integer; //you need to define a type to be able to return it
function ConcatArray(a1,a2:array of integer):TReturnArray;
var
i,r:integer;
begin
{ Low(array) is not necessarily 0 }
SetLength(result,High(a1)-Low(a1)+High(a2)-Low(a2)+2); //BAD idea to set a length you won't release, just to show the idea!
r:=0; //index on the result may be different to indexes on the sources
for i := Low(a1) to High(a1) do begin
result[r] := a1[i];
Inc(r);
end;
for i := Low(a2) to High(a2) do begin
result[r] := a2[i];
Inc(r);
end;
end;
procedure TForm1.Button1Click(Sender: TObject);
var
a1,a2:array of integer;
r1:array of integer;
i:integer;
begin
SetLength(a1,4);
SetLength(a2,3);
for i := Low(a1) to High(a1) do
a1[i] := i;
for i := Low(a2) to High(a2) do
a2[i] := i;
TReturnArray(r1) := ConcatArray(a1,a2);
for i := Low(r1) to High(r1) do
showMessage(IntToStr(r1[i]));
Finalize(r1); //IMPORTANT!
ShowMessage(IntToStr(High(r1)));
end;