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\\f\\map', map); }
require_once __DIR__ . '/../autoload.php'; use function nspl\ds\defaultarray; use function nspl\ds\dstring; use function nspl\f\map; use function nspl\op\methodCaller; // 1. Cast default array to regular PHP array $wordCounter = defaultarray(0); ++$wordCounter['hello']; ++$wordCounter['hello']; ++$wordCounter['hello']; ++$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. Dynamic email subject $subject = dstring()->addStr('Something bad happened on ')->addConstant('APPLICATION_ENV')->addStr(' at ')->addFunction('date', ['Y-m-d H:i:s']); define('APPLICATION_ENV', 'staging'); echo $subject . "\n";
<?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";
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)); }
require_once __DIR__ . '/../autoload.php'; use nspl\f; use function nspl\f\map; use function nspl\f\reduce; use function nspl\f\filter; use function nspl\f\rpartial; 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; $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 ids $userIds = map(propertyGetter('id'), $users); echo sprintf("User ids are: %s\n", implode(', ', $userIds)); // 2. 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); // 3. 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); } // 4. Get users older than 25
echo sprintf(" %s. %s\n", $id, $user->name); } // 6. 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); } // 7. 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); } // 8. 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))); } // 9. 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)); // 10. Re-order pets rating $petsRating = moveElement(['dog', 'hamster', 'cat'], 2, 1); echo "New pets rating:\n"; foreach ($petsRating as $pet) { echo sprintf(" %s\n", $pet); }