GeneralFunctions\ifElse()

higher-order returns Closure pure
((T → bool), (T → U), (T → U)) → (T → U)
At a glance — The functional equivalent of an if/else expression. Works well with always() as the else branch for default values.

Creates a Closure that branches on a predicate — applies one transformation when the predicate is true, another when it's false.

/**
  * @param callable(mixed):bool  $condition
  * @param callable(mixed):mixed $then
  * @param callable(mixed):mixed $else
  * @return Closure(mixed):mixed
  */
GeneralFunctions\ifElse(callable $condition, callable $then, callable $else): Closure

Returned Closure

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

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

Examples

Partial Application

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

$bucket = GeneralFunctions\ifElse(
  fn($n) => $n >= 0,
  GeneralFunctions\always('positive'),
  GeneralFunctions\always('negative')
);

echo $bucket(42);  // 'positive'
echo $bucket(-7);  // 'negative'

Details

General Functions

Releated Combinators Functions