GeneralFunctions\ifThen()

higher-order returns Closure pure
((T → bool), (T → T)) → (T → T)
At a glance — Conditional transformation without a full if/else. Use when you want to modify values that match some condition and leave the rest alone.

Creates a Closure that applies a transformation only when a predicate is true; otherwise returns the input unchanged.

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

Returned Closure

When GeneralFunctions\ifThen() 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.

// Upper-case only strings; leave everything else alone.
$shoutStrings = GeneralFunctions\ifThen('is_string', 'strtoupper');

var_dump($shoutStrings('foo')); // 'FOO'
var_dump($shoutStrings(42));    // 42 (unchanged)

Details

General Functions

Releated Combinators Functions