As a proof of concept I decided to implement the "map" functional method using ezthreads. It makes use of the new method generics, so to try it out you'll have to compile with trunk, but here's a simple example, where the input is a generic string list that gets '_modified' appended to each item
{$mode delphi}
{$ModeSwitch nestedprocvars}
program ezthreads_tester_functional;
uses
ezthreads, ezthreads.functional, fgl;
procedure TestSimpleList;
var
LList : TFPGList<String>;
LResult : IFPGListResult<String>;
I: Integer;
(*
method passed into map to perform some simple processing
*)
function Start(Const AThread : IEZThread; Const AItem : String) : String;
begin
Result := AItem + '_modified';
end;
begin
//input list with a few entries
LList := TFPGList<String>.Create;
LList.Add('hello');
LList.Add('World');
WriteLn('--Before--');
for I := 0 to Pred(LList.Count) do
WriteLn(LList[I]);
//now we should be able to call map with on our list
LResult := Map<String>(LList, Start);
//await the result before assigning new values to the list
Await(LResult);
//assign the modified values
LList.Assign(LResult.Result);
WriteLn('--After--');
for I := 0 to Pred(LList.Count) do
WriteLn(LList[I]);
LList.Free;
end;
begin
TestSimpleList;
end.
One thing to note is that the processing is done in a separate thread, so Await(IEZThread) is called to wait until it's finished.
Attached is the result after running
here's the link to my tester app if anyone wants to play around with it:
https://github.com/mr-highball/ezthreads/blob/master/test/ezthreads_tester_functional.lpr