Arrays\takeLast()

transformer terminal accepts iterable returns Closure pure throws
int → (Iterable → T[])
At a glance — Terminal — consumes the whole source. Do not use with infinite Generators. Throws on negative counts.

Creates a Closure that takes the last N elements of an array or iterable. Terminal — the last N are only knowable once the source has been fully consumed.

/**
  * @param int $count
  * @return Closure(iterable<int|string, mixed>):mixed[]
  * @throws \InvalidArgumentException if count is negative.
  */
Arrays\takeLast(int $count = 1): Closure

Returned Closure

When Arrays\takeLast() is called, it returns the following Closure which can be used like a regular function.

/**
  * @param iterable<int|string, mixed> $source
  * @return mixed[]
  */
$function (iterable $source): array

Examples

Partial Application

This can be used to create a simple closure which can be used as a regular function.

$last3 = Arrays\takeLast(3);
print_r($last3([1, 2, 3, 4, 5])); // [3, 4, 5]

Details

Arrays Functions

Releated Array take Functions