PHP code example of dooaki / container

1. Go to this page and download the library: Download dooaki/container 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/ */

    

dooaki / container example snippets




use dooaki\Container\Lazy\Enumerable;

class CountUp
{
    use Enumerable;

    public function each()
    {
        $i=0;
        while(1) {
            yield ++$i;
        }
    }
}

print_r((new CountUp())->take(3)->toArray());
// Array
// (
//     [0] => 1
//     [1] => 2
//     [2] => 3
// )




use dooaki\Container\Lazy\Enumerator;

function infinity() {
    $i = 0;
    while (++$i) {
        yield $i;
    }
}

$e = new Enumerator(function () { return infinity(); });

// Enumerator use Enumerable
$e->skip(10)
    ->select(function ($i) { return $i % 2; })
    ->take(5)
    ->each(function ($i) { echo $i, ' '; }); // 11 13 15 17 19


$a = Enumerator::from([1,2,3])
    ->map(functino($v) { return $v * 2 })
    ->toArray();
print_r($a);
/*
Array
(
    [0] => 2
    [1] => 4
    [2] => 6
)
*/