Skip to content
Ivan Semenkov edited this page Feb 14, 2021 · 2 revisions

Table of contents

About

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.

TOptionalValue

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>;

Create

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.

Example
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;

Iterate

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;
Example
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;

FirstEntry

Retrive the first entry.

function FirstEntry : TIterator;
Example
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;

TIterator

HasValue

Return true if iterator has correct value.

function HasValue : Boolean;
Example
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;
Next

Retrieve the iterator for next entry.

function Next : TIterator;
Example
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;
Value

To get value use Value property.

property Value : {$IFNDEF USE_OPTIONAL}V{$ELSE}TOptionalValue{$ENDIF};
Example
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;
Clone this wiki locally