Beispiel #1
0
Datei: op.php Projekt: ihor/Nspl
<?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\rpartial;
use function nspl\a\all;
use function nspl\a\any;
use function nspl\a\map;
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\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";
Beispiel #2
0
Datei: ds.php Projekt: ihor/Nspl
++$wordCounter['world'];
echo "Word counter:\n";
print_r($wordCounter->toArray());
// 2. Multidimensional default array
// Note that we create nested default array with an anonymous function.
// Otherwise, default array object will be shared across all parent array fields.
$matrix = defaultarray(function () {
    return defaultarray(0);
});
for ($i = 0; $i < 3; ++$i) {
    for ($j = 0; $j < 3; ++$j) {
        ++$matrix[$i][$j];
    }
}
echo "Matrix 3x3:\n";
print_r(map(methodCaller('toArray'), $matrix->toArray()));
// casting default array with all nested default arrays to PHP array
// 3. Set example
$set = set(1, 2);
$set->add('hello');
$set[] = 'world';
$set->update([3, 4], ['answer', 42]);
echo "Set:\n";
print_r($set->toArray());
foreach (['hello', 3, 4, 'answer', 42] as $element) {
    $set->delete($element);
}
echo "Set:\n";
print_r($set->toArray());
$array = [1, 2, 3];
$intersection = $set->intersection($array);
Beispiel #3
0
Datei: f.php Projekt: ihor/Nspl
require_once __DIR__ . '/../autoload.php';
use nspl\f;
use function nspl\f\partial;
use function nspl\f\rpartial;
use function nspl\f\flipped;
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);
Beispiel #4
0
 public function testMethodCaller()
 {
     $users = [new User('John', 18), new User('Jack', 20), new User('Sarah', 19)];
     $this->assertEquals(['John', 'Jack', 'Sarah'], map(methodCaller('getName'), $users));
     $this->assertEquals([18, 20, 19], map(methodCaller('getAge'), $users));
     $this->assertEquals([21, 23, 22], map(methodCaller('getAgeIn', [3]), $users));
 }
Beispiel #5
0
Datei: a.php Projekt: ihor/Nspl
    echo sprintf("    %s. %s\n", $id, $user->name);
}
// 8. Create a map (name => age) from users data
$usersAgeByName = indexed($users, propertyGetter('name'), true, propertyGetter('age'));
echo "Users age:\n";
foreach ($usersAgeByName as $name => $age) {
    echo sprintf("    %s is %s y.o.\n", $name, $age);
}
// 9. Get users with unique age (unique values in multidimensional array)
$usersWithUniqueAge = array_values(indexed($users, propertyGetter('age')));
echo "Users with unique age:\n";
foreach ($usersWithUniqueAge as $user) {
    echo sprintf("    %s is %s y.o.\n", $user->name, $user->age);
}
// 10. Group users by age range
$usersByAgeRange = keySorted(indexed($users, function ($user) {
    return floor($user->age / 10) * 10;
}, false));
echo "Users by age range:\n";
foreach ($usersByAgeRange as $age => $usersGroup) {
    echo sprintf("    %s-%s: %s\n", $age, $age + 9, implode(', ', map(propertyGetter('name'), $usersGroup)));
}
// 11. Get all numbers less than 20 which are divisible by 3
$numbers = take(range(3, 20), 20, 3);
echo sprintf("Numbers less than 20 which are divisible by 3: %s\n", implode(', ', $numbers));
// 12. Re-order pets rating
$petsRating = reorder(['dog', 'hamster', 'cat'], 2, 1);
echo "New pets rating:\n";
foreach ($petsRating as $pet) {
    echo sprintf("    %s\n", $pet);
}
Beispiel #6
0
 public function testMap()
 {
     $this->assertEquals(['A', 'B', 'C'], map('strtoupper', ['a', 'b', 'c']));
     $this->assertEquals([1, 4, 9], map(function ($v) {
         return $v * $v;
     }, new \ArrayIterator([1, 2, 3])));
     $this->assertEquals(['a' => 0, 'b' => 1, 'c' => 2], map('abs', array('a' => 0, 'b' => -1, 'c' => 2)));
     $this->assertEquals([], map('strtoupper', []));
     $range = function ($min, $max) {
         for ($i = $min; $i <= $max; ++$i) {
             (yield $i);
         }
     };
     $this->assertEquals([1, 4, 9], map(function ($v) {
         return $v * $v;
     }, $range(1, 3)));
     $this->assertEquals(['A', 'B', 'C'], call_user_func(map, 'strtoupper', ['a', 'b', 'c']));
     $this->assertEquals('\\nspl\\a\\map', map);
 }