Exemplo n.º 1
0
 public function testAll()
 {
     $this->assertTrue(all([true, true, true]));
     $this->assertTrue(all([true, 1, 'a', [1], new \StdClass()]));
     $this->assertTrue(all([]));
     $this->assertFalse(all([true, true, false]));
     $this->assertFalse(all([true, 0, 'a', [1], new \StdClass()]));
     $this->assertFalse(all([null, true, 1, 'a', [1], new \StdClass()]));
     $this->assertFalse(all([true, 1, 'a', [], new \StdClass()]));
     $this->assertFalse(all([true, 1, '', [1], new \StdClass()]));
     $this->assertTrue(all([19, 20, 21], function ($v) {
         return $v > 18;
     }));
     $this->assertFalse(all([19, 20, 21], function ($v) {
         return $v < 18;
     }));
     $this->assertTrue(call_user_func(all, [true, true, true]));
     $this->assertEquals('\\nspl\\a\\all', all);
 }
Exemplo n.º 2
0
<?php

require_once __DIR__ . '/../autoload.php';
use const nspl\op\object;
use const nspl\op\gt;
use function nspl\op\itemGetter;
use function nspl\op\propertyGetter;
use function nspl\f\map;
use function nspl\f\rpartial;
use function nspl\a\all;
use function nspl\a\any;
use function nspl\a\sorted;
$users = [array('name' => 'John', 'age' => 15), array('name' => 'Jack', 'age' => 35), array('name' => 'Sarah', 'age' => 25), array('name' => 'Norah', 'age' => 20), array('name' => 'Michael', 'age' => 30)];
// 1. Get user names from list of users presented with array data
$names = map(itemGetter('name'), $users);
echo sprintf("User names are: %s (users were presented with array data)\n", implode(', ', $names));
// 2. Convert list of user presented with array data to list of objects
$objects = map(object, $users);
echo sprintf("List of users converted to objects consists of types: %s\n", implode(', ', map('\\nspl\\ds\\getType', $objects)));
// 3. Sort users by age
$sorted = sorted($users, false, itemGetter('age'));
echo "Users sorted by age:\n";
foreach ($sorted as $user) {
    echo sprintf("    %s - %s y.o.\n", $user['name'], $user['age']);
}
// 4. Check if all numbers are positive
$allPositive = all([1, 2, 3, 4, 5], rpartial(gt, 0));
echo $allPositive ? "All numbers are positive\n" : "At least one number was not positive\n";
Exemplo n.º 3
0
Arquivo: a.php Projeto: ihor/Nspl
use function nspl\a\reduce;
use function nspl\a\filter;
use function nspl\a\sorted;
use function nspl\a\keySorted;
use function nspl\a\indexed;
use function nspl\a\take;
use function nspl\a\reorder;
use const nspl\op\eq;
use const nspl\op\object;
use function nspl\op\itemGetter;
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;