1. Go to this page and download the library: Download bartfeenstra/fu 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/ */
bartfeenstra / fu example snippets
use BartFeenstra\Functional as F;
use BartFeenstra\Functional\Iterable as I;
use BartFeenstra\Functional\Predicate as P;
use function BartFeenstra\Functional\Iterable\iter;
// Arrays.
$iterator = iter([3, 1, 4]);
// \Traversable (that (return callables that...) return iterators.
$callable = function (){
return function () {
return iter([]);
};
};
$iterator = iter($callable);
// Existing universal iterators are passed through.
$iterator = iter([]);
assert($iterator === iter($iterator));
// Objects can expose universal iterators as well.
$toIterator = new class() implements I\ToIterator {
public function iter(): I\Iterator {
return iter([]);
}
};
$iterator = iter($toIterator);
// Try executing a callable, catch all exceptions, and output a Result.
$result = F\try_except(function () {/** ... */});
// Try executing a callable, catch all Foo, Bar, Baz, and Qux exceptions, and output a Result.
$result = F\try_except(function () {/** ... */}, Foo::class, Bar::class, Baz::class, Qux::class);
// Try executing a callable at most twice, catch all exceptions, and output a Result.
$result = F\retry_except(function () {/** ... */});
// Try executing a callable at most 5 times, catch all Foo, Bar, Baz, and Qux exceptions, and output a Result.
$result = F\retry_except(function () {/** ... */}, 5, Foo::class, Bar::class, Baz::class, Qux::class);
// All values strictly identical to TRUE.
$predicate = P\true();
// All values strictly identical to FALSE.
$predicate = P\false();
// All values that evaluate to TRUE.
$predicate = P\truthy();
// All values that evaluate to FALSE.
$predicate = P\falsy();
// All values strictly identical to 0.
$predicate = P\id(0);
// All values equal to "Apples and oranges".
$predicate = P\eq('Apples and oranges');
// All values greater than 9.
$predicate = P\gt(9);
// All values greater than or equal to 99.
$predicate = P\ge(99);
// All values lesser than 15.
$predicate = P\lt(15);
// All values lesser than or equal to 666.
$predicate = P\le(666);
// All values that are instances of Foo, Bar, Baz, or Qux.
$predicate = P\instance_of(Foo::class, Bar::class, Baz::class, Qux::class);
// One or more values are lesser than 0 OR greater than 9.
$predicate = P\any(P\lt(0), P\gt(9));
// All values are greater than 0 AND lesser than 9.
$predicate = P\all(P\gt(0), P\lt(9));
// All values different from "Apples and oranges".
$predicate = P\not(P\eq('Apples and oranges'));
use BartFeenstra\Functional\Option;
use BartFeenstra\Functional\Some;
use BartFeenstra\Functional\SomeValue;
use BartFeenstra\Functional\None;
function get_option(): Option {
if (true) {
return new SomeValue(666);
}
return new None();
}
function handle_option(Option $value) {
if ($value instanceof Some) {
print sprintf('The value is %s.', $value());
}
// $value is an instance of None.
else {
print 'No value could be retrieved.';
}
}
handle_option(get_option());
use BartFeenstra\Functional\Ok;
use BartFeenstra\Functional\OkValue;
use BartFeenstra\Functional\Result;
use BartFeenstra\Functional\ThrowableError;
function get_result(): Result {
try {
// Do some things that may throw a ResultComputationException.
return new OkValue(666);
}
catch (\ResultComputationException $e) {
return new ThrowableError($e);
}
}
function handle_result(Result $result) {
if ($result instanceof Ok) {
print sprintf('The value is %s.', $result());
}
// $value is an instance of Error.
else {
print sprintf('An error occurred: %s.', $result);
}
}
handle_result(get_result());
$originalFunction = function (string $a, string $b, string $c, string $d): string {
return $a . $b . $c . $d;
};
// Fix the two first/left-handed arguments.
$newFunction = F\apply_l($originalFunction, 'A', 'B');
assert('ABCD' === $newFunction('C', 'D'));
// Fix the two last/right-handed arguments.
$newFunction = F\apply_r($originalFunction, 'C', 'D');
assert('ABCD' === $newFunction('A', 'B'));
// Fix two arguments by index/in the middle.
$newFunction = F\apply_i($originalFunction, 1, 'B', 'C');
assert('ABCD' === $newFunction('A', 'D'));