-
-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Need modern Pascal version (anonymous functions section, fix generics…
…_sorting for new FPC)
- Loading branch information
1 parent
847994c
commit dd39b9b
Showing
13 changed files
with
1,099 additions
and
12 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
42 changes: 42 additions & 0 deletions
42
htdocs/doc/modern_pascal_code_samples/anon_functions_assignment_test.dpr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
{ Working example adjusted from | ||
https://forum.lazarus.freepascal.org/index.php?topic=59468.0 } | ||
|
||
{$ifdef FPC} | ||
{$mode objfpc}{$H+}{$J-} | ||
{$modeswitch functionreferences} | ||
{$modeswitch anonymousfunctions} | ||
{$endif} | ||
|
||
uses Classes; | ||
|
||
type | ||
TFunc = function: LongInt; | ||
|
||
var | ||
p: TProcedure; | ||
f: TFunc; | ||
n: TNotifyEvent; | ||
begin | ||
procedure(const aArg: String) | ||
begin | ||
Writeln(aArg); | ||
end('Hello World'); | ||
|
||
p := procedure | ||
begin | ||
Writeln('Foobar'); | ||
end; | ||
p(); | ||
|
||
n := procedure(aSender: TObject) | ||
begin | ||
Writeln(HexStr(Pointer(aSender))); | ||
end; | ||
n(Nil); | ||
|
||
f := function MyRes : LongInt | ||
begin | ||
MyRes := 42; | ||
end; | ||
Writeln(f()); | ||
end. |
94 changes: 94 additions & 0 deletions
94
htdocs/doc/modern_pascal_code_samples/anon_functions_list_map_foreach.dpr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,94 @@ | ||
{ Example of Map, ForEach methods and processing list with anonymous functions. } | ||
|
||
{$ifdef FPC} | ||
{$mode objfpc}{$H+}{$J-} | ||
{$modeswitch functionreferences} | ||
{$modeswitch anonymousfunctions} | ||
{$endif} | ||
|
||
uses SysUtils, Generics.Collections; | ||
|
||
type | ||
{ Note about below TIntMapFunc and TIntMapProc definition, what to use? | ||
For anonymous functions all 3 versions will compile. | ||
You can assign anonymous function to any of them. | ||
Decide of the version based on what you want to assign to them *aside* | ||
from anonymous functions: | ||
- The 1st version (without "of object", without "reference to") | ||
allows to store a reference to a global function, | ||
- The 2nd (with "of object") | ||
allows to store a reference to a method of an object, | ||
- The 3rd (with "reference to") is the most universal, | ||
allows a lot of things -- | ||
see https://forum.lazarus.freepascal.org/index.php?topic=59468.0 . | ||
} | ||
|
||
TIntMapFunc = | ||
//function(const Index, Item: Integer): Integer; | ||
//function(const Index, Item: Integer): Integer of object; | ||
reference to function(const Index, Item: Integer): Integer; | ||
TIntMapProc = | ||
//procedure(const Index, Item: Integer); | ||
//procedure(const Index, Item: Integer) of object; | ||
reference to procedure(const Index, Item: Integer); | ||
|
||
TMyInts = class(specialize TList<Integer>) | ||
{ Change every item in the list using AFunc. } | ||
procedure Map(const AFunc: TIntMapFunc); | ||
{ Call AProc for every item in the list. } | ||
procedure ForEach(const AProc: TIntMapProc); | ||
end; | ||
|
||
procedure TMyInts.Map(const AFunc: TIntMapFunc); | ||
var | ||
Index: Integer; | ||
begin | ||
for Index := 0 to Count - 1 do | ||
Items[Index] := AFunc(Index, Items[Index]); | ||
end; | ||
|
||
procedure TMyInts.ForEach(const AProc: TIntMapProc); | ||
var | ||
Index: Integer; | ||
begin | ||
for Index := 0 to Count - 1 do | ||
AProc(Index, Items[Index]); | ||
end; | ||
|
||
var | ||
MyList: TMyInts; | ||
I: Integer; | ||
F: TIntMapFunc; | ||
begin | ||
MyList := TMyInts.Create; | ||
try | ||
for I := 0 to 10 do | ||
MyList.Add(I); | ||
|
||
F := function(const Index, Item: Integer): Integer | ||
begin | ||
Result := Item + 1; | ||
end; | ||
// effectively this increases all numbers on the list by 3 | ||
MyList.Map(F); | ||
MyList.Map(F); | ||
MyList.Map(F); | ||
|
||
// change all items to their squares | ||
MyList.Map(function(const Index, Item: Integer): Integer | ||
begin | ||
Result := Item * Item; | ||
end); | ||
|
||
// print all items | ||
MyList.ForEach(procedure(const Index, Item: Integer) | ||
begin | ||
WriteLn('Index: ', Index, ', Item: ', Item); | ||
end); | ||
finally FreeAndNil(MyList) end; | ||
end. |
48 changes: 48 additions & 0 deletions
48
htdocs/doc/modern_pascal_code_samples/anonymous_functions.dpr
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
{ Anonymous functions test. } | ||
|
||
{$ifdef FPC} | ||
{$mode objfpc}{$H+}{$J-} | ||
{$modeswitch functionreferences} | ||
{$modeswitch anonymousfunctions} | ||
{$endif} | ||
{$apptype CONSOLE} | ||
|
||
type | ||
TMyFunction = reference to function (const A, B: Integer): Integer; | ||
|
||
function ProcessTheList(const F: TMyFunction): Integer; | ||
var | ||
I: Integer; | ||
begin | ||
Result := 1; | ||
for I := 2 to 10 do | ||
Result := F(Result, I); | ||
end; | ||
|
||
var | ||
SomeFunction: TMyFunction; | ||
begin | ||
// Assign anonymous function to a variable | ||
SomeFunction := | ||
function(const A, B: Integer): Integer | ||
begin | ||
Result := A + B; | ||
end; | ||
WriteLn('1 + 2 + 3 ... + 10 = ', ProcessTheList(SomeFunction)); | ||
|
||
// Or, simpler, just define the anonymous function inside the parameter | ||
WriteLn('1 + 2 + 3 ... + 10 = ', ProcessTheList( | ||
function(const A, B: Integer): Integer | ||
begin | ||
Result := A + B; | ||
end | ||
)); | ||
|
||
// Similar test, now with multiplication | ||
WriteLn('1 * 2 * 3 ... * 10 = ', ProcessTheList( | ||
function(const A, B: Integer): Integer | ||
begin | ||
Result := A * B; | ||
end | ||
)); | ||
end. |
Oops, something went wrong.