}); 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; public $age; public function __construct($name, $age) { $this->name = $name; $this->age = $age; }
public function testPropertyGetter() { $users = [new User('John', 18), new User('Jack', 20), new User('Sarah', 19)]; $this->assertEquals(['John', 'Jack', 'Sarah'], map(propertyGetter('name'), $users)); $this->assertEquals([18, 20, 19], map(propertyGetter('age'), $users)); $this->assertEquals([array('name' => 'John', 'age' => 18), array('name' => 'Jack', 'age' => 20), array('name' => 'Sarah', 'age' => 19)], map(propertyGetter('name', 'age'), $users)); }
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)); }
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); }