1. Go to this page and download the library: Download ihor/nspl 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/ */
ihor / nspl example snippets
// get user ids
$userIds = map(propertyGetter('id'), $users);
// or sort them by age
$sortedByAge = sorted($users, methodCaller('getAge'));
// or check if they all are online
$online = all($users, methodCaller('isOnline'));
// or define new function as composition of the existing ones
$flatMap = compose(rpartial(flatten, 1), map);
// get user ids
$userIds = array_map(function($user) { return $user->id; }, $users);
// sort them by age, note that the following code modifies the original users array
usort($users, function($user1, $user2) {
return $user1->getAge() - $user2->getAge();
});
// check if they all are online
$online = true;
foreach ($users as $user) {
if (!$user->isOnline()) {
$online = false;
break;
}
}
// define new function as composition of the existing ones
$flatMap = function($function, $list) {
// note the inconsistency in array_map and array_reduce parameters
return array_reduce(array_map($function, $list), 'array_merge', []);
};
use function nspl\a\zip;
$pairs = zip([1, 2, 3], ['a', 'b', 'c']);
use nspl\a;
$pairs = a\zip([1, 2, 3], ['a', 'b', 'c']);
use const \nspl\a\flatten;
use const \nspl\a\map;
use function \nspl\f\compose;
use function \nspl\f\partial;
use function \nspl\f\rpartial;
$flatMap = compose(rpartial(flatten, 1), map);
assert(['hello', 'world', 'foo', 'bar'] === $flatMap(partial('explode', ' '), ['hello world', 'foo bar']));
use const \nspl\op\sum;
use const \nspl\a\filter;
use const \nspl\a\map;
use const \nspl\a\reduce;
use function \nspl\f\partial;
$isEven = function($x) { return $x % 2 === 0; };
$square = function($x) { return $x * $x; };
// sum of squares of all even numbers less than 20
$sum = pipe(
range(1, 20),
partial(filter, $isEven),
partial(map, $square),
partial(reduce, sum)
);
> use function \nspl\a\with;
>
> $sum = with(range(1, 20))
> ->filter($isEven)
> ->map($square)
> ->reduce(sum);
>
assert(6 === reduce(function($a, $b) { return $a + $b; }, [1, 2, 3]));
// Which is the same as
use const \nspl\op\sum;
assert(6 === reduce(sum, [1, 2, 3]));
// Calls generator function and logs the yielded values
function logged(callable $generatorFunction)
{
static $count = 1;
return function(...$args) use ($generatorFunction, &$count) {
foreach ($generatorFunction(...$args) as $value) {
echo $count++ . '. ' . (string) $generatorFunction . ' -> ' . $value . "\n";
yield $value;
};
};
}
function naturalNumbers()
{
$current = 1;
while (true) yield $current++;
}
const naturalNumbers = 'naturalNumbers';
// Returns square of a number
function square($n)
{
return $n * $n;
}
const square = 'square';
// Checks if a number is even
function isEven($n)
{
return $n % 2 === 0;
}
const isEven = 'isEven';
use const nspl\a\lazy\{take, map, filter};
$map = logged(map);
$take = logged(take);
$filter = logged(filter);
$numbers = logged(naturalNumbers)();
$evenNumbers = $filter(isEven, $numbers); // filter only even numbers
$firstThreeEvenNumbers = $take($evenNumbers, 3); // take only first 3 even numbers
$result = $map(square, $firstThreeEvenNumbers); // and calculate their squares
foreach ($result as $value) {
echo "\nNext value is $value \n\n";
}
use const \nspl\args\int;
use const \nspl\args\string;
use const \nspl\args\arrayAccess;
use function \nspl\args\expects;
function nth($sequence, $n)
{
expects([arrayAccess, string], $sequence);
expects(int, $n);
return $sequence[$n];
}
nth('hello world', 'blah');
use const \nspl\args\numeric;
use function \nspl\args\expects;
function sum($x, $y)
{
expectsAll(numeric, [$x, $y]);
return $x + $y;
}