O'Reilly logo

Functional PHP by Gilles Crettenand

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Applicative functors

Let's take an instance of our identity functor, holding some integer and a curryied version of an add function:

<?php 
 
$add = curry(function(int $a, int $b) { return $a + $b; }); 
 
$id = new IdentityFunctor(5); 

Now, what happens when we try to map the $add parameter on our functor? Consider the following code:

<?php 
 
$hum = $id->map($add); 
 
echo get_class($hum->get()); 
// Closure 

As you may have guessed, our functor now contains a closure representing a partially applied add parameter with the value 5 as the first parameter. You can use the get method to retrieve the function and use it, but it is not really that useful.

Another possibility would be to map another function, taking our function as a parameter, and doing something with ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required