This is a new site that's currently running on alpha code. There are going to be bugs. If you discover any, please report them on the site's issues page (GitHub account required). Thanks.
Warning: Many URLs are going to change. Refer to the README file to discover which library project's documentation has been completed.
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;
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):
Returns:
Returns:
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;
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;