Numbers\accumulatorFloat()

accumulator returns Closure pure
float → (float | null → Closure | float)
At a glance — A float accumulator: each call with a float appends to the running total and returns a fresh accumulator; call with null (or no argument) to finalise and read the total.

Creates a self-returning float accumulator. Each call with a float returns a new accumulator Closure with that value added to the running total. Call with null (or no argument) to read the accumulated float out.

/**
  * @param float $initial Optional seed value. Defaults to 0.0.
  * @return Closure(float|null):(Closure|float)
  */
Numbers\accumulatorFloat(float $initial = 0): Closure

Returned Closure

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

/**
  * @param float|null $value  Value to add, or null to finalise.
  * @return Closure|float     Closure while still accumulating, float once finalised.
  */
$function (?float $value = null): Closure|float

Examples

Partial Application

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

// Seed the accumulator with a starting value — each call returns a new accumulator.
$running = Numbers\accumulatorFloat(1.0);

// Add values step by step. Every add returns a fresh accumulator.
$running = $running(1.5);
$running = $running(2.25);
$running = $running(-0.75);

// Call with no argument (or null) to read the running total.
echo $running(); // 4.0

Curried

This can be called inline using currying.

// Chain the calls inline — every () adds; the final empty () finalises.
echo Numbers\accumulatorFloat()(1.5)(2.25)(0.25)(); // 4.0

Inlined with Higher Order Function

If you are not planning on reusing the Closure created, you can just call it inline with a higher order function as its callable.

// Fold an array of floats into a single total.
$prices = [19.99, 4.50, 12.75, 0.99];

$acc = array_reduce(
  $prices,
  fn($acc, $n) => $acc($n),
  Numbers\accumulatorFloat()
);

echo $acc(); // 38.23

Details

Numbers Functions

Releated Number accumulator Functions