Shift<T> class method

Project: Array Utilities Unit

Unit: DelphiDabbler.Lib.ArrayUtils

Record: TArrayUtils

Applies to: ~>0.1

class function Shift<T>(var A: TArray<T>): T;
  static;

Description

Removes the first element from a non-empty array.

The length of the array is reduced by one.

Parameters:

Returns:

Preconditions:

Example

This example shifts elements from an array until the array is empty.

procedure Shift_Eg;
var
  A, Expected: TArray<string>;
begin
  A := TArray<string>.Create('a', 'stitch', 'in', 'time');

  Expected := TArray<string>.Create('stitch', 'in', 'time');
  Assert(TArrayUtils.Shift<string>(A) = 'a');
  Assert(TArrayUtils.Equal<string>(Expected, A, SameStr));

  Expected := TArray<string>.Create('in', 'time');
  Assert(TArrayUtils.Shift<string>(A) = 'stitch');
  Assert(TArrayUtils.Equal<string>(Expected, A, SameStr));

  Expected := TArray<string>.Create('time');
  Assert(TArrayUtils.Shift<string>(A) = 'in');
  Assert(TArrayUtils.Equal<string>(Expected, A, SameStr));

  Expected := TArray<string>.Create();
  Assert(TArrayUtils.Shift<string>(A) = 'time');
  Assert(TArrayUtils.Equal<string>(Expected, A, SameStr));
end;

See Also