Concepts of Functional Programming In PHP
Composition and Pipeline
Composition in functional programming is the combination of two or more functions to give a new function. Composing functions together is like snapping together a series of pipes for our data to flow through.
The result of compose is to be a function of one argument, (let’s call the argument x), which works like applying function f()
to result of applying function f(g) to f(x)
.
In PHP also, not only in function can you use composition, composition can also be implemented in classes.
Using composition in writing class
class Head {
private function __construct() {}}
class Body {
public $head;
public function __construct(Head, $head) {
$this->head = "Head";
}
}
$partBody = new Body(new Head);
Example of implementing compositing using function
function compose($f, $g) {
return function($x) use ($f, $g) {
return $f($g($x));
};
}
$test = compose('strlen', 'trim');
$output = $test('Test','-'); //The output: '----'
Currying and Partial Application
Currying is often used as a synonym for partial application. Although both concepts allow us to bind some parameters of a function, the core ideas are a bit different.
Currying is the technique of transforming a function that takes multiple arguments in such a way that it can be called as a chain of functions each with a single argument.
function multiply($x,$y,$z) {
return $x * $y * $z;
};
function curried($x){
return function($y) use ($x){
return function($z) use ($x,$y){
return multiply($x, $y, $z);
};
};
}
$f1 = curried(2);
$f2 = $f1(3);
$result =$f2(1); //6
Partial application (or partial function application) refers to the process of fixing a number of arguments to a function, producing another function of smaller arity. Basically it is a technique to reduce the number of arguments to a function. If we build upon the multiply() function we have used earlier, we can fix the first argument to 1 and receive a new function that only requires two arguments:
function multiply($x, $y, $z) {
return $x*$y*$z;
}
function partial($x){
return function($y,$z) use ($x){
return multiply($x, $y, $z);
};
}
$f1=partial(2);
$result= $f1(3,1); //6
“Partial” receives a function and an arbitrarily defined number of arguments. We apply reflection on the received function and check the number of arguments. We return a function that receives the rest of the arguments and, when we match the required number, we evaluate the function with the passed parameters. When not, we recursively call partial with the current arguments and the same function, until we reach the edge-case to evaluate it.we now create something fairly easy.
Functors and Monads
Monads are an ordinary data type. It has an immutable value, that is, the result of the bind() method is another (Monadic) class. Monad is just a type m that has three particular functions defined on it.
It has three things:
1. A value (which may be no value at all, a simple type, an object or a function)
2. Method of getting its value, often referred to as return()
3. A way of binding (or using) the value into some function, often referred to as bind(), the return value of which is another Monad, often but not always the same type as as the donor Monad.
References