Arrays\scanR()

higher-order transformer accepts iterable returns Closure pure
((A, T) → A, A) → (Iterable → Iterable)
At a glance — Reverse running-fold. Requires materialisation of a Generator source (reverse iteration can't be lazy), but the result is re-yielded for API consistency.

Creates a Closure that emits each intermediate accumulation of a right-fold over an array or iterable, ending with the initial value.

/**
  * @param callable(mixed $carry, mixed $value):mixed $function
  * @param mixed $initialValue
  * @return Closure(iterable<int|string, mixed>):(array<int, mixed>|\Generator<int, mixed>)
  */
Arrays\scanR(callable $function, $initialValue): Closure

Returned Closure

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

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

Examples

Partial Application

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

$fromRight = Arrays\scanR(fn($acc, $v) => $acc . $v, '');

print_r($fromRight(['a', 'b', 'c']));
// ['cba', 'cb', 'c', '']

Details

Arrays Functions

Releated Array fold Functions