Skip to content

Latest commit

 

History

History
111 lines (81 loc) · 3.53 KB

File metadata and controls

111 lines (81 loc) · 3.53 KB

Transform<TIn,TOut> class method

Project: Array Utilities Unit

Unit: DelphiDabbler.Lib.ArrayUtils

Record: TArrayUtils

Applies to: ~>0.1

type
  TTransformer<TIn,TOut> = reference to function (const AValue: TIn): TOut;
  TTransformerEx<TIn,TOut> = reference to function (const AValue: TIn;
    const AIndex: Integer; const AArray: array of TIn): TOut;

class function Transform<TIn,TOut>(const A: array of TIn;
  const ATransformFunc: TTransformer<TIn,TOut>): TArray<TOut>;
  overload; static;

class function Transform<TIn,TOut>(const A: array of TIn;
  const ATransformFunc: TTransformerEx<TIn,TOut>): TArray<TOut>;
  overload; static;

Description

Transforms the elements, of type TIn, of a given array into elements of type TOut of another array.

The transformed array is the same length as the input array. The elements of the transformed array correspond to the elements of the input array at the same index.

Parameters:

  • A - The array whose elements are to be transformed.

  • ATransformFunc - Reference to a function that transforms a value of type TIn to another value of type TOut. This function is called once per element of A and its result is stored in the transformed array. ATransformFunc must be a function of type TTransformer<TIn,TOut> or TTransformerEx<TIn,TOut>

    Parameter(s):

    • AValue - Value of type TIn to be transformed.
    • AIndex - Index of AValue in A. (TTransformerEx<TIn,TOut> only.)
    • AArray - Reference to the array containing AValue, i.e. A. (TTransformerEx<TIn,TOut> only.)

    Returns:

    • The transformed value, of type TOut.

Returns:

  • The array of transformed values, with elements of type TOut.

Examples

Example #1

The first example uses the simpler TTransformer<TIn,TOut> version of the method to transform an array of integers between 1..10 into their roman numeral equivalents.

type
  TOneToTen = 1..10;

procedure Transform_Eg1;
var
  A: TArray<TOneToTen>;
  Got, Expected: TArray<string>;
  RomanTransformer: TArrayUtils.TTransformer<TOneToTen,string>;
begin
  {$RangeChecks On}
  RomanTransformer := function (const N: TOneToTen): string
    const
      Numerals: array[TOneToTen] of string = (
        'I', 'II', 'III', 'IV', 'V', 'VI', 'VII', 'VIII', 'IX', 'X'
      );
    begin
      Result := Numerals[N];
    end;

  A := TArray<TOneToTen>.Create(3, 5, 8, 2, 9);
  Got := TArrayUtils.Transform<TOneToTen, string>(A, RomanTransformer);
  Expected := TArray<string>.Create('III', 'V', 'VIII', 'II', 'IX');
  Assert(TArrayUtils.Equal<string>(Expected, Got, SameStr));
end;

Example #2

The second example uses the TTransformerEx<TIn,TOut> version of the method to raise a sequence of non-negative integers to the power of their position in the sequence. Note that, in this case, both TIn and TOut are the same type.

procedure Transform_Eq2;
var
  A, Got, Expected: TArray<Cardinal>;
begin
  A := TArray<Cardinal>.Create(5, 8, 3, 2, 0, 1, 3);
  Got := TArrayUtils.Transform<Cardinal,Cardinal>(
    A,
    function (const AValue: Cardinal; const AIndex: Integer;
      const A: array of Cardinal): Cardinal
    begin
      Result := Round(IntPower(AValue, AIndex + 1));
    end
  );
  Expected := TArray<Cardinal>.Create(5, 64, 27, 16, 0, 1, 2187);
  Assert(TArrayUtils.Equal<Cardinal>(Expected, Got));
end;

See Also