The idea of functional programming is that functions are first-class objects. So you can pass functions as parameters; and you can return functions.
So here you can construct a function:
$func_less = less_than(3);
So we basically construct a function like:
$func_less = function ($a) {
return $a < 3;
};
and now $func_less
is a function that takes a parameter $a
and checks if that $a
is less than three. For example:
php > $func_less_three = less_than(3);
php > var_dump($func_less_three(2));
bool(true)
php > var_dump($func_less_three(14));
bool(false)
So first we construct a function $func_less_three
, then we call that function with two numbers 2
and 14
. Since 2
is less than 3
, it returns bool(true)
on the first call, and since 14 is not less than 3, it return bool(false)
on the second call.
In most functional programming languages like Haskell, all functions take exactly one parameter. So instead of adding two numbers together, one first constructs a function that adds a (in that function) constant value to a parameter.