【Laravel6】LazyCollection触った (素数の無限シーケンス)

LaravelPHP

書いたやつ

<?php

declare(strict_types=1);

namespace Tests\Feature;

use Tests\TestCase;
use Illuminate\Support\LazyCollection;

class ExampleTest extends TestCase
{
    /**
     * 素数の無限シーケンスを返す
     * @return LazyCollection
     */
    function primes(): LazyCollection
    {
        return $this->sieve(
            $this->sequence()
                ->filter($this->gte(2))
        );
    }


    /**
     * @test
     */
    public function sample()
    {
        $taken = $this->primes()->take(100);

        echo $taken->join(', ');

        $this->assertTrue(true);
    }

    // ----------------------------------------
    // helpers
    // ----------------------------------------

    /**
     * 正の整数の無限シーケンスを返す
     * @return LazyCollection
     */
    function sequence(): LazyCollection
    {
        return new LazyCollection(function () {
            for ($i = 0;; ++$i) {
                yield $i;
            }
        });
    }

    /**
     * greater than述語の右辺部分適用を返す
     * @param int $right 右辺
     * @return callable
     */
    function gte(int $right): callable
    {
        return function (int $n) use ($right) {
            return $n >= $right;
        };
    }

    /**
     * $divisorで割り切れればtrueという述語を返す
     * @param int $divisor 除数
     * @return callable
     */
    function divisible(int $divisor)
    {
        assert($divisor !== 0);

        return function (int $n) use ($divisor) {
            return $n % $divisor === 0;
        };
    }

    /**
     * ふるい
     * @param LazyCollection $sequence ふるう前の無限シーケンス
     * @return LazyCollection 素数の無限シーケンス
     */
    function sieve(LazyCollection $sequence): LazyCollection
    {
        $head = $sequence->first();
        $tail = $sequence->skip(1);
        $tailSieved = $tail->reject($this->divisible($head));

        return (new LazyCollection($head))
            ->concat(new LazyCollection(
                function () use ($tailSieved) {
                    return $this->sieve($tailSieved);
                }
            ));
    }

}

結果

bash-4.4# ./vendor/bin/phpunit  tests/Feature/ExampleTest.php
PHPUnit 8.3.4 by Sebastian Bergmann and contributors.

.                                                                   1 / 1 (100%)
2, 3, 5, 7, 11, 13, 17, 19, 23, 29, 31, 37, 41, 43, 47, 53, 59, 61, 67, 71, 73, 79, 83, 89, 97, 101, 103, 107, 109, 113, 127, 131, 137, 139, 149, 151, 157, 163, 167, 173, 179, 181, 191, 193, 197, 199, 211, 223, 227, 229, 233, 239, 241, 251, 257, 263, 269, 271, 277, 281, 283, 293, 307, 311, 313, 317, 331, 337, 347, 349, 353, 359, 367, 373, 379, 383, 389, 397, 401, 409, 419, 421, 431, 433, 439, 443, 449, 457, 461, 463, 467, 479, 487, 491, 499, 503, 509, 521, 523, 541

Time: 1.21 seconds, Memory: 10.00 MB

OK (1 test, 1 assertion)

補遺

<?php
        return (new LazyCollection($head))
            ->concat(new LazyCollection(
                function () use ($tailSieved) {
                    return $this->sieve($tailSieved);
                }
            ));

このへんのクソコード感がすごい

無限シーケンス目的なら生のジェネレータを使ったほうがいいのかもしれない