GeneralFunctions\always()

returns Closure pure
T → (any → T)
At a glance — Constant-value supplier. Handy as a default inside ifElse or as a stub callback that should just return a known value.

Creates a Closure that always returns the same bound value, ignoring whatever argument it receives. Known in FP as the "K combinator" or "constant function".

/**
  * @param mixed $value The value you always want to return.
  * @return Closure(mixed):mixed
  */
GeneralFunctions\always($value): Closure

Returned Closure

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

/**
  * @param mixed $ignored
  * @return mixed
  */
$function ($ignored)

Examples

Partial Application

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

$alwaysTrue = GeneralFunctions\always(true);

var_dump($alwaysTrue(123));   // true
var_dump($alwaysTrue(null));  // true

// Useful as the "else" branch of ifElse:
$toIntOrZero = GeneralFunctions\ifElse('is_numeric', 'intval', GeneralFunctions\always(0));
echo $toIntOrZero('42');   // 42
echo $toIntOrZero('nope'); // 0

Details

General Functions

Releated Combinators Functions