Numbers\sum()

transformer returns Closure pure throws
number → (number → number)
At a glance — Bind an amount to add up front; the returned Closure is a reusable adder. Throws InvalidArgumentException on non-number input.

Creates a Closure that adds a pre-defined amount to any number passed to it. Useful wherever you want a reusable "add N" function for piping, mapping or composition.

/**
  * @param int|float $initial Defaults to 0.
  * @return Closure(int|float):(int|float)
  * @throws InvalidArgumentException If $initial is not int or float.
  */
Numbers\sum($initial = 0): Closure

Returned Closure

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

/**
  * @param int|float $value
  * @return int|float
  */
$function (int|float $value): int|float

Examples

Partial Application

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

// Create a function that adds 5 to whatever it receives.
$addFive = Numbers\sum(5);

// Called as a function.
echo $addFive(15.5); // 20.5
echo $addFive(-2);   // 3

// Used in a higher order function.
$array = array_map($addFive, [1, 2, 3, 4]);
print_r($array); // [6, 7, 8, 9]

Curried

This can be called inline using currying.

echo Numbers\sum(5)(10); // 15

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.

$array = array_map(Numbers\sum(100), [1, 2, 3]);
print_r($array); // [101, 102, 103]

Details

Numbers Functions

Releated Number arithmetic Functions