PHP code example of thesebas / itertools

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

    

thesebas / itertools example snippets


expect(iterator_to_array(gen(3)))->toBe(['a', 'b', 'c']);

$actual = tail(gen(5), 3, false);
expect(iterator_to_array($actual))->toBe(['c', 'd', 'e']);

$actual = head(gen(10), 3);
expect(iterator_to_array($actual))->toBe(['a', 'b', 'c']);


$actual = skip(gen(10), 4);
expect(iterator_to_array($actual))->toBe(['e', 'f', 'g', 'h', 'i', 'j']);

list($left, $right) = tee(gen(10));


expect(iterator_to_array(head($left, 3)))->toBe(['a', 'b', 'c']);
expect(iterator_to_array(head($right, 5)))->toBe(['a', 'b', 'c', 'd', 'e']);

expect(iterator_to_array(head($left, 5)))->toBe(['d', 'e', 'f', 'g', 'h']);
expect(iterator_to_array(head($right, 2)))->toBe(['f', 'g']);

expect(iterator_to_array(head($left, 2)))->toBe(['i', 'j']);
expect(iterator_to_array(head($right, 3)))->toBe(['h', 'i', 'j']);

$actual = chain(gen(5), gen(3));
expect(iterator_to_array($actual))->toBe(['a', 'b', 'c', 'd', 'e', 'a', 'b', 'c']);

$actual = filter(gen(10), function ($item, $key) {
    return $key % 2 == 1;
});
expect(iterator_to_array($actual))->toBe(['b', 'd', 'f', 'h', 'j']);


$actual = map(gen(3), function ($item, $key) {
    return "item {$key}: {$item}";
});

expect(iterator_to_array($actual))->toBe(['item 0: a', 'item 1: b', 'item 2: c']);

$actual = \iterator_to_array(map(chunk(gen(10), 3), '\\iterator_to_array'));
expect($actual)->toBe([
    ['a', 'b', 'c'],
    ['d', 'e', 'f'],
    ['g', 'h', 'i'],
    ['j']
]);