<?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";
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); } // 7. Index users by ids $usersIndexedByIds = indexed($users, propertyGetter('id')); // In case of array it would be indexed($users, 'id') echo "Users indexed by id:\n"; foreach ($usersIndexedByIds as $id => $user) { 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) {
// 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 getName() { return $this->name; } public function getAge()
public function testSorted() { $this->assertEquals([1, 2, 3], sorted([2, 3, 1])); $this->assertEquals([1, 2, 3], sorted(new \ArrayIterator([2, 3, 1]))); $this->assertEquals(array('carrot' => 'c', 'banana' => 'b', 'apple' => 'a'), sorted(array('carrot' => 'c', 'apple' => 'a', 'banana' => 'b'), true)); $this->assertEquals(['forty two', 'answer', 'the', 'is'], sorted(['the', 'answer', 'is', 'forty two'], true, 'strlen')); $this->assertEquals(['is', 'the', 'answer', 'forty two'], sorted(['the', 'answer', 'is', 'forty two'], 'strlen')); $isFruit = function ($v) { return in_array($v, ['apple', 'orange']); }; $this->assertEquals(['apple', 'orange', 'cat'], sorted(['orange', 'cat', 'apple'], false, null, function ($v1, $v2) use($isFruit) { if (!$isFruit($v1)) { return 1; } if (!$isFruit($v2)) { return -1; } return $v1 > $v2; })); $this->assertEquals([], sorted([])); $this->assertEquals([1], sorted([1])); $this->assertEquals(array('b' => null, 'a' => null), sorted(array('b' => null, 'a' => null))); $list = [3, 1, 2]; $this->assertEquals([1, 2, 3], sorted($list)); $this->assertEquals([3, 1, 2], $list); $this->assertEquals([1, 2, 3], call_user_func(sorted, [2, 3, 1])); $this->assertEquals('\\nspl\\a\\sorted', sorted); }