suramon / itertools
Iterators implemented in PHP, copy-pasted from Python
Installs: 19 122
Dependents: 0
Suggesters: 0
Security: 0
Stars: 17
Watchers: 5
Forks: 6
Open Issues: 0
Requires
- php: >=5.3
Requires (Dev)
- guzzlehttp/streams: ^3.0
- phpunit/phpunit: ~5.7
- dev-master
- 1.6.5
- 1.6.4
- 1.6.3
- 1.6.2
- 1.6.1
- 1.5.39
- 1.5.38
- 1.5.37
- 1.5.36
- 1.5.35
- 1.5.34
- 1.5.33
- 1.5.32
- 1.5.31
- 1.5.30
- 1.5.29
- 1.5.28
- 1.5.27
- 1.5.26
- 1.5.25
- 1.5.24
- 1.5.23
- 1.5.22
- 1.5.21
- 1.5.20
- 1.5.19
- 1.5.18
- 1.5.17
- 1.5.16
- 1.5.15
- 1.5.14
- 1.5.13
- 1.5.12
- 1.5.11
- 1.5.10
- 1.5.9
- 1.5.8
- 1.5.7
- 1.5.6
- 1.5.5
- 1.5.4
- 1.5.3
- 1.5.2
- 1.5.1
- 1.5.0
- 1.4.1
- 1.4.0
- 1.3.0
- 1.2.0
- 1.1.1
- 1.1.0
- 1.0.0
- dev-fixing-a-very-confusing-error-message
- dev-newtests
- dev-composer
This package is not auto-updated.
Last update: 2024-12-21 15:08:39 UTC
README
A set of iterators for PHP based on pythons itertools.
Some iterator examples
// 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; }
Install Guide
composer require suramon/itertools
Manual Install
- Download itertools directly from GitHub, or clone it with Git:
git clone https://github.com/SuRaMoN/relike.git
- Include the autoload header located in
src/autload.php
(eg:require 'relike/src/autoload.php';
) or you can use psr-0 - Learn and use itertools by looking at the examples, tests and source code
Some iterators explained
ChainIterator
Iterator equivalent of flattening a 2-dimensional array.
$iterators = new ArrayIterator(array(new RangeIterator(1, 5), new RangeIterator(6, 10))); $fattenedIterators = new ChainIterator($iterators); // will contain all numbers from 1 to 10
ChunkingIterator
Splits a iterator into smaller chunks. this can be used for batch processing.
$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(); }
ForkingIterator
This linux-only iterator is designed to be iterated by a foreach loop and forks a new process for each iteration.
$elements = new RangeIterator(0, 10); foreach(new ForkingIterator($elements) as $i) { var_dump($i, getmypid()); // wil spawn a new process to iterate each element }
HistoryIterator
An iterator that keeps track of the elements it iterates. It differs from the CachingIterator in the standard PHP library because this implementations allows the history size to be specified.
$range = new HistoryIterator(new ArrayIterator(range(1, 10))); foreach($range as $i) { if($range->hasPrev()) { echo $i, $range->prev(), "\n"; } }
MapIterator
Iterator equivalent or array_map.
$positiveNumbers = new RangeIterator(0, INF); // all numbers from 0 to infinity $positiveSquareNumbers = new MapIterator($positiveNumbers, function($n) {return $n*$n;}); // all positive square numbers
SliceIterator
Iterator equivalent of array_slice.
$lines = new SliceIterator(new FileLineIterator('file.txt'), 0, 1000); // will iterate the first 1000 lines of the file
UniqueIterator
Iterator equivalent of array_unique but only works for sorted input.
$uniqueEntries = new UniqueIterator(new ArrayIterator(array(1, 2, 2, 2, 3, 4, 2))); // will contain 1, 2, 3, 4, 2
RangeIterator
Iterator equivalent of range.
$lines = new SliceIterator(new FileLineIterator('file.txt'), 0, 1000); // will iterate the first 1000 lines of the file
ZipIterator
Inspired by pythons zip function. It can be constructed with an array of iterators and it iterates all of its arguments at the same index, returning during each iteration an array of the elements of each iterator on the same iteration positon
$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 }