PHP code example of suramon / itertools

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

    

suramon / itertools example snippets


// iterate the lines of a csv file
$lines = new FileLineIterator('file.csv');

// filter all non unique lines
$uniqueLines = new UniqueIterator($lines);

// convert unique csv string lines to array
$rows = new StringCsvIterator($uniqueLines);

// extract column 1 from the csv file
$column1 = new MapIterator($rows, function($row) { return $row['column1']; });

// output all rows in parallel
foreach(new ForkingIterator($column1) as $row) {
    echo $row;
}

$iterators = new ArrayIterator(array(new RangeIterator(1, 5), new RangeIterator(6, 10)));
$fattenedIterators = new ChainIterator($iterators); // will contain all numbers from 1 to 10

$iterator = new RangeIterator();
$batchsize = 100;
foreach(new ChunkingIterator($iterator, $batchsize) as $chunk) {
    $pdo->starttransaction();
    foreach($chunk as $element) {
        // process the iterator elements. using the transaction inside the chunkiterator makes sure the transaction stays small
    }
    $pdo->commit();
}

$elements = new RangeIterator(0, 10);
foreach(new ForkingIterator($elements) as $i) {
    var_dump($i, getmypid()); // wil spawn a new process to iterate each element
}

$range = new HistoryIterator(new ArrayIterator(range(1, 10)));
foreach($range as $i) {
    if($range->hasPrev()) {
        echo $i, $range->prev(), "\n";
    }
}

$positiveNumbers = new RangeIterator(0, INF); // all numbers from 0 to infinity
$positiveSquareNumbers = new MapIterator($positiveNumbers, function($n) {return $n*$n;}); // all positive square numbers

$uniqueEntries = new UniqueIterator(new ArrayIterator(array(1, 2, 2, 2, 3, 4, 2))); // will contain 1, 2, 3, 4, 2

$lines = new SliceIterator(new FileLineIterator('file.txt'), 0, 1000); // will iterate the first 1000 lines of the file

$csv1 = new FileCsvIterator('file1.csv');
$csv2 = new FileCsvIterator('file2.csv');
foreach(new ZipIterator(array($csv1, $csv2)) as $combinedRows) {
    $row1 = $combinedRows[0]; // a row in file1.csv
    $row2 = $combinedRows[1]; // row in file2.csv on same position
}