isaeken/loops

基本循环库

资助包维护!
isaeken

v3.1.3 2022-02-09 06:43 UTC

This package is auto-updated.

Last update: 2024-09-09 12:43:00 UTC


README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

安装

您可以通过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);

变更日志

请查看 CHANGELOG 了解最近更改的详细信息。

贡献

请查看 CONTRIBUTING 了解详情。

安全漏洞

请查阅 我们的安全策略 了解如何报告安全漏洞。

鸣谢

许可证

MIT许可证(MIT)。请参阅 许可证文件 了解更多信息。