Ejemplo n.º 1
0
Archivo: a.php Proyecto: 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);
}
Ejemplo n.º 2
0
Archivo: ATest.php Proyecto: ihor/Nspl
 public function testIndexed()
 {
     $animals = [array('id' => 9, 'type' => 'cat', 'name' => 'Snowball'), array('id' => 10, 'type' => 'dog', 'name' => 'Santa\'s Little Helper'), array('id' => 11, 'type' => 'cat', 'name' => 'Fluffy')];
     $this->assertEquals(array(9 => array('id' => 9, 'type' => 'cat', 'name' => 'Snowball'), 10 => array('id' => 10, 'type' => 'dog', 'name' => 'Santa\'s Little Helper'), 11 => array('id' => 11, 'type' => 'cat', 'name' => 'Fluffy')), indexed($animals, 'id'));
     $this->assertEquals(array(9 => array('id' => 9, 'type' => 'cat', 'name' => 'Snowball'), 10 => array('id' => 10, 'type' => 'dog', 'name' => 'Santa\'s Little Helper'), 11 => array('id' => 11, 'type' => 'cat', 'name' => 'Fluffy')), indexed(new \ArrayIterator($animals), 'id'));
     $this->assertEquals(array('cat' => array('id' => 11, 'type' => 'cat', 'name' => 'Fluffy'), 'dog' => array('id' => 10, 'type' => 'dog', 'name' => 'Santa\'s Little Helper')), indexed($animals, 'type'));
     $this->assertEquals(array('cat' => [array('id' => 9, 'type' => 'cat', 'name' => 'Snowball'), array('id' => 11, 'type' => 'cat', 'name' => 'Fluffy')], 'dog' => [array('id' => 10, 'type' => 'dog', 'name' => 'Santa\'s Little Helper')]), indexed($animals, 'type', false));
     $this->assertEquals(array(3 => [array('id' => 9, 'type' => 'cat', 'name' => 'Snowball'), array('id' => 10, 'type' => 'dog', 'name' => 'Santa\'s Little Helper'), array('id' => 11, 'type' => 'cat', 'name' => 'Fluffy')]), indexed($animals, function ($animal) {
         return strlen($animal['type']);
     }, false));
     $this->assertEquals(array('Snowball' => 'cat', 'Santa\'s Little Helper' => 'dog', 'Fluffy' => 'cat'), indexed($animals, 'name', true, function ($animal) {
         return $animal['type'];
     }));
     $this->assertEquals(array(9 => array('id' => 9, 'type' => 'cat', 'name' => 'Snowball'), 10 => array('id' => 10, 'type' => 'dog', 'name' => 'Santa\'s Little Helper'), 11 => array('id' => 11, 'type' => 'cat', 'name' => 'Fluffy')), call_user_func(indexed, $animals, 'id'));
     $this->assertEquals('\\nspl\\a\\indexed', indexed);
 }