You can install the package via composer:
composer require isaeken/loops
loop(5, function (\IsaEken\Loops\Index $index) {
return $index->odd;
}); // [false, true, false, true, false]
$loop = async_loop(5, function (\IsaEken\Loops\Index $index) {
return $index->even;
});
// ...
await($loop); // [true, false, true, false, true]
$callback = new class implements \IsaEken\Loops\Contracts\LoopCallback {
public function __invoke(Index $index, Loop $loop = null): int
{
return $index->index;
}
};
$loop = new Loop(2, $callback);
$loop->run();
$loop->results(); // [0, 1]
loop(2 ,function (\IsaEken\Loops\Index $index, \IsaEken\Loops\Loop $loop) {
return [
'iteration' => $index->iteration,
'index' => $index->index,
'remaining' => $index->remaining,
'count' => $index->count,
'first' => $index->first,
'last' => $index->last,
'odd' => $index->odd,
'even' => $index->even,
];
});
// [
// [
// 'iteration' => 1,
// 'index' => 0,
// 'remaining' => 1,
// 'count' => 2,
// 'first' => true,
// 'last' => false,
// 'odd' => false,
// 'even' => true,
// ],
// [
// 'iteration' => 2,
// 'index' => 1,
// 'remaining' => 0,
// 'count' => 2,
// 'first' => false,
// 'last' => true,
// 'odd' => true,
// 'even' => false,
// ]
// ]
loop(3, function (\IsaEken\Loops\Index $index, \IsaEken\Loops\Loop $loop) {
if ($index->index > 1) {
$loop->break();
}
return $index->index;
}); // [0, 1]
loop_random(function (\IsaEken\Loops\Index $index, \IsaEken\Loops\Loop $loop) {
return $index->index;
}); // executed random times.
$min = 5;
$max = 10;
loop_random(function (\IsaEken\Loops\Index $index, \IsaEken\Loops\Loop $loop) {
return $index->index;
}, $min, $max);
loop_random(function (\IsaEken\Loops\Index $index) {
return $index->even;
}, seed: 123456789);
Please see CHANGELOG for more information on what has changed recently.
Please see CONTRIBUTING for details.
Please review our security policy on how to report security vulnerabilities.
The MIT License (MIT). Please see License File for more information.