https://github.com/p-samuel/anonymous-methods
Using higher-order logic to copy JavaScript's "filter", "map" and "reduce" functions in Delphi.
https://github.com/p-samuel/anonymous-methods
anonymous-functions delphi delphi10 filter find foreach-loop higher-order-functions higher-order-logic javascript map pascal reduce
Last synced: 3 months ago
JSON representation
Using higher-order logic to copy JavaScript's "filter", "map" and "reduce" functions in Delphi.
- Host: GitHub
- URL: https://github.com/p-samuel/anonymous-methods
- Owner: p-samuel
- License: mit
- Created: 2022-08-27T18:26:44.000Z (over 3 years ago)
- Default Branch: main
- Last Pushed: 2024-12-05T14:36:36.000Z (over 1 year ago)
- Last Synced: 2025-10-08T21:58:35.207Z (5 months ago)
- Topics: anonymous-functions, delphi, delphi10, filter, find, foreach-loop, higher-order-functions, higher-order-logic, javascript, map, pascal, reduce
- Language: Pascal
- Homepage:
- Size: 64.5 KB
- Stars: 7
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
## Filter
``` pascal
LNumbers := TArray.Create(1, 3, 4, 5, 6, 14, 15, 17, 22, 23, 25, 32);
FilteredNumbers := TUtil.Filter(LNumbers,
function (ANumber: Integer): Boolean
begin
Result := (ANumber mod 3 = 0);
end);
```
## Map
``` pascal
LWords := TArray.Create('a', 'b', 'c', 'd', 'e');
UpperCaseWords := TUtil.Map(LWords,
function (AWord: String): String
begin
Result := UpperCase(AWord);
end
);
```
## Reduce
``` pascal
LScores := TArray.Create(8.5, 6.5, 7.8, 10);
LTotal := TUtil.Reduce(LScores,
function (Acc, Item: Double): Double
begin
Result := Acc + Item;
end, 0);
```
## Find
``` pascal
//Find first item which price is equal to 200.
LCart := TArray.Create(
TItem.Create('Bike', 400),
TItem.Create('Clock', 15),
TItem.Create('Closet', 200),
TItem.Create('Cabin', 200),
TItem.Create('Stove', 100),
TItem.Create('Pots', 20)
);
LItem := TUtil.Find(LCart,
function (Item: TItem): Boolean
begin
Result := Item.Price = 200;
end);
```
## Every
```pascal
LNames := TArray.Create('Samuel', 'Bob', 'Obama', 'Andrew', 'Julia');
LEvery := TUtil.Every(LNames,
function (Name: String): Boolean
begin
Result := Name.ToLower().Contains('a');
end);
```
## Some
```pascal
LNumbers := TArray.Create(0, 1, 2, 3, 4);
LSome := TUtil.Some(LNumbers,
function (Number: Integer): Boolean
begin
Result := Number > 5;
end);
```
## ForEach
``` pascal
LNumbers := TArray.Create(1, 2, 3, 4, 5);
TUtil.ForEach(LNumbers,
function (Number: Integer): Integer
begin
Result := Number + 1000;
end);
```