1. Go to this page and download the library: Download martinezdelariva/functional library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
martinezdelariva / functional example snippets
namespace Foo;
use function Martinezdelariva\Functional\pipe;
class Bar
{
public function baz()
{
pipe(
'trim',
'ucwords'
)(' string ');
}
}
namespace Foo;
use Martinezdelariva\Functional as F;
class Bar
{
public function baz()
{
F\pipe(
'trim',
'ucwords'
)(' string ');
}
}
function sum(int $one, int $two, int $three) : int {
return $one + $two + $three;
};
// curry firsts params
$sum3 = curry('sum', 1, 2);
$sum3(3); // output: 6
$sum3(5); // output: 8
// curry whole function
$sum = curry('sum');
$sum1 = $sum(1);
$sum1(2, 3); // output: 6
// curry with all params given
curry(sum::class, 1, 2, 3); // output: 6
function minus(int $one, int $two) : int {
return $one - $two;
};
$minus1 = curry_right('minus');
echo $minus1(1)(4); // output: 3
$patterns = [
'a' => "It's an 'a'",
'foo' => function ($input, $other) {
return "$input::$other";
},
'is_int' => function ($input) {
return $input + 1;
},
\stdClass::class => "It's an 'stdClass'",
_ => "Default"
];
// provinding params
match($patterns, 'a'); // output: "It's an 'a'"
// provinding one by one param due `match` is curried
$match = match($patterns);
$match(5); // output: 6
$match(new \stdClass()); // output: "It's an 'stdClass'"
$match('unknown'); // output: "Default"
// provinding param to callables with more params
$match('foo')('bar'); // output: foo::bar
$format = pipe(
'trim',
'ucwords',
function (string $names) : string {
return implode(', ', explode(' ', $names));
}
);
$format(' john mary andre'); // output: John, Mary, Andre