Numbers\power()

transformer returns Closure pure throws
number → (number → number)
At a glance — Bind an exponent; the returned Closure wraps pow() to raise any number to that power. Throws InvalidArgumentException on non-number input.

Creates a Closure that raises any number passed to it to a pre-defined exponent. Wraps `pow($value, $exponent)`.

/**
  * @param int|float $exponent
  * @return Closure(int|float):(int|float)
  * @throws InvalidArgumentException If $exponent is not int or float.
  */
Numbers\power($exponent): Closure

Returned Closure

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

/**
  * @param int|float $value
  * @return int|float  Result of pow($value, $exponent).
  * @throws InvalidArgumentException If $value is not int or 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 squares every number it receives.
$squared = Numbers\power(2);

// Called as a function.
echo $squared(5);    // 25
echo $squared(1.5);  // 2.25
echo $squared(10);   // 100

// Used in a higher order function.
$array = array_map($squared, [1, 2, 3, 4, 5]);
print_r($array); // [1, 4, 9, 16, 25]

Curried

This can be called inline using currying.

echo Numbers\power(3)(2); // 8

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.

$cubes = array_map(Numbers\power(3), [1, 2, 3, 4]);
print_r($cubes); // [1, 8, 27, 64]

Details

Numbers Functions

Releated Number transform Functions