PHP code example of baguette / copipe-iter

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

    

baguette / copipe-iter example snippets


$data = [1, 2, 3];
$twice = function ($n) { return $n * 2; };

foreach (map($data, $twice) as $a) {
    echo $a, PHP_EOL;
}
// [output] 2, 4, 6

$data = ['apple', 'orange', 'strawberry'];
$deco = function ($k, $v) { return "{$k}: $v"; };

foreach (map_kv($data, $deco) as $a) {
    echo $a, PHP_EOL;
}
// [output] "1: apple", "2: orange", "3: strawberry"

$data = range(1, 100);

foreach (take($data, 5) as $a) {
    echo $a, PHP_EOL;
}
// [output] 1, 2, 3, 4, 5

$fib = function () {
    $n = 0; yield $n;
    $m = 1; yield $m;

    while (true) {
        $c = $n + $m;
        yield $c;
        $n = $m;
        $m = $c;
    }
};

$fib_10 = to_array(take($fib(), 10));
//=> [0, 1, 1, 2, 3, 5, 8, 13, 21, 34]