PHP code example of jeremeamia / iter8
1. Go to this page and download the library: Download jeremeamia/iter8 library . Choose the download type require .
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
jeremeamia / iter8 example snippets
const PEOPLE = [
['name' => 'Abby', 'age' => 19],
['name' => 'Benny', 'age' => 21],
['name' => 'Cally', 'age' => 22],
['name' => 'Danny', 'age' => 24],
['name' => 'Danny', 'age' => 24],
['name' => 'Eddy', 'age' => 18],
];
$iter = Gen::from(PEOPLE);
$iter = Iter::filter($iter, Func::compose([
Func::index('age'),
Func::operator('>=', 20),
]));
$iter = Iter::map($iter, Func::index('name'));
$iter = Iter::debounce($iter);
Iter::print($iter);
#> ['Benny', 'Cally', 'Danny']
$iter = Iter::pipe(Gen::from(PEOPLE), [
Pipe::filter(Func::compose([
Func::index('age'),
Func::operator('>=', 20),
])),
Pipe::map(Func::index('name')),
Pipe::debounce(),
]);
Iter::print($iter);
#> ['Benny', 'Cally', 'Danny']
$iter = Iter::pipe(Gen::from(PEOPLE), [
Pipe::pluck('age'),
Pipe::reduce('max'),
Pipe::switch(function (int $maxAge) {
return Gen::range(1, $maxAge);
}),
]);
Iter::print($iter);
#> [1, 2, 3, ..., 22, 23, 24]
$collection = Collection::from(PEOPLE)
->filter(Func::compose([
Func::index('age'),
Func::operator('>=', 20),
]))
->map(Func::index('name'))
->debounce();
$collection->print();
#> ['Benny', 'Cally', 'Danny']
$items = Gen::defer(function () use ($apiClient) {
$apiResult = $apiClient->getItems();
foreach ($apiResult['items'] as $data) {
yield Models\Item::fromArray($data);
}
});
// ...
// First iteration
foreach ($items as $item) { /* ... */ }
// ...
// Another iteration
foreach ($items as $item) { /* ... */ }
$apiResult = $apiClient->getItems();
$items = Iter::map($apiResult['items'], function (array $data) {
return Models\Item::fromArray($data);
});
$items = Iter::rewindable($items);
// ...
// First iteration
foreach ($items as $item) { /* ... */ }
// ...
// Another iteration
foreach ($items as $item) { /* ... */ }