Partial applications

A partial application or partial function application is a technique to reduce the number of arguments of a function or to use partial arguments and create another function to act on the remaining arguments in order to produce the same output as what we would get if it were called with all the arguments at once. If we consider our sum function to be partial, where it is expected to take three parameters, but we can call it with two arguments, and later on add the remaining one. Here is the code sample. The sum function used in this example is from the previous section:

function partial($funcName, ...$args) { 
return function(...$innerArgs) use ($funcName, $args) {
$allArgs = array_merge($args, $innerArgs);
return call_user_func_array($funcName, $allArgs);
};
}

$sum = partial("sum", 10, 20);
$sum = $sum(30);

echo $sum;

Sometimes, we get confused between currying and partial application even though they are completely different in their approaches and principles.

As we can see, there are so many things to consider while dealing with functional programming in PHP. It will be a lengthier process to implement data structures using functional programming in PHP from scratch. In order to solve this problem, we will explore an excellent functional programming library for PHP, called Tarsana. It is open source and comes with the MIT license. We will explore this library and also use it as our base for functional data structure implementation in PHP.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset