Example #1
0
 public function testReduce()
 {
     $this->assertEquals(6, reduce(function ($a, $b) {
         return $a + $b;
     }, [1, 2, 3]));
     $this->assertEquals('abc', reduce(function ($a, $b) {
         return $a . $b;
     }, new \ArrayIterator(['a', 'b', 'c']), ''));
     $this->assertEquals(64, reduce('pow', array('a' => 3, 'b' => 2, 'c' => 1), 2));
     $this->assertEquals(0, reduce(function ($a, $b) {
         return $a * $b;
     }, [], 0));
     $this->assertEquals(1, reduce(function ($a, $b) {
         return $a * $b;
     }, [], 1));
     $this->assertEquals(6, call_user_func(reduce, function ($a, $b) {
         return $a + $b;
     }, [1, 2, 3]));
     $this->assertEquals('\\nspl\\f\\reduce', reduce);
 }
Example #2
0
$userIds = map(propertyGetter('id'), $users);
echo sprintf("User ids are: %s\n", implode(', ', $userIds));
// 2. Count users younger than 25
$youngerThan25Count = reduce(function ($count, $user) {
    return $count + (int) ($user->age < 25);
}, $users);
echo sprintf("%s users are younger than 25\n", $youngerThan25Count);
// 3. Get users younger than 25
$youngerThan25 = filter(function ($user) {
    return $user->age < 25;
}, $users);
echo "These users are younger than 25:\n";
foreach ($youngerThan25 as $user) {
    echo sprintf("    %s - %s y.o.\n", $user->name, $user->age);
}
// 4. Get users older than 25
$isOlderThan25 = compose(rpartial(gt, 25), propertyGetter('age'));
$olderThan25 = filter($isOlderThan25, $users);
echo "These users are older than 25:\n";
foreach ($olderThan25 as $user) {
    echo sprintf("    %s - %s y.o.\n", $user->name, $user->age);
}
// 5. Memoizing heavy calculations
$factorial = function ($n) {
    echo "Calculating {$n}!\n";
    return reduce(mul, range(1, $n), 1);
};
$memoizedFactorial = memoized($factorial);
foreach ([3, 3, 5, 5, 5] as $n) {
    echo sprintf("%s! = %s\n", $n, $memoizedFactorial($n));
}
Example #3
0
use function nspl\op\itemGetter;
use function nspl\op\propertyGetter;
use function nspl\f\map;
use function nspl\f\reduce;
use function nspl\f\flipped;
use function nspl\f\partial;
// 1. Check all statuses are "ready"
$statuses = ['ready', 'ready', 'not-ready'];
$ready = all($statuses, partial(eq, 'ready'));
echo $ready ? "Everybody is ready\n" : "Someone is not ready\n";
// 2. Check at least someone is "ready"
$someoneIsReady = any($statuses, partial(eq, 'ready'));
echo $someoneIsReady ? "Someone is ready\n" : "Everybody is not ready\n";
// 3. Get user name from which can be stored as username, user_name or name in data array
$data = array('id' => 1337, 'name' => 'John', 'gender' => 'male');
$name = reduce(flipped(partial(getByKey, $data)), ['username', 'user_name', 'name'], '');
echo sprintf("User name is %s\n", $name);
// 4. Sort list of user objects by their name
$users = map(object, [array('id' => 1, 'name' => 'John', 'age' => 15), array('id' => 2, 'name' => 'Jack', 'age' => 35), array('id' => 3, 'name' => 'Sarah', 'age' => 25), array('id' => 4, 'name' => 'Norah', 'age' => 20), array('id' => 5, 'name' => 'Michael', 'age' => 30), array('id' => 6, 'name' => 'Bob', 'age' => 30)]);
$usersSortedByName = sorted($users, false, propertyGetter('name'));
echo "Users sorted by name:\n";
foreach ($usersSortedByName as $user) {
    echo sprintf("    %s\n", $user->name);
}
// 5. Index users by ids
$usersIndexedByIds = indexed($users, propertyGetter('id'));
// In case of array it would be indexed($users, 'id')
echo "Users indexed by id:\n";
foreach ($usersIndexedByIds as $id => $user) {
    echo sprintf("    %s. %s\n", $id, $user->name);
}