Example #1
0
display('5.1. Sorted by the 1st character with nspl\\op\\itemGetter', $sortedByTheFirstCharacter);
$sortedByTheFirstCharacterDesc = sorted(['bc', 'a', 'abc'], true, itemGetter(0));
display('5.2. Sorted by the 1st character with nspl\\op\\itemGetter in descending order', $sortedByTheFirstCharacterDesc);
// 6. Sort array with comparison function (order word by the 1st character)
$sortedByTheFirstCharacter = sorted(['bc', 'a', 'abc'], false, null, function ($v1, $v2) {
    return chr($v1[0]) - chr($v2[0]);
});
display('6. Sorted by the 1st character with a comparison function', $sortedByTheFirstCharacter);
// 7. Sort list of strings lexicographically
$sortedLexicographically = sorted(['bc', 'a', 'abc'], false, null, 'strcmp');
display('7. Sorted lexicographically', $sortedLexicographically);
// 8. Sort multidimensional array (sort list of users by their names)
$users = [array('name' => 'Robert', 'age' => 20), array('name' => 'Alex', 'age' => 30), array('name' => 'Jack', 'age' => 25)];
$sortedByName = sorted($users, itemGetter('name'));
display('8.1. Users sorted by name', $sortedByName);
$sortedByNameDesc = sorted($users, true, itemGetter('name'));
display('8.2. Users sorted by name in descending order', $sortedByNameDesc);
// 9. Sort list of objects by property value (sort list of users by their name)
$users = [new User('Robert', 20), new User('Alex', 30), new User('Jack', 25)];
$sortedByName = sorted($users, propertyGetter('name'));
display('9.1. Users presented as list of objects sorted by name', $sortedByName);
$sortedByNameDesc = sorted($users, true, propertyGetter('name'));
display('9.2. Users presented as list of objects sorted by name in descending order', $sortedByNameDesc);
// 10. Sort list of objects by method result (sort list of users by their age)
$sortedByAge = sorted($users, methodCaller('getAge'));
display('10.1. Users presented as list of objects sorted by age', $sortedByAge);
$sortedByAgeDesc = sorted($users, true, methodCaller('getAge'));
display('10.2. Users presented as list of objects sorted by age in descending order', $sortedByAgeDesc);
class User
{
    public $name;
Example #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";
Example #3
0
File: OpTest.php Project: ihor/Nspl
 public function testItemGetter()
 {
     $first = itemGetter(0);
     $second = itemGetter(1);
     $firstAndSecond = itemGetter(0, 1);
     $this->assertEquals(1, $first([1, 2]));
     $this->assertEquals(2, $second([1, 2]));
     $this->assertEquals([1, 2], $firstAndSecond([1, 2, 3]));
     $users = [array('name' => 'John', 'age' => 18), array('name' => 'Jack', 'age' => 20), array('name' => 'Sarah', 'age' => 19)];
     $this->assertEquals(['John', 'Jack', 'Sarah'], map(itemGetter('name'), $users));
 }