-
Notifications
You must be signed in to change notification settings - Fork 7
TMap
Ivan Semenkov edited this page Feb 14, 2021
·
2 revisions
Map applying the given functor to each item of a given iterable object.
uses
utils.functional;
type
generic TMap<V; Iterator; Functor> = class
Iterator
is a forward iterator implemented object.
Functor
is a unary functor which applying to elements.
If macro {$USE_OPTIONAL}
is defined, then all methods return a TOptionalValue wrapper, otherwise V.
uses
utils.optional;
type
TOptionalValue = {$IFDEF FPC}specialize{$ENDIF} TOptional<V>;
A new map object can be created by call its constructor.
constructor Create (AIterator : Iterator; AFunctor : Functor);
AIterator
is a forward iterator implemented object.
AFunctor
is unary functor which applying to elements.
uses
container.list, utils.functor, utils.functional;
type
TIntegerList = {$IFDEF FPC}specialize{$ENDIF} TList<Integer, TCompareFunctorInteger>;
TPow2Functor = class({$IFDE FPC}specialize{$ENDIF} TUnaryFunctor<Integer, Integer>)
public
function Call(AValue : Integer) : Integer; override;
begin
Result := AValue * AValue;
end;
end;
TPowMap = {$IFDEF FPC}specialize{$ENDIF} TMap<Integer, TIntegerList.TIterator, TPow2Functor>;
var
list : TIntegerList;
functor : TPow2Functor;
map : TPowMap;
begin
list := TIntegerList.Create;
functor := TPow2Functor.Create;
map := TPowMap.Create(list.FirstEntry, functor);
FreeAndNil(map);
FreeAndNil(functor);
FreeAndNil(list);
end;
It is possible to iterate for TMap values using in
operator. Each value would present as TMap.TIterator object.
uses
utils.functional;
type
generic TMap<V; Iterator> = class
type
TIterator = class
end;
uses
container.list, utils.functor, utils.functional;
type
TIntegerList = {$IFDEF FPC}specialize{$ENDIF} TList<Integer, TCompareFunctorInteger>;
TPow2Functor = class({$IFDE FPC}specialize{$ENDIF} TUnaryFunctor<Integer, Integer>)
public
function Call(AValue : Integer) : Integer; override;
begin
Result := AValue * AValue;
end;
end;
TPowMap = {$IFDEF FPC}specialize{$ENDIF} TMap<Integer, TIntegerList.TIterator, TPow2Functor>;
var
list : TIntegerList;
functor : TPow2Functor;
map : TPowMap;
iterator : TPowMap.TIterator;
begin
list := TIntegerList.Create;
functor := TPow2Functor.Create;
map := TPowMap.Create(list.FirstEntry, functor);
for iterator in map do
;
FreeAndNil(map);
FreeAndNil(functor);
FreeAndNil(list);
end;
Retrive the first entry.
function FirstEntry : TIterator;
uses
container.list, utils.functor, utils.functional;
type
TIntegerList = {$IFDEF FPC}specialize{$ENDIF} TList<Integer, TCompareFunctorInteger>;
TPow2Functor = class({$IFDE FPC}specialize{$ENDIF} TUnaryFunctor<Integer, Integer>)
public
function Call(AValue : Integer) : Integer; override;
begin
Result := AValue * AValue;
end;
end;
TPowMap = {$IFDEF FPC}specialize{$ENDIF} TMap<Integer, TIntegerList.TIterator, TPow2Functor>;
var
list : TIntegerList;
functor : TPow2Functor;
map : TPowMap;
iterator : TPowMap.TIterator;
begin
list := TIntegerList.Create;
functor := TPow2Functor.Create;
map := TPowMap.Create(list.FirstEntry, functor);
iterator := map.FirstEntry;
while iterator.HasValue do
begin
iterator := iterator.Next;
end;
FreeAndNil(map);
FreeAndNil(functor);
FreeAndNil(list);
end;
Return true if iterator has correct value.
function HasValue : Boolean;
uses
container.list, utils.functor, utils.functional;
type
TIntegerList = {$IFDEF FPC}specialize{$ENDIF} TList<Integer, TCompareFunctorInteger>;
TPow2Functor = class({$IFDE FPC}specialize{$ENDIF} TUnaryFunctor<Integer, Integer>)
public
function Call(AValue : Integer) : Integer; override;
begin
Result := AValue * AValue;
end;
end;
TPowMap = {$IFDEF FPC}specialize{$ENDIF} TMap<Integer, TIntegerList.TIterator, TPow2Functor>;
var
list : TIntegerList;
functor : TPow2Functor;
map : TPowMap;
iterator : TPowMap.TIterator;
begin
list := TIntegerList.Create;
functor := TPow2Functor.Create;
map := TPowMap.Create(list.FirstEntry, functor);
for iterator in map do
begin
if iterator.HasValue then
;
end;
FreeAndNil(map);
FreeAndNil(functor);
FreeAndNil(list);
end;
Retrieve the iterator for next entry.
function Next : TIterator;
uses
container.list, utils.functor, utils.functional;
type
TIntegerList = {$IFDEF FPC}specialize{$ENDIF} TList<Integer, TCompareFunctorInteger>;
TPow2Functor = class({$IFDE FPC}specialize{$ENDIF} TUnaryFunctor<Integer, Integer>)
public
function Call(AValue : Integer) : Integer; override;
begin
Result := AValue * AValue;
end;
end;
TPowMap = {$IFDEF FPC}specialize{$ENDIF} TMap<Integer, TIntegerList.TIterator, TPow2Functor>;
var
list : TIntegerList;
functor : TPow2Functor;
map : TPowMap;
iterator : TPowMap.TIterator;
begin
list := TIntegerList.Create;
functor := TPow2Functor.Create;
map := TPowMap.Create(list.FirstEntry, functor);
for iterator in map do
begin
if iterator.Next.HasValue then
;
end;
FreeAndNil(map);
FreeAndNil(functor);
FreeAndNil(list);
end;
To get value use Value property.
property Value : {$IFNDEF USE_OPTIONAL}V{$ELSE}TOptionalValue{$ENDIF};
uses
container.list, utils.functor, utils.functional;
type
TIntegerList = {$IFDEF FPC}specialize{$ENDIF} TList<Integer, TCompareFunctorInteger>;
TPow2Functor = class({$IFDE FPC}specialize{$ENDIF} TUnaryFunctor<Integer, Integer>)
public
function Call(AValue : Integer) : Integer; override;
begin
Result := AValue * AValue;
end;
end;
TPowMap = {$IFDEF FPC}specialize{$ENDIF} TMap<Integer, TIntegerList.TIterator, TPow2Functor>;
var
list : TIntegerList;
functor : TPow2Functor;
map : TPowMap;
iterator : TPowMap.TIterator;
begin
list := TIntegerList.Create;
functor := TPow2Functor.Create;
map := TPowMap.Create(list.FirstEntry, functor);
for iterator in map do
begin
writeln(iterator.Value);
end;
FreeAndNil(map);
FreeAndNil(functor);
FreeAndNil(list);
end;