Example #1
0
File: a.php Project: ihor/Nspl
use function nspl\op\propertyGetter;
use function nspl\f\partial;
$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)]);
// 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 ids
$userIds = map(propertyGetter('id'), $users);
echo sprintf("User ids are: %s\n", implode(', ', $userIds));
// 4. 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);
// 5. 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);
}
// 6. Sort list of user objects by their name
$usersSortedByName = sorted($users, false, propertyGetter('name'));
echo "Users sorted by name:\n";
foreach ($usersSortedByName as $user) {
    echo sprintf("    %s\n", $user->name);
Example #2
0
File: f.php Project: ihor/Nspl
use function nspl\f\compose;
use function nspl\f\memoized;
use const nspl\op\object;
use const nspl\op\gt;
use const nspl\op\mul;
use function nspl\op\propertyGetter;
use const nspl\a\value;
use function nspl\a\map;
use function nspl\a\reduce;
use function nspl\a\filter;
$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)]);
// 1. 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(value, $data)), ['username', 'user_name', 'name'], '');
echo sprintf("User name is %s\n", $name);
// 2. 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);
}
// 3. 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
File: ATest.php Project: ihor/Nspl
 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\\a\\reduce', reduce);
 }