Download
IterTools PHP ? ?????????????? ??? ?????? ? ???????????? ??????????
????????????? ??? PHP ?????????? itertools ? Python.
??????????
IterTools ???????????? ??? ???? ??????????? ??? ????????????:
-
???????????? ? ?????
-
???????????? ? ?????? ?????????? ???????
?????? ???????????? ? ?????
foreach (Multi::zip(['a', 'b'], [1, 2]) as [$letter, $number]) {
print($letter . $number); // a1, b2
}
?????? ???????????? ? ?????? ?????????? ???????
$result = Stream::of([1, 1, 2, 2, 3, 4, 5])
->distinct() // [1, 2, 3, 4, 5]
->map(fn ($x) => $x2) // [1, 4, 9, 16, 25]
->filter(fn ($x) => $x < 10) // [1, 4, 9]
->toSum(); // 14
?????? ?????????? ????????????? ???????? ? ?????? iterable ??????????:
* array
* Iterator
* Generator
* Traversable
??????? ??????????
??????????? ??? ???????????? ? ??????
???????????? ?????????? ?????????
| ????? | ???????? | ?????? ???? |
|-----------------------------|---------------------------------------------------------------------------------------------|-------------------------------------|
| chain | ??????????????? ????????? ????????? | Multi::chain($list1, $list2) |
| zip | ??????????? ????????? ?????????, ???? ?? ?????????? ????? ???????? ???????? | Multi::zip($list1, $list2) |
| zipEqual | ??????????? ????????? ????????? ?????? ???????, ? ?????? ?????? ???????? ??????? ?????????? | Multi::zipEqual($list1, $list2) |
| zipLongest | ??????????? ????????? ?????????, ???? ?? ?????????? ????? ??????? ???????? | Multi::zipLongest($list1, $list2) |
???????????? ????? ?????????
| ????? | ???????? | ?????? ???? |
|------------------------------------------------|------------------------------------------------------------------------------|-------------------------------------------------------------|
| chunkwise | ????????? ?????????, ???????? ?? ????? | Single::chunkwise($data, $chunkSize) |
| chunkwiseOverlap | ????????? ?????????, ???????? ?? ????????????????????? ????? | Single::chunkwiseOverlap($data, $chunkSize, $overlapSize) |
| compress | ?????????? ???????? ?? ?????????, ????????? ?????????? | Single::compress($data, $selectors) |
| compressAssociative | ?????????? ???????? ?? ????????? ?? ???????? ?????? | Single::compressAssociative($data, $selectorKeys) |
| dropWhile | ?????????? ????????, ???? ???????? ?????????? ?????? | Single::dropWhile($data, $predicate) |
| filter | ?????????? ?????? ?? ????????, ??? ??????? ???????? ?????????? ?????? | Single::filter($data) |
| filterTrue | ?????????? ?????? ???????? ???????? ?? ????????? | Single::filterTrue($data) |
| filterFalse | ?????????? ?????? ?????? ???????? ?? ????????? | Single::filterFalse($data, $predicate) |
| filterKeys | ?????????? ?????? ?? ????????, ??? ?????? ??????? ???????? ?????????? ?????? | Single::filterKeys($data, $predicate) |
| flatMap | ??????????? ????????? ? ?????????? ?????????? ?? 1 ??????? ??????????? | Single::flaMap($data, $mapper) |
| flatten | ?????????????? ????????? ????????? | Single::flatten($data, [$dimensions]) |
| groupBy | ?????????? ???????? ????????? | Single::groupBy($data, $groupKeyFunction, [$itemKeyFunc]) |
| limit | ???????????? ???????????? ????????? ???????? ???????????? ?????? ???????? | Single::limit($data, $limit) |
| map | ??????????? ????????? ? ?????????????? callback-??????? | Single::map($data, $function) |
| pairwise | ????????? ????????? ??????? (? ??????????) | Single::pairwise($data) |
| reindex | ??????????????? key-value ????????? | Single::reindex($data, $reindexer) |
| repeat | ????????? ?????? ??????? ???????? ????? ??? | Single::repeat($item, $repetitions) |
| reverse | ????????? ????????? ? ???????? ??????? | Single::reverse($data) |
| slice | ?????????? ?????????? ????????? | Single::slice($data, [$start], [$count], [$step]) |
| string | ????????? ?????? ??????????? | Single::string($string) |
| takeWhile | ?????? ????????, ???? ???????? ?????????? ?????? | Single::takeWhile($data, $predicate) |
??????????? ????????????
| ????? | ???????? | ?????? ???? |
|------------------------------|------------------------------------------------------|----------------------------------|
| count | ?????????? ?????????? ?????????????????? ????? ????? | Infinite::count($start, $step) |
| cycle | ?????????? ??????????? ??????? ????????? | Infinite::cycle($collection) |
| repeat | ?????????? ????????? ?????? ???????? | Infinite::repeat($item) |
???????????? ????????? ????????
| ????? | ???????? | ?????? ???? |
|-------------------------------------------|------------------------------------------|--------------------------------------------|
| choice | ????????? ?????? ????????? ?? ?????? | Random::choice($list, $repetitions) |
| coinFlip | ????????? ?????? ?????? (0 ??? 1) | Random::coinFlip($repetitions) |
| number | ????????? ????? ????? | Random::number($min, $max, $repetitions) |
| percentage | ????????? ???????????? ????? ????? 0 ? 1 | Random::percentage($repetitions) |
| rockPaperScissors | ????????? ????? "??????-???????-??????" | Random::rockPaperScissors($repetitions) |
?????????????? ????????????
| ????? | ???????? | ?????? ???? |
|--------------------------------------------|-------------------------------------|----------------------------------------------------|
| runningAverage | ?????????? ???????? ??????????????? | Math::runningAverage($numbers, $initialValue) |
| runningDifference | ?????????? ???????? | Math::runningDifference($numbers, $initialValue) |
| runningMax | ????? ????????????? ???????? | Math::runningMax($numbers, $initialValue) |
| runningMin | ????? ???????????? ???????? | Math::runningMin($numbers, $initialValue) |
| runningProduct | ?????????? ???????????? | Math::runningProduct($numbers, $initialValue) |
| runningTotal | ?????????? ????? | Math::runningTotal($numbers, $initialValue) |
???????????? ???????? ? ??????????????
| ????? | ???????? | ?????? ???? |
|-----------------------------------------------------------------|-----------------------------------------------------------------------|--------------------------------------------------------------|
| distinct | ????????? ?????????, ???????? ?????? ?????????? ???????? | Set::distinct($data) |
| intersection | ??????????? ?????????? ????????? | Set::intersection(...$iterables) |
| intersectionCoercive | ??????????? ?????????? ????????? ? ?????? ?????????? ????? | Set::intersectionCoercive(...$iterables) |
| partialIntersection | ????????? ??????????? ?????????? ????????? | Set::partialIntersection($minCount, ...$iterables) |
| partialIntersectionCoercive | ????????? ??????????? ?????????? ????????? ? ?????? ?????????? ????? | Set::partialIntersectionCoercive($minCount, ...$iterables) |
| symmetricDifference | ?????????????? ??????? ?????????? ????????? | Set::symmetricDifference(...$iterables) |
| symmetricDifferenceCoercive | ?????????????? ??????? ?????????? ????????? ? ?????? ?????????? ????? | Set::symmetricDifferenceCoercive(...$iterables) |
???????????? ? ???????????
| Iterator | Description | Code Snippet |
|-------------------|------------------------------------------|-------------------------------------|
| asort | ????????? ????????? ? ??????????? ?????? | Sort::asort($data, [$comparator]) |
| sort | ????????? ????????? | Sort::sort($data, [$comparator]) |
???????????? ??????
| Iterator | Description | Code Snippet |
|----------------------------|-----------------------------------------------|--------------------------------|
| readCsv | ????????? ????????? ????? CSV-????? ????????? | File::readCsv($fileHandle) |
| readLines | ????????? ?????????? ????? ????????? | File::readLines($fileHandle) |
?????????????? ??????????? ?????????
| Iterator | Description | Code Snippet |
|-----------------------------------------------|----------------------------------------------------------------|------------------------------------------------------------------|
| tee | ??????? ????????? ?????????? ??????????? ?????????? ?? ??????? | Transform::tee($data, $count) |
| toArray | ??????????? ??????????? ????????? ? ?????? | Transform::toArray($data) |
| toAssociativeArray | ??????????? ??????????? ????????? ? ????????????? ?????? | Transform::toAssociativeArray($data, [$keyFunc], [$valueFunc]) |
| toIterator | ??????????? ??????????? ????????? ? ???????? | Transform::toIterator($data) |
??????? ? ?????????
| ????? | ???????? | ?????? ???? |
|---------------------------------------------------------|------------------------------------------------------------------------------------------------------|---------------------------------------------------|
| allMatch | ???????, ???? ???????? ?????????? ?????? ??? ???? ????????? ????????? | Summary::allMatch($data, $predicate) |
| anyMatch | ???????, ???? ???????? ?????????? ?????? ???? ?? ??? ?????? ???????? ????????? | Summary::anyMatch($data, $predicate) |
| arePermutations | ???????, ???? ????????? ???????? ?????????????? ???? ????? | Summary::arePermutations(...$iterables) |
| arePermutationsCoercive | ???????, ???? ????????? ???????? ?????????????? ???? ????? (? ?????? ?????????? ?????) | Summary::arePermutationsCoercive(...$iterables) |
| exactlyN | ???????, ???? ???????? ?????????? ?????? ? ???????? ??? N ????????? | Summary::exactlyN($data, $n, $predicate) |
| isPartitioned | ???????, ???? ???????? ???????? ????????? ? ????????? ????? ??????? (?????????? ?????????? ????????) | Summary::isPartitioned($data, $predicate) |
| isSorted | ???????, ???? ????????? ????????????? ? ?????? ??????? | Summary::isSorted($data) |
| isReversed | ???????, ???? ????????? ????????????? ? ???????? ??????? | Summary::isReversed($data) |
| noneMatch | ???????, ???? ???????? ?????????? ???? ??? ???? ????????? ????????? | Summary::noneMatch($data, $predicate) |
| same | ???????, ???? ?????? ????????? ????????? | Summary::same(...$iterables) |
| sameCount | ???????, ???? ?????? ????????? ????? ?????????? ????? | Summary::sameCount(...$iterables) |
?????????????
| ????? | ???????? | ?????? ???? |
|----------------------------------------|-------------------------------------------------------------------------------------|---------------------------------------------------------------|
| toAverage | ??????? ?????????????? ????????? ????????? | Reduce::toAverage($numbers) |
| toCount | ????? ????????? | Reduce::toCount($data) |
| toFirst | ?????? ??????? ????????? | Reduce::toFirst() |
| toFirstAndLast | ?????? ? ???????? ???????? ????????? | Reduce::toFirstAndLast() |
| toLast | ????????? ??????? ????????? | Reduce::toLast() |
| toMax | ???????????? ??????? ????????? | Reduce::toMax($numbers, [$compareBy]) |
| toMin | ??????????? ??????? ????????? | Reduce::toMin($numbers, [$compareBy]) |
| toMinMax | ??????????? ? ???????????? ??????? ????????? | Reduce::toMinMax($numbers, [$compareBy]) |
| toProduct | ???????????? ????????? ????????? | Reduce::toProduct($numbers) |
| toRange | ??????? ????? ???????????? ? ??????????? ?????????? ????????? | Reduce::toRange($numbers) |
| toString | ?????????????? ????????? ? ?????? | Reduce::toString($data, [$separator], [$prefix], [$suffix]) |
| toSum | ????? ????????? ????????? | Reduce::toSum($numbers) |
| toValue | ????????????? ????????? ?? ????????, ???????????? ? ?????????????? callback-??????? | Reduce::toValue($data, $reducer, $initialValue) |
?????????? ????? ??????????
????????? ??????
| ???????? | ???????? | ?????? ???? |
|--------------------------------------------------|------------------------------------------------------------------------------------------------------|-----------------------------------------------------|
| of | ??????? ????? ??? ?????????? ??????? ?? ?????? ????????? | Stream::of($iterable) |
| ofCoinFlips | ??????? ????? ??? ?????????? ??????? ?? ??????????? ????????? ??????? ?????? | Stream::ofCoinFlips($repetitions) |
| ofCsvFile | ??????? ????? ??? ?????????? ??????? ?? ????? CSV-????? | Stream::ofCsvFile($fileHandle) |
| ofFileLines | ??????? ????? ??? ?????????? ??????? ?? ????? ????? | Stream::ofFileLines($fileHandle) |
| ofEmpty | ??????? ????? ??? ?????????? ??????? ?? ?????? ????????? | Stream::ofEmpty() |
| ofRandomChoice | ??????? ????? ??? ?????????? ??????? ?? ??????????? ????????? ??????? ???????? ?? ?????? | Stream::ofRandomChoice($items, $repetitions) |
| ofRandomNumbers | ??????? ????? ??? ?????????? ??????? ?? ???????????? ?????? ????????? ????? ????? | Stream::ofRandomNumbers($min, $max, $repetitions) |
| ofRandomPercentage | ??????? ????? ??? ?????????? ??????? ?? ???????????? ?????? ????????? ???????????? ????? ????? 0 ? 1 | Stream::ofRandomPercentage($repetitions) |
| ofRange | ??????? ????? ??? ?????????? ??????? ?? ?????????????? ?????????? | Stream::ofRange($start, $end, $step) |
| ofRockPaperScissors | ??????? ????? ??? ?????????? ??????? ?? ??????????? ????????? ??????? "??????-???????-??????" | Stream::ofRockPaperScissors($repetitions) |
?????????? ????????
| ???????? | ???????? | ?????? ???? |
|---------------------------------------------------------------------------|--------------------------------------------------------------------------------------------------------------|-----------------------------------------------------------------------------------|
| asort | ????????? ????????? ? ??????????? ?????? | $stream->asort([$comparator]) |
| chainWith | ????????? ? ????? ????????? ?????? ????????? ??? ????????????????? ???????????? | $stream->chainWith(...$iterables) |
| compress | ??????????????? ?? ????????? ????????, ??????? ?? ??????? | $stream->compress($selectors) |
| compressAssociative | Compress source by filtering out keys not selected | $stream->compressAssociative($selectorKeys) |
| chunkwise | ????????? ????????? ? ?????????? ?? ?????? | $stream->chunkwise($chunkSize) |
| chunkwiseOverlap | ????????? ????????? ? ?????????? ?? ????????????????????? ?????? | $stream->chunkwiseOverlap($chunkSize, $overlap) |
| distinct | ????????? ?????????, ???????? ?????? ?????????? ???????? | $stream->distinct($strict) |
| dropWhile | ?????????? ???????? ?? ?????????, ???? ???????? ?????????? ???? | $stream->dropWhile($predicate) |
| filter | ?????????? ?? ????????? ?????? ?? ????????, ??? ??????? ???????? ?????????? ?????? | $stream->filterTrue($predicate) |
| filterTrue | ?????????? ?????? ???????? ???????? ?? ????????? | $stream->filterTrue($predicate) |
| filterFalse | ?????????? ?????? ?????? ???????? ?? ????????? | $stream->filterFalse($predicate) |
| filterKeys | ?????????? ?????? ?? ????????, ??? ?????? ??????? ???????? ?????????? ?????? | $stream->filterKeys($predicate) |
| flatMap | ??????????? ????????? ? ?????????? ?????????? ?? 1 ??????? ??????????? | $stream->flatMap($function) |
| flatten | ?????????????? ????????? ????????? | $stream->flatten($dimensions) |
| groupBy | ?????????? ???????? ?? ????????? ?? ????????? ??????? | $stream->groupBy($groupKeyFunction) |
| infiniteCycle | ?????????? ??????????? ??????? ????????? | $stream->infiniteCycle() |
| intersectionWith | ?????????? ??????????? ???????? ????????? ? ??????? ??????????? | $stream->intersectionWith(...$iterables) |
| intersection CoerciveWith | ?????????? ??????????? ???????? ????????? ? ??????? ??????????? (? ?????? ?????????? ?????) | $stream->intersectionCoerciveWith(...$iterables) |
| limit | ???????????? ???????????? ????????? ???????? ???????????? ?????? ???????? | $stream->limit($limit) |
| map | ??????????? ????????? ? ?????????????? callback-??????? | $stream->map($function) |
| pairwise | ????????? ????????? ??????? (? ??????????) | $stream->pairwise() |
| partialIntersectionWith | ?????????? ????????? ??????????? ???????? ????????? ? ??????? ??????????? | $stream->partialIntersectionWith( $minIntersectionCount, ...$iterables) |
| partialIntersection CoerciveWith | ?????????? ????????? ??????????? ???????? ????????? ? ??????? ??????????? (? ?????? ?????????? ?????) | $stream->partialIntersectionCoerciveWith( $minIntersectionCount, ...$iterables) |
| reindex | ??????????????? key-value ????????? | $stream->reindex($reindexer) |
| reverse | ????????? ????????? ? ???????? ??????? | $stream->reverse() |
| runningAverage | ?????????? ???????? ??????????????? ????????? ????????? | $stream->runningAverage($initialValue) |
| runningDifference | ?????????? ???????? ????????? ????????? | $stream->runningDifference($initialValue) |
| runningMax | ????? ????????????? ???????? ?? ????????? | $stream->runningMax($initialValue) |
| runningMin | ????? ???????????? ???????? ?? ????????? | $stream->runningMin($initialValue) |
| runningProduct | ?????????? ???????????? ????????? ????????? | $stream->runningProduct($initialValue) |
| runningTotal | ?????????? ????? ????????? ????????? | $stream->runningTotal($initialValue) |
| slice | ?????????? ?????????? ????????? | $stream->slice([start], [$count], [step]) |
| sort | ????????? ???????? ????????? | $stream->sort([$comparator]) |
| symmetricDifferenceWith | ?????????? ?????????????? ???????? ???????? ????????? ? ??????? ??????????? | $this->symmetricDifferenceWith(...$iterables) |
| symmetricDifference CoerciveWith | ?????????? ?????????????? ???????? ???????? ????????? ? ??????? ??????????? (? ?????? ?????????? ?????) | $this->symmetricDifferenceCoerciveWith( ...$iterables) |
| takeWhile | ?????? ???????? ?? ?????????, ???? ???????? ?????????? ?????? | $stream->takeWhile($predicate) |
| zipWith | ??????????? ????????? ????????? ?????? ? ???????, ???? ?? ?????????? ????? ???????? ???????? | $stream->zipWith(...$iterables) |
| zipLongestWith | ??????????? ????????? ????????? ?????? ? ???????, ???? ?? ?????????? ????? ??????? ???????? | $stream->zipLongestWith(...$iterables) |
| zipEqualWith | ??????????? ????????? ????????? ?????? ? ??????? ?????? ???????, ? ?????? ?????? ???????? ??????? ?????????? | $stream->zipEqualWith(...$iterables) |
??????????? ????????
??????? ? ?????????
| ???????? | ???????? | ?????? ???? |
|------------------------------------------------------------------|------------------------------------------------------------------------------------------------------|-------------------------------------------------------|
| allMatch | ???????, ???? ???????? ?????????? ?????? ??? ???? ????????? ????????? | $stream->allMatch($predicate) |
| anyMatch | ???????, ???? ???????? ?????????? ?????? ???? ?? ??? ?????? ???????? ????????? | $stream->anyMatch($predicate) |
| arePermutationsWith | ???????, ???? ????????? ???????? ?????????????? ???? ????? | $stream->arePermutationsWith(...$iterables) |
| arePermutationsCoerciveWith | ???????, ???? ????????? ???????? ?????????????? ???? ????? (? ?????? ?????????? ?????) | $stream->arePermutationsCoerciveWith(...$iterables) |
| exactlyN | ???????, ???? ???????? ?????????? ?????? ? ???????? ??? N ????????? | $stream->exactlyN($n, $predicate) |
| isPartitioned | ???????, ???? ???????? ???????? ????????? ? ????????? ????? ??????? (?????????? ?????????? ????????) | $stream::isPartitioned($predicate) |
| isSorted | ???????, ???? ????????? ????????????? ? ?????? ??????? | $stream->isSorted() |
| isReversed | ???????, ???? ????????? ????????????? ? ???????? ??????? | $stream->isReversed() |
| noneMatch | ???????, ???? ???????? ?????????? ???? ??? ???? ????????? ????????? | $stream->noneMatch($predicate) |
| sameWith | ???????, ???? ?????? ????????? ????????? | $stream->sameWith(...$iterables) |
| sameCountWith | ???????, ???? ?????? ????????? ????? ?????????? ????? | $stream->sameCountWith(...$iterables) |
?????????????
| Terminal Operation | Description | Code Snippet |
|------------------------------------------|---------------------------------------------------------------------|---------------------------------------------------------|
| toAverage | ??????? ?????????????? ????????? ????????? | $stream->toAverage() |
| toCount | ????? ????????? | $stream->toCount() |
| toFirst | ?????? ??????? ????????? | $stream->toFirst() |
| toFirstAndLast | ?????? ? ???????? ???????? ????????? | $stream->toFirstAndLast() |
| toLast | ????????? ??????? ????????? | $stream->toLast() |
| toMax | ???????????? ???????? ?? ????????? ????????? | $stream->toMax([$compareBy]) |
| toMin | ??????????? ???????? ?? ????????? ????????? | $stream->toMin([$compareBy]) |
| toMinMax | ??????????? ? ???????????? ???????? ?? ????????? ????????? | $stream->toMinMax([$compareBy]) |
| toProduct | ???????????? ????????? ????????? | $stream->toProduct() |
| toString | ?????????????? ????????? ? ?????? | $stream->toString([$separator], [$prefix], [$suffix]) |
| toSum | ????? ????????? ????????? | $stream->toSum() |
| toRange | ??????? ????? ???????????? ? ??????????? ?????????? ????????? | $stream->toRange() |
| toValue | ????????????? ????????? ?? ????????, ???????????? callback-???????? | $stream->toValue($reducer, $initialValue) |
???????? ?????????????
| Terminal Operation | Description | Code Snippet |
|-------------------------------------------------|-------------------------------------------------------------|---------------------------------------------------------|
| toArray | ?????????? ?????? ?? ????????? ?????? | $stream->toArray() |
| toAssociativeArray | ?????????? ????????????? ?????? ?? ????????? ?????? | $stream->toAssociativeArray($keyFunc, $valueFunc) |
| tee | ??????? ????????? ?????????? ??????????? ??????? ?? ??????? | $stream->tee($count) |
???????? ? ????????? ?????????
| Terminal Operation | Description | Code Snippet |
|---------------------------------|-------------------------------------------------------|-------------------------------------------------------|
| callForEach | ???????? callback-??????? ??? ??????? ???????? ?????? | $stream->callForEach($function) |
| print | print ??????? ???????? ?????? | $stream->print([$separator], [$prefix], [$suffix]) |
| printLn | print ??????? ???????? ?????? ? ????? ?????? | $stream->printLn() |
| printR | print_r ??????? ???????? ?????? | $stream->printR() |
| toCsvFile | ?????????? ?????????? ?????? ? CSV ???? | $stream->toCsvFile($fileHandle, [$headers]) |
| toFile | ?????????? ?????????? ?????? ? ???? | $stream->toFile($fileHandle) |
| var_dump | var_dump ??????? ???????? ?????? | $stream->varDump() |
?????????
???????? ?????????? ? ??????????? ? ????? composer.json ?????? ???????:
{
"require": {
"markrogoyski/itertools-php": "1.*"
}
}
??????????? composer, ????? ?????????? ??????????:
$ php composer.phar install
Composer ????????? IterTools ? ????? vendor ?????? ???????. ????? ????? ?? ?????? ???????????? ?????????? ??????????
? ?????? ?????? ???????, ????????? Composer Autoloader.
require_once __DIR__ . '/vendor/autoload.php';
?????????????? ??????? ?????????. ????????? ?????? ??????? ? ?????????, ???????? ? ???????? ?????????? ?????? ???????:
$ php composer.phar require markrogoyski/itertools-php:1.*
??????????? ??????????
?????????????
??? ??????? ???????? ? iterable ??????????:
* array (???)
* Generator (???)
* Iterator (?????????)
* Traversable (?????????)
???????????? ?????????? ?????????
Chain
??????????????? ????????? ????????? ???????????.
use IterTools\Multi;
$prequels = ['Phantom Menace', 'Attack of the Clones', 'Revenge of the Sith'];
$originals = ['A New Hope', 'Empire Strikes Back', 'Return of the Jedi'];
foreach (Multi::chain($prequels, $originals) as $movie) { print($movie);
}
// 'Phantom Menace', 'Attack of the Clones', 'Revenge of the Sith', 'A New Hope', 'Empire Strikes Back', 'Return of the Jedi'
### Zip
??????????? ????????? ?????????, ???? ?? ?????????? ????? ???????? ????????.
use IterTools\Multi;
$languages = ['PHP', 'Python', 'Java', 'Go'];
$mascots = ['elephant', 'snake', 'bean', 'gopher'];
foreach (Multi::zip($languages, $mascots) as [$language, $mascot]) {
print("The {$language} language mascot is an {$mascot}.");
}
// The PHP language mascot is an elephant.
// ...
????? ????????? ?????? ???? ????????? ?? ????. $names = ['Ryu', 'Ken', 'Chun Li', 'Guile'];
$countries = ['Japan', 'USA', 'China', 'USA'];
$signatureMoves = ['hadouken', 'shoryuken', 'spinning bird kick', 'sonic boom'];
foreach (Multi::zip($names, $countries, $signatureMoves) as [$name, $country, $signatureMove]) {
$streetFighter = new StreetFighter($name, $country, $signatureMove);
}
??????????: ??? ????????? ?????? ???? ???????????? ????????????, ????? ?????????? ????? ???????? ?????????.
ZipLongest
??????????? ????????? ?????????, ???? ?? ?????????? ????? ??????? ????????.
??????????: ? ?????? ???????????? ????????? ?????? ???? ??? ????????????? ????????? ? ?????? ????????? ???????? ????? ????????????? ???????? `null`.
use IterTools\Multi;
$letters = ['A', 'B', 'C'];
$numbers = [1, 2];
foreach (Multi::zipLongest($letters, $numbers) as [$letter, $number]) { // ['A', 1], ['B', 2], ['C', null]
}
### ZipEqual
??????????? ????????? ????????? ?????? ???????, ? ?????? ?????? ???????? ??????? ??????????.
??????? `\LengthException`, ???? ????? ????????? ???????? ???????, ??? ?????? ?????????? ????? ????????.
use IterTools\Multi;
$letters = ['A', 'B', 'C'];
$numbers = [1, 2, 3];
foreach (Multi::zipEqual($letters, $numbers) as [$letter, $number]) {
// ['A', 1], ['B', 2], ['C', 3]
}
???????????? ????? ?????????
Chunkwise
????????? ?????????, ???????? ?? ????? ?????????? ?????.
??????????? ?????? ????? ? 1.
use IterTools\Single;
$movies = [ 'Phantom Menace', 'Attack of the Clones', 'Revenge of the Sith',
'A New Hope', 'Empire Strikes Back', 'Return of the Jedi',
'The Force Awakens', 'The Last Jedi', 'The Rise of Skywalker'
];
foreach (Single::chunkwise($movies, 3) as $trilogy) { $trilogies[] = $trilogy;
}
// [
// ['Phantom Menace', 'Attack of the Clones', 'Revenge of the Sith'],
// ['A New Hope', 'Empire Strikes Back', 'Return of the Jedi'],
// ['The Force Awakens', 'The Last Jedi', 'The Rise of Skywalker]'
// ]
### Chunkwise Overlap
????????? ?????????, ???????? ?? ????????????????????? ?????.
-
??????????? ?????? ????? ? 1.
-
?????? ????????? ?????? ???? ?????? ????? ?????.
use IterTools\Single;
$numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
foreach (Single::chunkwiseOverlap($numbers, 3, 1) as $chunk) {
// [1, 2, 3], [3, 4, 5], [5, 6, 7], [7, 8, 9], [9, 10]
}
Compress
??????????????? ??????????? ???????? ?? ?????????.
use IterTools\Single;
$movies = [ 'Phantom Menace', 'Attack of the Clones', 'Revenge of the Sith',
'A New Hope', 'Empire Strikes Back', 'Return of the Jedi',
'The Force Awakens', 'The Last Jedi', 'The Rise of Skywalker'
];
$goodMovies = [0, 0, 0, 1, 1, 1, 1, 0, 0];
foreach (Single::compress($movies, $goodMovies) as $goodMovie) { print($goodMovie);
}
// 'A New Hope', 'Empire Strikes Back', 'Return of the Jedi', 'The Force Awakens'
### Compress Associative
?????????? ???????? ?? ????????? ?? ???????? ??????.
-
??????? ????? ???? ?????? ?????? ??? ????? ????? (?? ???????? ? ??????? PHP-????????).
use IterTools\Single;
$starWarsEpisodes = [
'I' => 'The Phantom Menace',
'II' => 'Attack of the Clones',
'III' => 'Revenge of the Sith',
'IV' => 'A New Hope',
'V' => 'The Empire Strikes Back',
'VI' => 'Return of the Jedi',
'VII' => 'The Force Awakens',
'VIII' => 'The Last Jedi',
'IX' => 'The Rise of Skywalker',
];
$originalTrilogyNumbers = ['IV', 'V', 'VI'];
foreach (Single::compressAssociative($starWarsEpisodes, $originalTrilogyNumbers) as $episode => $title) {
print("$episode: $title" . \PHP_EOL);
}
// IV: A New Hope
// V: The Empire Strikes Back
// VI: Return of the Jedi
Drop While
?????????? ????????, ???? ???????? ?????????? ??????.
????? ???? ??? ???????? ??????? ?????? true , ??? ??????????? ???????? ???????? ? ???????.
use IterTools\Single;
$scores = [50, 60, 70, 85, 65, 90];
$predicate = fn ($x) => $x < 70;
foreach (Single::dropWhile($scores, $predicate) as $score) { print($score);
}
// 70, 85, 65, 90
### Filter
?????????? ?????? ?? ????????, ??? ??????? ???????? ?????????? ??????.
use IterTools\Single;
$starWarsEpisodes = [1, 2, 3, 4, 5, 6, 7, 8, 9];
$goodMoviePredicate = fn ($episode) => $episode > 3 && $episode < 8;
foreach (Single::filter($starWarsEpisodes, $goodMoviePredicate) as $goodMovie) {
print($goodMovie);
}
// 4, 5, 6, 7
Filter True
?????????? ?????? ???????? ???????? ?? ?????????. ?????????? ???????????? ??????????.
???? ???????? ?? ???????, ???????? ????????? ????????? ?????????? ? bool ??? ??????.
use IterTools\Single;
$reportCardGrades = [100, 0, 95, 85, 0, 94, 0];
foreach (Single::filterTrue($reportCardGrades) as $goodGrade) { print($goodGrade);
}
// 100, 95, 85, 94
### Filter False
?????????? ?????? ?????? ???????? ?? ?????????. ?????????? ???????????? ??????????.
???? ???????? ?? ???????, ???????? ????????? ????????? ?????????? ? `bool` ??? ??????.
use IterTools\Single;
$alerts = [0, 1, 1, 0, 1, 0, 0, 1, 1];
foreach (Single::filterFalse($alerts) as $noAlert) {
print($noAlert);
}
// 0, 0, 0, 0
Filter Keys
?????????? ?????? ?? ????????, ??? ?????? ??????? ???????? ?????????? ??????.
use IterTools\Single;
$olympics = [ 2000 => 'Sydney',
2002 => 'Salt Lake City',
2004 => 'Athens',
2006 => 'Turin',
2008 => 'Beijing',
2010 => 'Vancouver',
2012 => 'London',
2014 => 'Sochi',
2016 => 'Rio de Janeiro',
2018 => 'Pyeongchang',
2020 => 'Tokyo',
2022 => 'Beijing',
];
$summerFilter = fn ($year) => $year % 4 === 0;
foreach (Single::filterKeys($olympics, $summerFilter) as $year => $hostCity) { print("$year: $hostCity" . \PHP_EOL);
}
// 2000: Sydney
// 2004: Athens
// 2008: Beijing
// 2012: London
// 2016: Rio de Janeiro
// 2020: Tokyo
### Flat Map
??????????? ????????? ? ?????????? ?????????? ?? 1 ??????? ???????????.
use IterTools\Single;
$data = [1, 2, 3, 4, 5];
$mapper = fn ($item) => [$item, -$item];
foreach (Single::flatMap($data, $mapper) as $number) {
print($number . ' ');
}
// 1 -1 2 -2 3 -3 4 -4 5 -5
Flatten
?????????????? ????????? ?????????.
use IterTools\Single;
$multidimensional = [1, [2, 3], [4, 5]];
$flattened = [];
foreach (Single::flatten($multidimensional) as $number) { $flattened[] = $number;
}
// [1, 2, 3, 4, 5]
### Group By
?????????? ???????? ????????? ?? ????????? ???????.
-
??????? `$groupKeyFunction` ?????? ?????????? ????? ???? (??? ????????? ??????) ??? ????????? ??????.
-
??????? `$itemKeyFunction` (???????????? ????????) ????????? ????????? ????????? ??????? ?????????? ? ??????.
use IterTools\Single;
$cartoonCharacters = [
['Garfield', 'cat'],
['Tom', 'cat'],
['Felix', 'cat'],
['Heathcliff', 'cat'],
['Snoopy', 'dog'],
['Scooby-Doo', 'dog'],
['Odie', 'dog'],
['Donald', 'duck'],
['Daffy', 'duck'],
];
$charactersGroupedByAnimal = [];
foreach (Single::groupBy($cartoonCharacters, fn ($x) => $x[1]) as $animal => $characters) {
$charactersGroupedByAnimal[$animal] = $characters;
}
/*
'cat' => [
['Garfield', 'cat'],
['Tom', 'cat'],
['Felix', 'cat'],
['Heathcliff', 'cat'],
],
'dog' => [
['Snoopy', 'dog'],
['Scooby-Doo', 'dog'],
['Odie', 'dog'],
],
'duck' => [
['Donald', 'duck'],
['Daffy', 'duck'],
*/
Limit
???????????? ???????????? ????????? ???????? ???????????? ?????? ????????.
????????????? ??????? ????????????, ????? ????? ???????? ????????? $limit .
use IterTools\Single;
$matrixMovies = ['The Matrix', 'The Matrix Reloaded', 'The Matrix Revolutions', 'The Matrix Resurrections'];
$limit = 1;
foreach (Single::limit($matrixMovies, $limit) as $goodMovie) { print($goodMovie);
}
// 'The Matrix' (and nothing else)
### Map
??????????? ????????? ? ?????????????? callback-???????.
????????? ?????????? ???????????? ????? ????????? ??????????? ?????? callback-??????? ??? ??????? ????????.
use IterTools\Single;
$grades = [100, 99, 95, 98, 100];
$strictParentsOpinion = fn ($g) => $g === 100 ? 'A' : 'F';
foreach (Single::map($grades, $strictParentsOpinion) as $actualGrade) {
print($actualGrade);
}
// A, F, F, F, A
Pairwise
????????? ????????? ??????? (? ??????????).
?????????? ?????? ?????????, ???? ????????? ???????? ?????? 2-? ?????????.
use IterTools\Single;
$friends = ['Ross', 'Rachel', 'Chandler', 'Monica', 'Joey', 'Phoebe'];
foreach (Single::pairwise($friends) as [$leftFriend, $rightFriend]) { print("{$leftFriend} and {$rightFriend}");
}
// Ross and Rachel, Rachel and Chandler, Chandler and Monica, ...
### Repeat
????????? ?????? ???????? ???????? ????? ???.
use IterTools\Single;
$data = 'Beetlejuice';
$repetitions = 3;
foreach (Single::repeat($data, $repetitions) as $repeated) {
print($repeated);
}
// 'Beetlejuice', 'Beetlejuice', 'Beetlejuice'
Reindex
??????????????? key-value ?????????, ????????? ???????-??????????.
use IterTools\Single;
$data = [ [
'title' => 'Star Wars: Episode IV ? A New Hope',
'episode' => 'IV',
'year' => 1977,
],
[
'title' => 'Star Wars: Episode V ? The Empire Strikes Back',
'episode' => 'V',
'year' => 1980,
],
[
'title' => 'Star Wars: Episode VI ? Return of the Jedi',
'episode' => 'VI',
'year' => 1983,
],
];
$reindexFunc = fn (array $swFilm) => $swFilm['episode'];
$reindexedData = [];
foreach (Single::reindex($data, $reindexFunc) as $key => $filmData) { $reindexedData[$key] = $filmData;
}
// [
// 'IV' => [
// 'title' => 'Star Wars: Episode IV ? A New Hope',
// 'episode' => 'IV',
// 'year' => 1977,
// ],
// 'V' => [
// 'title' => 'Star Wars: Episode V ? The Empire Strikes Back',
// 'episode' => 'V',
// 'year' => 1980,
// ],
// 'VI' => [
// 'title' => 'Star Wars: Episode VI ? Return of the Jedi',
// 'episode' => 'VI',
// 'year' => 1983,
// ],
// ]
### Reverse
????????? ????????? ? ???????? ???????.
use IterTools\Single;
$words = ['Alice', 'answers', 'your', 'questions', 'Bob'];
foreach (Single::reverse($words) as $word) {
print($word . ' ');
}
// Bob questions your answers Alice
Slice
?????????? ?????????? ?????????.
use IterTools\Single;
$olympics = [1992, 1994, 1996, 1998, 2000, 2002, 2004, 2006, 2008, 2010, 2012, 2014, 2016, 2018, 2020, 2022];
$winterOlympics = [];
foreach (Single::slice($olympics, 1, 8, 2) as $winterYear) { $winterOlympics[] = $winterYear;
}
// [1994, 1998, 2002, 2006, 2010, 2014, 2018, 2022]
### String
????????? ?????? ???????????.
use IterTools\Single;
$string = 'MickeyMouse';
$listOfCharacters = [];
foreach (Single::string($string) as $character) {
$listOfCharacters[] = $character;
}
// ['M', 'i', 'c', 'k', 'e', 'y', 'M', 'o', 'u', 's', 'e']
Take While
?????? ????????, ???? ???????? ?????????? ??????.
????????????? ??????? ????????????, ??? ?????? ???????? ??????? ?????? ????.
use IterTools\Single;
$prices = [0, 0, 5, 10, 0, 0, 9];
$isFree = fn ($price) => $price == 0;
foreach (Single::takeWhile($prices, $isFree) as $freePrice) { print($freePrice);
}
// 0, 0
## ??????????? ????????????
### Count
?????????? ?????????? ?????????????????? ????? ?????.
use IterTools\Infinite;
$start = 1;
$step = 1;
foreach (Infinite::count($start, $step) as $i) {
print($i);
}
// 1, 2, 3, 4, 5 ...
Cycle
?????????? ??????????? ??????? ?????????.
use IterTools\Infinite;
$hands = ['rock', 'paper', 'scissors'];
foreach (Infinite::cycle($hands) as $hand) { RockPaperScissors::playHand($hand);
}
// rock, paper, scissors, rock, paper, scissors, ...
### Repeat (Infinite)
?????????? ????????? ?????? ????????.
use IterTools\Infinite;
$dialogue = 'Are we there yet?';
foreach (Infinite::repeat($dialogue) as $repeated) {
print($repeated);
}
// 'Are we there yet?', 'Are we there yet?', 'Are we there yet?', ...
???????????? ????????? ????????
Choice
?????????? ????????? ?????? ????????? ?? ??????.
use IterTools\Random;
$cards = ['Ace', 'King', 'Queen', 'Jack', 'Joker'];
$repetitions = 10;
foreach (Random::choice($cards, $repetitions) as $card) { print($card);
}
// 'King', 'Jack', 'King', 'Ace', ... [random]
### CoinFlip
?????????? ????????? ?????? ?????? (0 ??? 1).
use IterTools\Random;
$repetitions = 10;
foreach (Random::coinFlip($repetitions) as $coinFlip) {
print($coinFlip);
}
// 1, 0, 1, 1, 0, ... [random]
Number
?????????? ????????? ????? ?????.
use IterTools\Random;
$min = 1;
$max = 4;
$repetitions = 10;
foreach (Random::number($min, $max, $repetitions) as $number) { print($number);
}
// 3, 2, 5, 5, 1, 2, ... [random]
### Percentage
?????????? ????????? ???????????? ????? ????? 0 ? 1.
use IterTools\Random;
$repetitions = 10;
foreach (Random::percentage($repetitions) as $percentage) {
print($percentage);
}
// 0.30205562629132, 0.59648594775233, ... [random]
RockPaperScissors
????????? ????? "??????-???????-??????".
use IterTools\Random;
$repetitions = 10;
foreach (Random::rockPaperScissors($repetitions) as $rpsHand) { print($rpsHand);
}
// 'paper', 'rock', 'rock', 'scissors', ... [random]
## ?????????????? ????????????
### Running Average
?????????? ???????? ??????????????? ????????? ????????? ? ???????? ????????????.
use IterTools\Math;
$grades = [100, 80, 80, 90, 85];
foreach (Math::runningAverage($grades) as $runningAverage) {
print($runningAverage);
}
// 100, 90, 86.667, 87.5, 87
Running Difference
?????????? ???????? ????????? ????????? ? ???????? ????????????.
use IterTools\Math;
$credits = [1, 2, 3, 4, 5];
foreach (Math::runningDifference($credits) as $runningDifference) { print($runningDifference);
}
// -1, -3, -6, -10, -15 ??????????? ????????? ?????? ?????????? ? ????????? ????????.
use IterTools\Math;
$dartsScores = [50, 50, 25, 50];
$startingScore = 501;
foreach (Math::runningDifference($dartsScores, $startingScore) as $runningScore) { print($runningScore);
}
// 501, 451, 401, 376, 326
### Running Max
????? ????????????? ???????? ? ???????? ????????????.
use IterTools\Math;
$numbers = [1, 2, 1, 3, 5];
foreach (Math::runningMax($numbers) as $runningMax) {
print($runningMax);
}
// 1, 2, 2, 3, 5
Running Min
????? ???????????? ???????? ? ???????? ????????????.
use IterTools\Math;
$numbers = [3, 4, 2, 5, 1];
foreach (Math::runningMin($numbers) as $runningMin) { print($runningMin);
}
// 3, 3, 2, 2, 1
### Running Product
?????????? ???????????? ????????? ????????? ? ???????? ????????????.
use IterTools\Math;
$numbers = [1, 2, 3, 4, 5];
foreach (Math::runningProduct($numbers) as $runningProduct) {
print($runningProduct);
}
// 1, 2, 6, 24, 120
??????????? ????????? ?????? ?????????? ? ????????? ????????. use IterTools\Math;
$numbers = [1, 2, 3, 4, 5];
$initialValue = 5;
foreach (Math::runningProduct($numbers, $initialValue) as $runningProduct) {
print($runningProduct);
}
// 5, 5, 10, 30, 120, 600
Running Total
?????????? ????? ????????? ????????? ? ???????? ????????????.
use IterTools\Math;
$prices = [1, 2, 3, 4, 5];
foreach (Math::runningTotal($prices) as $runningTotal) { print($runningTotal);
}
// 1, 3, 6, 10, 15
??????????? ????????? ?????? ?????????? ? ????????? ????????.
use IterTools\Math;
$prices = [1, 2, 3, 4, 5];
$initialValue = 5;
foreach (Math::runningTotal($prices, $initialValue) as $runningTotal) { print($runningTotal);
}
// 5, 6, 8, 11, 15, 20
## ???????????? ???????? ? ??????????????
### Distinct
????????? ?????????, ??????? ?????? ?????????? ????????.
?? ????????? ????????? ????????? ? ?????? ??????? ?????????. ????????? ???????? false ????????? $strict , ????? ???????? ? ?????? ?????????? ?????.
use IterTools\Set;
$chessSet = ['rook', 'rook', 'knight', 'knight', 'bishop', 'bishop', 'king', 'queen', 'pawn', 'pawn', ... ];
foreach (Set::distinct($chessSet) as $chessPiece) {
print($chessPiece);
}
// rook, knight, bishop, king, queen, pawn
$mixedTypes = [1, '1', 2, '2', 3];
foreach (Set::distinct($mixedTypes, false) as $datum) {
print($datum);
}
// 1, 2, 3
Intersection
????????? ??????????? ?????????.
???? ???? ?? ? ????? ????????? ??????????? ????????????? ????????, ???????? ??????? ??????????? ??????????????.
use IterTools\Set;
$chessPieces = ['rook', 'knight', 'bishop', 'queen', 'king', 'pawn'];
$shogiPieces = ['rook', 'knight', 'bishop' 'king', 'pawn', 'lance', 'gold general', 'silver general'];
foreach (Set::intersection($chessPieces, $shogiPieces) as $commonPiece) { print($commonPiece);
}
// rook, knight, bishop, king, pawn
### Intersection Coercive
????????? ??????????? ????????? ? ?????? ?????????? ?????.
???? ???? ?? ? ????? ????????? ??????????? ????????????? ????????, ???????? ??????? ??????????? ??????????????.
use IterTools\Set;
$numbers = [1, 2, 3, 4, 5];
$numerics = ['1', '2', 3];
foreach (Set::intersectionCoercive($numbers, $numerics) as $commonNumber) {
print($commonNumber);
}
// 1, 2, 3
Partial Intersection
????????? M-????????? ??????????? ?????????.
* ???? ???? ?? ? ????? ????????? ??????????? ????????????? ????????, ???????? ??????? ??????????? ??????????????.
* ???? `$minIntersectionCount = 1`, ???????? ??????? ??????????? ??????????????.
use IterTools\Set;
$staticallyTyped = ['c++', 'java', 'c#', 'go', 'haskell'];
$dynamicallyTyped = ['php', 'python', 'javascript', 'typescript'];
$supportsInterfaces = ['php', 'java', 'c#', 'typescript'];
foreach (Set::partialIntersection(2, $staticallyTyped, $dynamicallyTyped, $supportsInterfaces) as $language) { print($language);
}
// c++, java, c#, go, php
### Partial Intersection Coercive
????????? M-????????? ??????????? ????????? ? ?????? ?????????? ?????.
-
???? ???? ?? ? ????? ????????? ??????????? ????????????? ????????, ???????? ??????? ??????????? ??????????????.
-
???? `$minIntersectionCount = 1`, ???????? ??????? ??????????? ??????????????.
use IterTools\Set;
$set1 = [1, 2, 3],
$set2 = ['2', '3', 4, 5],
$set3 = [1, '2'],
foreach (Set::partialIntersectionCoercive(2, $set1, $set2, $set3) as $partiallyCommonNumber) {
print($partiallyCommonNumber);
}
// 1, 2, 3
Symmetric difference
????????? ?????????????? ???????? ?????????.
???? ???? ?? ? ????? ????????? ??????????? ????????????? ????????, ???????? ??????? ????????? ???????? ??????????????.
use IterTools\Set;
$a = [1, 2, 3, 4, 7];
$b = ['1', 2, 3, 5, 8];
$c = [1, 2, 3, 6, 9];
foreach (Set::symmetricDifference($a, $b, $c) as $item) { print($item);
}
// 1, 4, 5, 6, 7, 8, 9
### Symmetric difference Coercive
????????? ?????????????? ???????? ????????? ? ?????? ?????????? ?????.
???? ???? ?? ? ????? ????????? ??????????? ????????????? ????????, ???????? ??????? ????????? ???????? ??????????????.
use IterTools\Set;
$a = [1, 2, 3, 4, 7];
$b = ['1', 2, 3, 5, 8];
$c = [1, 2, 3, 6, 9];
foreach (Set::symmetricDifferenceCoercive($a, $b, $c) as $item) {
print($item);
}
// 4, 5, 6, 7, 8, 9
???????????? ? ???????????
ASort
????????? ????????? ? ??????????? ??????.
???? `$comparator` ?? ???????, ???????? ????????? ?????? ???? ????????.
use IterTools\Single;
$worldPopulations = [ 'China' => 1_439_323_776,
'India' => 1_380_004_385,
'Indonesia' => 273_523_615,
'Pakistan' => 220_892_340,
'USA' => 331_002_651,
];
foreach (Sort::sort($worldPopulations) as $country => $population) { print("$country: $population" . \PHP_EOL);
}
// Pakistan: 220,892,340
// Indonesia: 273,523,615
// USA: 331,002,651
// India: 1,380,004,385
// China: 1,439,323,776
### Sort
????????? ?????????.
???? $comparator ?? ???????, ???????? ????????? ?????? ???? ????????.
use IterTools\Single;
$data = [3, 4, 5, 9, 8, 7, 1, 6, 2];
foreach (Single::sort($data) as $datum) {
print($datum);
}
// 1, 2, 3, 4, 5, 6, 7, 8, 9
???????????? ??????
Read CSV
????????? ????????? ????? CSV-????? ?????????.
use IterTools\File;
$fileHandle = \fopen('path/to/file.csv', 'r');
foreach (File::readCsv($fileHandle) as $row) { print_r($row);
}
// Each column field is an element of the array
### Read Lines
????????? ?????????? ????? ?????????.
use IterTools\File;
$fileHandle = \fopen('path/to/file.txt', 'r');
foreach (File::readLines($fileHandle) as $line) {
print($line);
}
Transform
Tee
??????? ????????? ?????????? ??????????? ?????????? ?? ???????.
use IterTools\Transform;
$daysOfWeek = ['Mon', 'Tues', 'Wed', 'Thurs', 'Fri', 'Sat', 'Sun'];
$count = 3;
[$week1, $week2, $week3] = Transform::tee($data, $count);
// Each $week contains iterator containing ['Mon', 'Tues', 'Wed', 'Thurs', 'Fri', 'Sat', 'Sun']
### To Array
??????????? ??????????? ????????? ? ??????.
use IterTools\Transform;
$iterator = new \ArrayIterator([1, 2, 3, 4, 5]);
$array = Transform::toArray($iterator);
To Associative Array
??????????? ??????????? ????????? ? ????????????? ??????.
use IterTools\Transform;
$messages = ['message 1', 'message 2', 'message 3'];
$keyFunc = fn ($msg) => \md5($msg);
$valueFunc = fn ($msg) => strtoupper($msg);
$associativeArray = Transform::toAssociativeArray($messages, $keyFunc, $valueFunc);
// [
// '1db65a6a0a818fd39655b95e33ada11d' => 'MESSAGE 1',
// '83b2330607fe8f817ce6d24249dea373' => 'MESSAGE 2',
// '037805d3ad7b10c5b8425427b516b5ce' => 'MESSAGE 3',
// ]
### To Iterator
??????????? ??????????? ????????? ? ????????.
use IterTools\Transform;
$array = [1, 2, 3, 4, 5];
$iterator = Transform::toIterator($array);
??????? ? ?????????
All Match
?????????? ??????, ???? ??? ???? ????????? ????????? ???????? ?????? ??????.
use IterTools\Summary;
$finalFantasyNumbers = [4, 5, 6];
$isOnSuperNintendo = fn ($ff) => $ff >= 4 && $ff <= 6;
$boolean = Summary::allMatch($finalFantasyNumbers, $isOnSuperNintendo);
// true
$isOnPlaystation = fn ($ff) => $ff >= 7 && $ff <= 9;
$boolean = Summary::allMatch($finalFantasyNumbers, $isOnPlaystation);
// false
### Any Match
?????????? ??????, ???? ???? ?? ??? ?????? ???????? ????????? ???????? ?????? ??????.
use IterTools\Summary;
$answers = ['fish', 'towel', 42, "don't panic"];
$isUltimateAnswer = fn ($a) => a == 42;
$boolean = Summary::anyMatch($answers, $isUltimateAnswer);
// true
Are Permutations
????????? ??????, ???? ????????? ???????? ?????????????? ???? ?????.
use IterTools\Summary;
$iter = ['i', 't', 'e', 'r'];
$rite = ['r', 'i', 't', 'e'];
$reit = ['r', 'e', 'i', 't'];
$tier = ['t', 'i', 'e', 'r'];
$tire = ['t', 'i', 'r', 'e'];
$trie = ['t', 'r', 'i', 'e'];
$boolean = Summary::arePermutations($iter, $rite, $reit, $tier, $tire, $trie);
// true
### Are Permutations Coercive
?????????? ??????, ???? ????????? ???????? ?????????????? ???? ?????
(? ?????? ?????????? ?????).
use IterTools\Summary;
$set1 = [1, 2.0, '3'];
$set2 = [2.0, '1', 3];
$set3 = [3, 2, 1];
$boolean = Summary::arePermutationsCoercive($set1, $set2, $set3);
// true
Exactly N
???????, ???? ???????? ?????????? ?????? ? ???????? ??? N ?????????.
-
???????? ???????? ?????????????? ??????????.
-
?? ????????? ???????? ????????? ?????????? ???????? ???????? ????????? ? ???? `bool`.
use IterTools\Summary;
$twoTruthsAndALie = [true, true, false];
$n = 2;
$boolean = Summary::exactlyN($twoTruthsAndALie, $n);
// true
$ages = [18, 21, 24, 54];
$n = 4;
$predicate = fn ($age) => $age >= 21;
$boolean = Summary::isSorted($ages, $n, $predicate);
// false
### Is Partitioned
?????????? ??????, ???? ??? ???????? ???????? ????????? ? ????????? ????? ??????? (?????????? ?????????? ????????).
- ?????????? ?????? ??? ?????? ????????? ? ??? ????????? ? ????? ?????????.
- ???? ???????? ?? ??? ???????, ?????????? ???????? ?????????? ????? ?????????? ??? ???????? ? ???????? ????.
use IterTools\Summary;
$numbers = [0, 2, 4, 1, 3, 5];
$evensBeforeOdds = fn ($item) => $item % 2 === 0;
$boolean = Summary::isPartitioned($numbers, $evensBeforeOdds);
Is Sorted
?????????? ??????, ???? ????????? ????????????? ? ?????? ???????.
-
???????? ?????? ???? ????????.
-
??? ?????? ????????? ??? ????????? ?? ?????? ???????? ?????? ?????????? ??????.
use IterTools\Summary;
$numbers = [1, 2, 3, 4, 5];
$boolean = Summary::isSorted($numbers);
// true
$numbers = [3, 2, 3, 4, 5];
$boolean = Summary::isSorted($numbers);
// false
### Is Reversed
?????????? ??????, ???? ????????? ????????????? ? ???????? ???????.
- ???????? ????????? ?????? ???? ????????.
- ??? ?????? ????????? ??? ????????? ?? ?????? ???????? ?????? ?????????? ??????.
use IterTools\Summary;
$numbers = [5, 4, 3, 2, 1];
$boolean = Summary::isReversed($numbers);
// true
$numbers = [1, 4, 3, 2, 1];
$boolean = Summary::isReversed($numbers);
// false
None Match
?????????? ??????, ???? ??? ???? ????????? ????????? ???????? ?????? ????.
use IterTools\Summary;
$grades = [45, 50, 61, 0];
$isPassingGrade = fn ($grade) => $grade >= 70;
$boolean = Summary::noneMatch($grades, $isPassingGrade);
// true
### Same
???????, ???? ?????? ????????? ?????????.
???? ? ????? ???????? ???? ????????? ??? ?? ?????, ?? ?????? ??????.
use IterTools\Summary;
$cocaColaIngredients = ['carbonated water', 'sugar', 'caramel color', 'phosphoric acid'];
$pepsiIngredients = ['carbonated water', 'sugar', 'caramel color', 'phosphoric acid'];
$boolean = Summary::same($cocaColaIngredients, $pepsiIngredients);
// true
$cocaColaIngredients = ['carbonated water', 'sugar', 'caramel color', 'phosphoric acid'];
$spriteIngredients = ['carbonated water', 'sugar', 'citric acid', 'lemon lime flavorings'];
$boolean = Summary::same($cocaColaIngredients, $spriteIngredients);
// false
Same Count
???????, ???? ?????? ????????? ????? ?????????? ?????.
???? ? ????? ???????? ???? ????????? ??? ?? ?????, ?? ?????? ??????.
use IterTools\Summary;
$prequels = ['Phantom Menace', 'Attack of the Clones', 'Revenge of the Sith'];
$originals = ['A New Hope', 'Empire Strikes Back', 'Return of the Jedi'];
$sequels = ['The Force Awakens', 'The Last Jedi', 'The Rise of Skywalker'];
$boolean = Summary::sameCount($prequels, $originals, $sequels);
// true
$batmanMovies = ['Batman Begins', 'The Dark Knight', 'The Dark Knight Rises'];
$matrixMovies = ['The Matrix', 'The Matrix Reloaded', 'The Matrix Revolutions', 'The Matrix Resurrections'];
$result = Summary::sameCount($batmanMovies, $matrixMovies);
// false
## Reduce
### To Average
?????????? ??????? ?????????????? ????????? ?????????.
??? ?????? ????????? ?????????? `null`.
use IterTools\Reduce;
$grades = [100, 90, 95, 85, 94];
$finalGrade = Reduce::toAverage($numbers);
// 92.8
To Count
?????????? ????? ?????? ?????????.
use IterTools\Reduce;
$someIterable = ImportantThing::getCollectionAsIterable();
$length = Reduce::toCount($someIterable);
// 3
### To First
?????????? ?????? ??????? ?????????.
??????? \LengthException ???? ????????? ?????.
use IterTools\Reduce;
$input = [10, 20, 30];
$result = Reduce::toFirst($input);
// 10
To First And Last
?????????? ?????? ? ????????? ???????? ?????????.
??????? `\LengthException` ???? ???????? ? ?????? ????????? ?????.
use IterTools\Reduce;
$input = [10, 20, 30];
$result = Reduce::toFirstAndLast($input);
// [10, 30]
### To Last
?????????? ????????? ??????? ?????????.
??????? \LengthException ???? ????????? ?????.
use IterTools\Reduce;
$input = [10, 20, 30];
$result = Reduce::toLast($input);
// 30
To Max
?????????? ???????????? ??????? ?????????.
- ??????? `$compareBy` ?????? ?????????? ????????? ????????.
- ???? ???????? `$compareBy` ?? ???????, ???????? ????????? ?????? ???? ????????.
- ??? ?????? ????????? ?????????? `null`.
use IterTools\Reduce;
$numbers = [5, 3, 1, 2, 4];
$result = Reduce::toMax($numbers);
// 5
$movieRatings = [ [
'title' => 'Star Wars: Episode IV - A New Hope',
'rating' => 4.6
],
[
'title' => 'Star Wars: Episode V - The Empire Strikes Back',
'rating' => 4.8
],
[
'title' => 'Star Wars: Episode VI - Return of the Jedi',
'rating' => 4.6
],
];
$compareBy = fn ($movie) => $movie['rating'];
$highestRatedMovie = Reduce::toMax($movieRatings, $compareBy);
// [
// 'title' => 'Star Wars: Episode V - The Empire Strikes Back',
// 'rating' => 4.8
// ];
### To Min
?????????? ??????????? ??????? ?????????.
-
??????? `$compareBy` ?????? ?????????? ????????? ????????.
-
???? ???????? `$compareBy` ?? ???????, ???????? ????????? ?????? ???? ????????.
-
??? ?????? ????????? ?????????? `null`.
use IterTools\Reduce;
$numbers = [5, 3, 1, 2, 4];
$result = Reduce::toMin($numbers);
// 1
$movieRatings = [
[
'title' => 'The Matrix',
'rating' => 4.7
],
[
'title' => 'The Matrix Reloaded',
'rating' => 4.3
],
[
'title' => 'The Matrix Revolutions',
'rating' => 3.9
],
[
'title' => 'The Matrix Resurrections',
'rating' => 2.5
],
];
$compareBy = fn ($movie) => $movie['rating'];
$lowestRatedMovie = Reduce::toMin($movieRatings, $compareBy);
// [
// 'title' => 'The Matrix Resurrections',
// 'rating' => 2.5
// ]
To Min Max
?????????? ??????????? ? ???????????? ???????? ?????????.
- ??????? `$compareBy` ?????? ?????????? ????????? ????????.
- ???? ???????? `$compareBy` ?? ???????, ???????? ????????? ?????? ???? ????????.
- ??? ?????? ????????? ?????????? `[null, null]`.
use IterTools\Reduce;
$numbers = [1, 2, 7, -1, -2, -3];;
[$min, $max] = Reduce::toMinMax($numbers);
// [-3, 7]
$reportCard = [ [
'subject' => 'history',
'grade' => 90
],
[
'subject' => 'math',
'grade' => 98
],
[
'subject' => 'science',
'grade' => 92
],
[
'subject' => 'english',
'grade' => 85
],
[
'subject' => 'programming',
'grade' => 100
],
];
$compareBy = fn ($class) => $class['grade'];
$bestAndWorstSubject = Reduce::toMinMax($reportCard, $compareBy);
// [
// [
// 'subject' => 'english',
// 'grade' => 85
// ],
// [
// 'subject' => 'programming',
// 'grade' => 100
// ],
// ]
### To Product
?????????? ???????????? ????????? ?????????.
??? ?????? ????????? ?????????? `null`.
use IterTools\Reduce;
$primeFactors = [5, 2, 2];
$number = Reduce::toProduct($primeFactors);
// 20
To Range
?????????? ???????? ????????????? ? ???????????? ????????? ?????????.
??? ?????? ????????? ?????????? `0`.
use IterTools\Reduce;
$grades = [100, 90, 80, 85, 95];
$range = Reduce::toRange($numbers);
// 20
### To String
??????????? ????????? ? ??????, "????????" ?? ????????.
* ???????? ??????????????? ????????? `$separator` ??????????? ? ???????? ??????????? ????? ?????????? ? ??????.
* ???????? ??????????????? ????????? `$prefix` ??????????? ? ?????? ??????.
* ???????? ??????????????? ????????? `$suffix` ??????????? ? ????? ??????.
use IterTools\Reduce;
$words = ['IterTools', 'PHP', 'v1.0'];
$string = Reduce::toString($words);
// IterToolsPHPv1.0
$string = Reduce::toString($words, '-');
// IterTools-PHP-v1.0
$string = Reduce::toString($words, '-', 'Library: ');
// Library: IterTools-PHP-v1.0
$string = Reduce::toString($words, '-', 'Library: ', '!');
// Library: IterTools-PHP-v1.0!
To Sum
?????????? ????? ????????? ?????????.
use IterTools\Reduce;
$parts = [10, 20, 30];
$sum = Reduce::toSum($parts);
// 60
### To Value
?????????? ????????? ?? ????????, ???????????? ? ?????????????? callback-???????.
use IterTools\Reduce;
$input = [1, 2, 3, 4, 5];
$sum = fn ($carry, $item) => $carry + $item;
$result = Reduce::toValue($input, $sum, 0);
// 15
?????????? ????? ??????????
????????????? ?????? ??????? ????????? ??? ?????????????? ???????? ? ?????? ??????????? ????????? ? ??????? ????????? ????????.
?????? ?????????? ???????? ? ????:
-
????????? ?????? ??? ???????? ???????, ???????????????? ??????? ????????? ??? ?????? ? ???????????? ??????????.
-
?????? ??? ?????????????? ???????? ????????? ?????? ? ????? ?????.
-
??????? ?????????? ?????? ??????????????:
* ??????, ????????????? ????? ? ????????? ???????? ??? ? ????????? ??????.
$result = Stream::of([1, 1, 2, 2, 3, 4, 5])
->distinct() // [1, 2, 3, 4, 5]
->map(fn ($x) => $x2) // [1, 4, 9, 16, 25]
->filterTrue(fn ($x) => $x < 10) // [1, 4, 9]
->toSum(); // 14
* ??????????? ?????????????? ????????? ?????? ?? ????? ????? ? ?????????????? ????? `foreach`.
$result = Stream::of([1, 1, 2, 2, 3, 4, 5])
->distinct() // [1, 2, 3, 4, 5]
->map(fn ($x) => $x2) // [1, 4, 9, 16, 25]
->filterTrue(fn ($x) => $x < 10); // [1, 4, 9]
foreach ($result as $item) {
// 1, 4, 9
}
????????? ??????
Of
??????? ????? ?? ?????? ?????????.
use IterTools\Stream;
$iterable = [1, 2, 3];
$result = Stream::of($iterable) ->chainWith([4, 5, 6], [7, 8, 9])
->zipEqualWith([1, 2, 3, 4, 5, 6, 7, 8, 9])
->toValue(fn ($carry, $item) => $carry + array_sum($item));
// 90
#### Of Coin Flips
??????? ????? ?? ??????????? ????????? ??????? ??????.
use IterTools\Stream;
$result = Stream::ofCoinFlips(10)
->filterTrue()
->toCount();
// 5 (random)
Of CSV File
??????? ????? ?? ????? CSV-?????.
use IterTools\Stream;
$fileHandle = \fopen('path/to/file.csv', 'r');
$result = Stream::of($fileHandle) ->toArray();
#### Of Empty
??????? ????? ?? ?????? ?????????.
use IterTools\Stream;
$result = Stream::ofEmpty()
->chainWith([1, 2, 3])
->toArray();
// 1, 2, 3
Of File Lines
??????? ????? ?? ????? ?????.
use IterTools\Stream;
$fileHandle = \fopen('path/to/file.txt', 'r');
$result = Stream::of($fileHandle) ->map('strtoupper');
->toArray();
#### Of Random Choice
??????? ????? ?? ??????????? ????????? ??????? ???????? ?? ??????.
use IterTools\Stream;
$languages = ['PHP', 'Go', 'Python'];
$languages = Stream::ofRandomChoice($languages, 5)
->toArray();
// 'Go', 'PHP', 'Python', 'PHP', 'PHP' (random)
Of Random Numbers
??????? ????? ?? ???????????? ?????? ????????? ????? ?????.
use IterTools\Stream;
$min = 1;
$max = 3;
$reps = 7;
$result = Stream::ofRandomNumbers($min, $max, $reps) ->toArray();
// 1, 2, 2, 1, 3, 2, 1 (random)
#### Of Random Percentage
??????? ????? ?? ???????????? ?????? ????????? ???????????? ????? ????? 0 ? 1.
use IterTools\Stream;
$stream = Stream::ofRandomPercentage(3)
->toArray();
// 0.8012566976245, 0.81237281724151, 0.61676896329459 [random]
Of Range
??????? ????? ??? ?????????? ??????? ?? ?????????????? ??????????.
use IterTools\Stream;
$numbers = Stream::ofRange(0, 5) ->toArray();
// 0, 1, 2, 3, 4, 5
#### Of Rock Paper Scissors
??????? ????? ?? ??????????? ????????? ??????? "??????-???????-??????".
use IterTools\Stream;
$rps = Stream::ofRockPaperScissors(5)
->toArray();
// 'paper', 'rock', 'rock', 'scissors', 'paper' [random]
?????????? ????????
ASort
????????? ????????? ? ?????? ? ??????????? ??????.
???? `$comparator` ?? ???????, ???????? ???????? ????????? ?????? ???? ????????.
use IterTools\Stream;
$worldPopulations = [ 'China' => 1_439_323_776,
'India' => 1_380_004_385,
'Indonesia' => 273_523_615,
'USA' => 331_002_651,
];
$result = Stream::of($worldPopulations) ->filter(fn ($pop) => $pop > 300_000_000)
->asort()
->toAssociativeArray();
// USA => 331_002_651,
// India => 1_380_004_385,
// China => 1_439_323_776,
#### Chain With
????????? ? ????? ?????????? ????????? ?????? ????????? ??? ????????????????? ????????????.
??????? ???? ??????? ?????????????????? ?? ?????????????????? ? ?????? ? ?????????? ?????? ???????????????????.
use IterTools\Stream;
$input = [1, 2, 3];
$result = Stream::of($input)
->chainWith([4, 5, 6])
->chainWith([7, 8, 9])
->toArray();
// 1, 2, 3, 4, 5, 6, 7, 8, 9
Compress
??????????????? ?? ?????? ????????, ??????? ?? ???????.
?????? ?????????? ????????, ????? ???????? ???????? ? ??????? (???????? ????????? `1`),
? ????? ????????? (???????? ????????? `0`).
use IterTools\Stream;
$input = [1, 2, 3];
$result = Stream::of($input) ->compress([0, 1, 1])
->toArray();
// 2, 3
#### Compress Associative
???????? ?? ???????? ????????? ???????? ?? ???????? ??????.
-
??????? ????? ???? ?????? ?????? ??? ????? ????? (?? ???????? ? ??????? PHP-????????).
use IterTools\Stream;
$starWarsEpisodes = [
'I' => 'The Phantom Menace',
'II' => 'Attack of the Clones',
'III' => 'Revenge of the Sith',
'IV' => 'A New Hope',
'V' => 'The Empire Strikes Back',
'VI' => 'Return of the Jedi',
'VII' => 'The Force Awakens',
'VIII' => 'The Last Jedi',
'IX' => 'The Rise of Skywalker',
];
$sequelTrilogyNumbers = ['VII', 'VIII', 'IX'];
$sequelTrilogy = Stream::of($starWarsEpisodes)
->compressAssociative($sequelTrilogyNumbers)
->toAssociativeArray();
// 'VII' => 'The Force Awakens',
// 'VIII' => 'The Last Jedi',
// 'IX' => 'The Rise of Skywalker',
Chunkwise
????????? ???????? ?? ?????? ? ?????????? ?? ??????.
??????????? ?????? ????? ? 1.
use IterTools\Stream;
$friends = ['Ross', 'Rachel', 'Chandler', 'Monica', 'Joey'];
$result = Stream::of($friends) ->chunkwise(2)
->toArray();
// ['Ross', 'Rachel'], ['Chandler', 'Monica'], ['Joey']
#### Chunkwise Overlap
????????? ???????? ?? ?????? ? ?????????? ?? ????????????????????? ??????.
-
??????????? ?????? ????? ? 1.
-
?????? ????????? ?????? ???? ?????? ????? ?????.
use IterTools\Stream;
$numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];
$result = Stream::of($friends)
->chunkwiseOverlap(3, 1)
->toArray()
// [1, 2, 3], [3, 4, 5], [5, 6, 7], [7, 8, 9]
Distinct
????????? ???????? ?? ??????, ???????? ?????? ?????????? ????????.
?? ????????? ????????? ????????? ? ?????? ??????? ?????????. ????????? ???????? `false` ????????? `$strict`, ????? ???????? ? ?????? ?????????? ?????.
use IterTools\Stream;
$input = [1, 2, 1, 2, 3, 3, '1', '1', '2', '3'];
$stream = Stream::of($input) ->distinct()
->toArray();
// 1, 2, 3, '1', '2', '3'
$stream = Stream::of($input) ->distinct(false)
->toArray();
// 1, 2, 3
#### Drop While
?????????? ???????? ?? ??????, ???? ???????? ?????????? ????.
????? ???? ??? ???????? ??????? ?????? true , ??? ??????????? ???????? ???????? ? ???????.
use IterTools\Stream;
$input = [1, 2, 3, 4, 5]
$result = Stream::of($input)
->dropWhile(fn ($value) => $value < 3)
->toArray();
// 3, 4, 5
Filter
?????????? ?? ?????? ?????? ?? ????????, ??? ??????? ???????? ?????????? ??????.
?? ????????? (???? ?? ???????) ???????? ???????? ???????? ????????? ? `bool`.
use IterTools\Stream;
$input = [1, -1, 2, -2, 3, -3];
$result = Stream::of($input) ->filter(fn ($value) => $value > 0)
->toArray();
// 1, 2, 3
#### Filter True
?????????? ?? ?????? ?????? ???????? ????????. ?????????? ???????????? ??????????.
?? ????????? (???? ?? ???????) ???????? ???????? ???????? ????????? ? bool .
use IterTools\Stream;
$input = [0, 1, 2, 3, 0, 4];
$result = Stream::of($input)
->filterTrue()
->toArray();
// 1, 2, 3, 4
Filter False
?????????? ?? ?????? ?????? ?????? ????????. ?????????? ???????????? ??????????.
?? ????????? (???? ?? ???????) ???????? ???????? ???????? ????????? ? `bool`.
use IterTools\Stream;
$input = [0, 1, 2, 3, 0, 4];
$result = Stream::of($input) ->filterFalse(fn ($value) => $value > 0)
->filterFalse()
->toArray();
// 0, 0
#### Filter Keys
?????????? ?? ?????? ?????? ?? ????????, ??? ?????? ??????? ???????? ?????????? ??????.
$olympics = [
2000 => 'Sydney',
2002 => 'Salt Lake City',
2004 => 'Athens',
2006 => 'Turin',
2008 => 'Beijing',
2010 => 'Vancouver',
2012 => 'London',
2014 => 'Sochi',
2016 => 'Rio de Janeiro',
2018 => 'Pyeongchang',
2020 => 'Tokyo',
2022 => 'Beijing',
];
$winterFilter = fn ($year) => $year % 4 === 2;
$result = Stream::of($olympics)
->filterKeys($winterFilter)
->toAssociativeArray();
}
// 2002 => Salt Lake City
// 2006 => Turin
// 2010 => Vancouver
// 2014 => Sochi
// 2018 => Pyeongchang
// 2022 => Beijing
Flat Map
??????????? ????????? ?? ?????? ? ?????????? ?????????? ?? 1 ??????? ???????????.
$data = [1, 2, 3, 4, 5];
$mapper fn ($item) => ($item % 2 === 0) ? [$item, $item] : $item;
$result = Stream::of($data) ->flatMap($mapper)
->toArray();
// [1, 2, 2, 3, 4, 4, 5]
#### Flatten
?????????????? ????????? ????????? ?? ??????.
$data = [1, [2, 3], [4, 5]];
$result = Stream::of($data)
->flatten($mapper)
->toArray();
// [1, 2, 3, 4, 5]
Group By
?????????? ???????? ?? ?????? ?? ????????? ???????.
??????? `$groupKeyFunction` ?????? ?????????? ????? ???? ??? ????????? ??????.
use IterTools\Stream;
$input = [1, -1, 2, -2, 3, -3];
$result = Stream::of($input) ->groupBy(fn ($item) => $item > 0 ? 'positive' : 'negative');
foreach ($result as $group => $item) { // 'positive' => [1, 2, 3], 'negative' => [-1, -2, -3]
}
#### Infinite Cycle
?????????? ??????????? ??????? ????????? ??????.
use IterTools\Stream;
$input = [1, 2, 3];
$result = Stream::of($input)
->infiniteCycle()
->print();
// 1, 2, 3, 1, 2, 3, ...
Intersection With
??????????? ???????? ? ?????? ????????? ? ??????? ??????????? ???????????.
use IterTools\Stream;
$numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];
$numerics = ['1', '2', 3, 4, 5, 6, 7, '8', '9'];
$oddNumbers = [1, 3, 5, 7, 9, 11];
$stream = Stream::of($numbers) ->intersectionWith($numerics, $oddNumbers)
->toArray();
// 3, 5, 7
#### Intersection Coercive With
??????????? ???????? ? ?????? ????????? ? ??????? ??????????? ??????????? ? ?????? ?????????? ?????.
use IterTools\Stream;
$languages = ['php', 'python', 'c++', 'java', 'c#', 'javascript', 'typescript'];
$scriptLanguages = ['php', 'python', 'javascript', 'typescript'];
$supportsInterfaces = ['php', 'java', 'c#', 'typescript'];
$stream = Stream::of($languages)
->intersectionCoerciveWith($scriptLanguages, $supportsInterfaces)
->toArray();
// 'php', 'typescript'
Limit
???????????? ???????????? ????????? ?? ?????? ???????? ???????????? ?????? ????????.
????????????? ??????? ????????????, ????? ????? ???????? ????????? $limit .
Use IterTools\Single;
$matrixMovies = ['The Matrix', 'The Matrix Reloaded', 'The Matrix Revolutions', 'The Matrix Resurrections'];
$limit = 1;
$goodMovies = Stream::of($matrixMovies) ->limit($limit)
->toArray();
// 'The Matrix' (and nothing else)
#### Map
??????????? ???????? ? ?????? ????????? ? ?????????????? callback-???????.
use IterTools\Stream;
$grades = [100, 95, 98, 89, 100];
$result = Stream::of($grades)
->map(fn ($grade) => $grade === 100 ? 'A' : 'F')
->toArray();
// A, F, F, F, A
Pairwise
????????? ???????? ?? ?????? ??????? (? ??????????).
???????? ????? ???????? ??????, ???? ???????? ???????? ?????? 2-? ?????????.
use IterTools\Stream;
$input = [1, 2, 3, 4, 5];
$stream = Stream::of($input) ->pairwise()
->toArray();
// [1, 2], [2, 3], [3, 4], [4, 5]
#### Partial Intersection With
????????? ??????????? ???????? ? ?????? ????????? ? ??????? ??????????? ???????????.
use IterTools\Stream;
$numbers = [1, 2, 3, 4, 5, 6, 7, 8, 9];
$numerics = ['1', '2', 3, 4, 5, 6, 7, '8', '9'];
$oddNumbers = [1, 3, 5, 7, 9, 11];
$stream = Stream::of($numbers)
->partialIntersectionWith($numerics, $oddNumbers)
->toArray();
// 1, 3, 4, 5, 6, 7, 9
Partial Intersection Coercive With
????????? ??????????? ???????? ? ?????? ????????? ? ??????? ??????????? ???????????, ??????????? ? ?????? ?????????? ?????.
use IterTools\Stream;
$languages = ['php', 'python', 'c++', 'java', 'c#', 'javascript', 'typescript'];
$scriptLanguages = ['php', 'python', 'javascript', 'typescript'];
$supportsInterfaces = ['php', 'java', 'c#', 'typescript'];
$stream = Stream::of($languages) ->partialIntersectionCoerciveWith(2, $scriptLanguages, $supportsInterfaces)
->toArray();
// 'php', 'python', 'java', 'typescript', 'c#', 'javascript'
#### Reindex
??????????????? key-value ????????? ?? ??????, ????????? ???????-??????????.
use IterTools\Single;
$data = [
[
'title' => 'Star Wars: Episode IV ? A New Hope',
'episode' => 'IV',
'year' => 1977,
],
[
'title' => 'Star Wars: Episode V ? The Empire Strikes Back',
'episode' => 'V',
'year' => 1980,
],
[
'title' => 'Star Wars: Episode VI ? Return of the Jedi',
'episode' => 'VI',
'year' => 1983,
],
];
$reindexFunc = fn (array $swFilm) => $swFilm['episode'];
$reindexResult = Stream::of($data)
->reindex($reindexFunc)
->toAssociativeArray();
// [
// 'IV' => [
// 'title' => 'Star Wars: Episode IV ? A New Hope',
// 'episode' => 'IV',
// 'year' => 1977,
// ],
// 'V' => [
// 'title' => 'Star Wars: Episode V ? The Empire Strikes Back',
// 'episode' => 'V',
// 'year' => 1980,
// ],
// 'VI' => [
// 'title' => 'Star Wars: Episode VI ? Return of the Jedi',
// 'episode' => 'VI',
// 'year' => 1983,
// ],
// ]
Reverse
????????? ????????? ?? ?????? ? ???????? ???????.
use IterTools\Stream;
$words = ['are', 'you', 'as', 'bored', 'as', 'I', 'am'];
$reversed = Stream::of($words) ->reverse()
->toString(' ');
// am I as bored as you are
#### Running Average
??????????? ??????? ?????????????? ????????? ?? ?????? ? ???????? ????????????.
use IterTools\Stream;
$input = [1, 3, 5];
$result = Stream::of($input)
->runningAverage();
foreach ($result as $item) {
// 1, 2, 3
}
Running Difference
??????????? ???????? ????????? ?? ?????? ? ???????? ????????????.
use IterTools\Stream;
$input = [1, 2, 3, 4, 5];
$result = Stream::of($input) ->runningDifference()
->toArray();
// -1, -3, -6, -10, -15
#### Running Max
?????????? ?????, ?????? ???????????? ??????? ?? ????????? ?????? ? ???????? ????????????.
use IterTools\Stream;
$input = [1, -1, 2, -2, 3, -3];
$result = Stream::of($input)
->runningMax()
->toArray();
// 1, 1, 2, 2, 3, 3
Running Min
?????????? ?????, ?????? ??????????? ??????? ?? ????????? ?????? ? ???????? ????????????.
use IterTools\Stream;
$input = [1, -1, 2, -2, 3, -3];
$result = Stream::of($input) ->runningMin()
->toArray();
// 1, -1, -1, -2, -2, -3
#### Running Product
?????????? ?????, ????????????? ???????????? ????????? ?? ????????? ?????? ? ???????? ????????????.
use IterTools\Stream;
$input = [1, 2, 3, 4, 5];
$result = Stream::of($input)
->runningProduct()
->toArray();
// 1, 2, 6, 24, 120
Running Total
?????????? ?????, ????????????? ????? ????????? ?? ????????? ?????? ? ???????? ????????????.
use IterTools\Stream;
$input = [1, 2, 3, 4, 5];
$result = Stream::of($input) ->runningTotal()
->toArray();
// 1, 3, 6, 10, 15
#### Slice
???????? ?????????? ????????? ?? ??????.
use IterTools\Stream;
$olympics = [1992, 1994, 1996, 1998, 2000, 2002, 2004, 2006, 2008, 2010, 2012, 2014, 2016, 2018, 2020, 2022];
$summerOlympics = Stream::of($olympics)
->slice(0, 8, 2)
->toArray();
// [1992, 1996, 2000, 2004, 2008, 2012, 2016, 2020]
Sort
????????? ???????? ? ?????? ?????????.
???? `$comparator` ?? ???????, ???????? ???????? ????????? ?????? ???? ????????.
use IterTools\Stream;
$input = [3, 4, 5, 9, 8, 7, 1, 6, 2];
$result = Stream::of($input) ->sort()
->toArray();
// 1, 2, 3, 4, 5, 6, 7, 8, 9
#### Symmetric difference With
?????????? ?????, ?????????? ?????????????? ???????? ????????? ?????? ? ???????? ??????? ?????????.
???? ???? ?? ? ????? ????????? ??? ? ?????? ??????????? ????????????? ????????, ???????? ??????? ????????? ???????? ??? ??????????????.
use IterTools\Stream;
$a = [1, 2, 3, 4, 7];
$b = ['1', 2, 3, 5, 8];
$c = [1, 2, 3, 6, 9];
$stream = Stream::of($a)
->symmetricDifferenceWith($b, $c)
->toArray();
// '1', 4, 5, 6, 7, 8, 9
Symmetric difference Coercive With
?????????? ?????, ?????????? ?????????????? ???????? ????????? ?????? ? ???????? ??????? ?????????, ?????????? ? ?????? ?????????? ?????.
???? ???? ?? ? ????? ????????? ??? ? ?????? ??????????? ????????????? ????????, ???????? ??????? ????????? ???????? ??? ??????????????.
use IterTools\Stream;
$a = [1, 2, 3, 4, 7];
$b = ['1', 2, 3, 5, 8];
$c = [1, 2, 3, 6, 9];
$stream = Stream::of($a) ->symmetricDifferenceCoerciveWith($b, $c)
->toArray();
// 4, 5, 6, 7, 8, 9
#### Take While
????????? ???????? ? ??????, ???? ???????? ?????????? ??????.
-
????????????? ??????? ????????, ??? ?????? ???????? ??????? ?????? ????.
-
?? ????????? (???? ?? ???????) ???????? ???????? ???????? ????????? ? `bool`.
use IterTools\Stream;
$input = [1, -1, 2, -2, 3, -3];
$result = Stream::of($input)
->takeWhile(fn ($value) => abs($value) < 3);
foreach ($result as $item) {
// 1, -1, 2, -2
}
Zip With
??????????? ????????? ???????? ?? ?????? ?????? ? ?????????? ?????????? ?????????, ???? ?? ?????????? ????? ???????? ????????.
* ??????? ????????, ??????? ?????????? ?????? ?? ?????????? ??????????.
* ???????? ?????????? ??????? `zip()` ? Python.
* ??? ????????? ?????? ????? ?????????? ??????? ???????????? ?? ???????, ???? ????? ???????? ????????? ?? ??????????.
use IterTools\Stream;
$input = [1, 2, 3];
$stream = Stream::of($input) ->zipWith([4, 5, 6])
->zipWith([7, 8, 9])
->toArray();
// [1, 4, 7], [2, 5, 8], [3, 6, 9]
#### Zip Longest With
??????????? ????????? ???????? ?? ?????? ?????? ? ?????????? ?????????? ?????????, ???? ?? ?????????? ????? ??????? ????????.
-
??????? ????????, ??????? ?????????? ?????? ?? ?????????? ??????????.
-
???????? ?????????? ??????? `zip_longest()` ? Python.
-
??? ????????? ?????? ????? ?????????? ??????? ???????????? ?? ???????, ???? ????? ??????? ????????? ?? ??????????.
-
??? ????????? ?????? ????? ?????? ?????? ????????? `null` ??? ?????????, ??????? ???????????.
use IterTools\Stream;
$input = [1, 2, 3, 4, 5];
$result = Stream::of($input)
->zipLongestWith([4, 5, 6])
->zipLongestWith([7, 8, 9, 10]);
foreach ($result as $item) {
// [1, 4, 7], [2, 5, 8], [3, 6, 9], [4, null, 10], [null, null, 5]
}
Zip Equal With
??????????? ????????? ???????? ?? ?????? ?????? ? ?????????? ?????????? ????????? (??? ????????? ?????? ???? ????? ?????).
???????? ??? `Multi::zip()`, ?? ??????? `\LengthException`, ????? ??????????, ??? ????? ????????? ??????
(????? ?????????? ????? ???????? ?????????).
use IterTools\Stream;
$input = [1, 2, 3];
$result = Stream::of($input) ->zipEqualWith([4, 5, 6])
->zipEqualWith([7, 8, 9]);
foreach ($result as $item) { // [1, 4, 7], [2, 5, 8], [3, 6, 9]
}
### ??????????? ????????
#### ??????? ? ??????
##### All Match
?????????? ??????, ???? ??? ???? ????????? ?? ?????? ???????? ?????????? ??????.
use IterTools\Summary;
$finalFantasyNumbers = [4, 5, 6];
$isOnSuperNintendo = fn ($ff) => $ff >= 4 && $ff <= 6;
$boolean = Stream::of($finalFantasyNumbers)
->allMatch($isOnSuperNintendo);
// true
Any Match
?????????? ??????, ???? ???? ?? ??? ?????? ???????? ?? ?????? ???????? ?????????? ??????.
use IterTools\Summary;
$answers = ['fish', 'towel', 42, "don't panic"];
$isUltimateAnswer = fn ($a) => a == 42;
$boolean = Stream::of($answers) ->anyMatch($answers, $isUltimateAnswer);
// true
##### Are Permutations With
????????? ??????, ???? ????????? ?? ?????? ? ?????????? ????????? ???????? ?????????????? ???? ?????.
use IterTools\Summary;
$rite = ['r', 'i', 't', 'e'];
$reit = ['r', 'e', 'i', 't'];
$tier = ['t', 'i', 'e', 'r'];
$tire = ['t', 'i', 'r', 'e'];
$trie = ['t', 'r', 'i', 'e'];
$boolean = Stream::of(['i', 't', 'e', 'r'])
->arePermutationsWith($rite, $reit, $tier, $tire, $trie);
// true
Are Permutations Coercive With
????????? ??????, ???? ????????? ?? ?????? ? ?????????? ????????? ???????? ?????????????? ???? ?????
(? ?????? ?????????? ?????).
use IterTools\Summary;
$set2 = [2.0, '1', 3];
$set3 = [3, 2, 1];
$boolean = Stream::of([1, 2.0, '3']) ->arePermutationsCoerciveWith($set2, $set3);
// true
##### Exactly N
?????????? ??????, ???? ? ???????? ??? n ????????? ?? ?????? ???????? ?????????? ??????.
- ???????? ???????? ?????????????? ??????????.
- ?? ????????? (???? ?? ???????) ???????? ???????? ???????? ????????? ? `bool`.
use IterTools\Summary;
$twoTruthsAndALie = [true, true, false];
$n = 2;
$boolean = Stream::of($twoTruthsAndALie)->exactlyN($n);
// true
Is Partitioned
?????????? ??????, ???? ??? ???????? ???????? ???????? ?? ?????? ????????? ? ????????? ????? ???????
(?????????? ?????????? ????????).
-
?????????? ?????? ??? ?????? ????????? ? ??? ????????? ? ????? ?????????.
-
???? ???????? ?? ??? ???????, ?????????? ???????? ?????????? ????? ?????????? ??? ???????? ? ???????? ????.
use IterTools\Summary;
$numbers = [0, 2, 4, 1, 3, 5];
$evensBeforeOdds = fn ($item) => $item % 2 === 0;
$boolean = Stream::($numbers) ->isPartitioned($evensBeforeOdds);
// true
##### Is Sorted
?????????? ??????, ???? ????????? ????????? ?? ?????? ????????????? ? ?????? ???????, ????? ? ????.
???????? ?????? ???? ????????.
??? ?????? ????????? ??? ????????? ?? ?????? ???????? ?????? ?????????? ??????.
use IterTools\Stream;
$input = [1, 2, 3, 4, 5];
$result = Stream::of($input)
->isSorted();
// true
$input = [1, 2, 3, 2, 1];
$result = Stream::of($input)
->isSorted();
// false
Is Reversed
?????????? ??????, ???? ????????? ????????? ?? ?????? ????????????? ? ???????? ???????, ????? ? ????.
???????? ?????? ???? ????????.
??? ?????? ????????? ??? ????????? ?? ?????? ???????? ?????? ?????????? ??????.
use IterTools\Stream;
$input = [5, 4, 3, 2, 1];
$result = Stream::of($input) ->isReversed();
// true
$input = [1, 2, 3, 2, 1];
$result = Stream::of($input) ->isReversed();
// false
##### None Match
?????????? ??????, ???? ??? ???? ????????? ?? ?????? ???????? ?????? ????.
use IterTools\Summary;
$grades = [45, 50, 61, 0];
$isPassingGrade = fn ($grade) => $grade >= 70;
$boolean = Stream::of($grades)->noneMatch($isPassingGrade);
// true
Same With
?????????? ??????, ???? ????????? ????????? ?? ?????? ????????? ?????????? ? ?????????? ??????????.
???? ? ????? ?? ???????? ?? ????? ?????????, ?? ?????? ??????.
use IterTools\Stream;
$input = [1, 2, 3, 4, 5];
$result = Stream::of($input) ->sameWith([1, 2, 3, 4, 5]);
// true
$result = Stream::of($input) ->sameWith([5, 4, 3, 2, 1]);
// false
##### Same Count With
?????????? ??????, ???? ? ????????? ????????? ?? ??????, ? ??? ?????????? ????????? ????? ?????????? ?????.
???? ? ????? ?? ???????? ?? ????? ?????????, ?? ?????? ??????.
use IterTools\Stream;
$input = [1, 2, 3, 4, 5];
$result = Stream::of($input)
->sameCountWith([5, 4, 3, 2, 1]);
// true
$result = Stream::of($input)
->sameCountWith([1, 2, 3]);
// false
?????????????
To Average
?????????? ??????? ?????????????? ????????? ????????? ?? ??????.
??? ?????? ????????? ?????? `null`.
use IterTools\Stream;
$input = [2, 4, 6, 8];
$result = Stream::of($iterable) ->toAverage();
// 5
##### To Count
?????????? ????? ????????? ????????? ?? ??????.
use IterTools\Stream;
$input = [10, 20, 30, 40, 50];
$result = Stream::of($iterable)
->toCount();
// 5
To First
?????????? ?????? ??????? ?? ????????? ? ??????.
??????? `\LengthException` ???? ???????? ? ?????? ????????? ?????.
use IterTools\Stream;
$input = [10, 20, 30];
$result = Stream::of($input) ->toFirst();
// 10
##### To First And Last
?????????? ?????? ? ????????? ???????? ?? ????????? ? ??????.
??????? \LengthException ???? ???????? ? ?????? ????????? ?????.
use IterTools\Stream;
$input = [10, 20, 30];
$result = Stream::of($input)
->toFirstAndLast();
// [10, 30]
To Last
?????????? ????????? ??????? ?? ????????? ? ??????.
??????? `\LengthException` ???? ???????? ? ?????? ????????? ?????.
use IterTools\Stream;
$input = [10, 20, 30];
$result = Stream::of($input) ->toLast();
// 30
##### To Max
?????????? ???????????? ??????? ????????? ?? ??????.
-
??????? `$compareBy` ?????? ?????????? ????????? ????????.
-
???? ???????? `$compareBy` ?? ???????, ???????? ????????? ?????? ???? ????????.
-
??? ?????? ????????? ?????? `null`.
use IterTools\Stream;
$input = [1, -1, 2, -2, 3, -3];
$result = Stream::of($iterable)
->toMax();
// 3
To Min
?????????? ??????????? ??????? ????????? ?? ??????.
- ??????? `$compareBy` ?????? ?????????? ????????? ????????.
- ???? ???????? `$compareBy` ?? ???????, ???????? ????????? ?????? ???? ????????.
- ??? ?????? ????????? ?????? `null`.
use IterTools\Stream;
$input = [1, -1, 2, -2, 3, -3];
$result = Stream::of($iterable) ->toMin();
// -3
##### To Min Max
?????????? ??????????? ? ???????????? ???????? ????????? ?? ??????.
-
??????? `$compareBy` ?????? ?????????? ????????? ????????.
-
???? ???????? `$compareBy` ?? ???????, ???????? ????????? ?????? ???? ????????.
-
??? ?????? ????????? ?????? `[null, null]`.
use IterTools\Stream;
$numbers = [1, 2, 3, -1, -2, -3];
[$min, $max] = Stream::of($numbers)
->toMinMax();
// [-3, 3]
To Product
?????????? ???????????? ????????? ????????? ?? ??????.
??? ?????? ????????? ?????? `null`.
use IterTools\Stream;
$input = [1, 2, 3, 4, 5];
$result = Stream::of($iterable) ->toProduct();
// 120
##### To Range
?????????? ??????? ????? ???????????? ? ??????????? ?????????? ????????? ?? ??????.
??? ?????? ????????? ?????? 0 .
use IterTools\Stream;
$grades = [100, 90, 80, 85, 95];
$range = Stream::of($numbers)
->toRange();
// 20
To String
??????????? ????????? ?? ?????? ? ??????, "????????" ?? ????????.
-
???????? ??????????????? ????????? `$separator` ??????????? ? ???????? ??????????? ????? ?????????? ? ??????.
-
???????? ??????????????? ????????? `$prefix` ??????????? ? ?????? ??????.
-
???????? ??????????????? ????????? `$suffix` ??????????? ? ????? ??????.
use IterTools\Stream;
$words = ['IterTools', 'PHP', 'v1.0'];
$string = Stream::of($words)->toString($words);
// IterToolsPHPv1.0
$string = Stream::of($words)->toString($words, '-');
// IterTools-PHP-v1.0
$string = Stream::of($words)->toString($words, '-', 'Library: ');
// Library: IterTools-PHP-v1.0
$string = Stream::of($words)->toString($words, '-', 'Library: ', '!');
// Library: IterTools-PHP-v1.0!
##### To Sum
?????????? ????? ????????? ????????? ?? ??????.
use IterTools\Stream;
$input = [1, 2, 3, 4, 5];
$result = Stream::of($iterable)
->toSum();
// 15
To Value
?????????? ????????? ?? ?????? ?? ????????, ???????????? ? ?????????????? callback-???????.
? ??????? ?? array_reduce() , ???????? ? ?????? iterable ??????.
use IterTools\Stream;
$input = [1, 2, 3, 4, 5];
$result = Stream::of($iterable) ->toValue(fn ($carry, $item) => $carry + $item);
// 15
#### ???????? ???????????
##### To Array
?????????? ?????? ???? ????????? ?? ??????.
use IterTools\Stream;
$array = Stream::of([1, 1, 2, 2, 3, 4, 5])
->distinct()
->map(fn ($x) => $x2)
->toArray();
// [1, 4, 9, 16, 25]
To Associative Array
?????????? ????????????? ?????? ???? ????????? ?? ??????.
use IterTools\Stream;
$keyFunc
$array = Stream::of(['message 1', 'message 2', 'message 3']) ->map('strtoupper')
->toAssociativeArray(
fn ($s) => \md5($s),
fn ($s) => $s
);
// [3b3f2272b3b904d342b2d0df2bf31ed4 => MESSAGE 1, 43638d919cfb8ea31979880f1a2bb146 => MESSAGE 2, ... ]
##### Tee
??????? ????????? ?????????? ??????????? ??????? ?? ???????.
use IterTools\Transform;
$daysOfWeek = ['Mon', 'Tues', 'Wed', 'Thurs', 'Fri', 'Sat', 'Sun'];
$count = 3;
[$week1Stream, $week2Stream, $week3Stream] = Stream::of($daysOfWeek)
->tee($count);
// Each $weekStream contains ['Mon', 'Tues', 'Wed', 'Thurs', 'Fri', 'Sat', 'Sun']
???????? ? ????????? ?????????
Call For Each
???????? callback-??????? ??? ??????? ???????? ?? ??????.
use IterTools\Stream;
$languages = ['PHP', 'Python', 'Java', 'Go'];
$mascots = ['elephant', 'snake', 'bean', 'gopher'];
$zipPrinter = fn ($zipped) => print("{$zipped[0]}'s mascot: {$zipped[1]}");
Stream::of($languages) ->zipWith($mascots)
->callForEach($zipPrinter);
// PHP's mascot: elephant
// Python's mascot: snake
// ...
##### Print
???????? `print()` ??? ??????? ???????? ?? ??????.
* ???????? ? ?????? ?????? ????? ????????? ?????????????.
use IterTools\Stream;
$words = ['IterTools', 'PHP', 'v1.0'];
Stream::of($words)->print(); // IterToolsPHPv1.0
Stream::of($words)->print('-'); // IterTools-PHP-v1.0
Stream::of($words)->print('-', 'Library: '); // Library: IterTools-PHP-v1.0
Stream::of($words)->print('-', 'Library: ', '!'); // Library: IterTools-PHP-v1.0!
Print Line
???????? ???????? ?? ?????? ?????? ? ????? ??????.
-
???????? ? ?????? ?????? ????? ????????? ?????????????.
use IterTools\Stream;
$words = ['IterTools', 'PHP', 'v1.0'];
Stream::of($words)->printLn();
// IterTools
// PHP
// v1.0
##### Print R
???????? `print_r()` ??? ??????? ???????? ?? ??????.
use IterTools\Stream;
$items = [$string, $array, $object];
Stream::of($words)->printR();
// print_r output
To CSV File
?????????? ?????????? ?????? ? CSV ????.
use IterTools\Stream;
$starWarsMovies = [ ['Star Wars: Episode IV ? A New Hope', 'IV', 1977],
['Star Wars: Episode V ? The Empire Strikes Back', 'V', 1980],
['Star Wars: Episode VI ? Return of the Jedi', 'VI', 1983],
];
$header = ['title', 'episode', 'year'];
Stream::of($data) ->toCsvFile($fh, $header);
// title,episode,year
// "Star Wars: Episode IV ? A New Hope",IV,1977
// "Star Wars: Episode V ? The Empire Strikes Back",V,1980
// "Star Wars: Episode VI ? Return of the Jedi",VI,1983
##### To File
?????????? ?????????? ?????? ? ????.
use IterTools\Stream;
$data = ['item1', 'item2', 'item3'];
$header = '<ul>';
$footer = '</ul>';
Stream::of($data)
->map(fn ($item) => " <li>$item</li>")
->toFile($fh, \PHP_EOL, $header, $footer);
// <ul>
// <li>item1</li>
// <li>item2</li>
// <li>item3</li>
// </ul>
Var Dump
???????? var_dump() ??? ??????? ???????? ?? ??????.
use IterTools\Stream;
$items = [$string, $array, $object];
Stream::of($words)->varDump();
// var_dump output
## ?????????? ???????
IterTools ????????? ????????????? ?????? ???????, ????? ???????? ????? ?????????.
#### Zip Strings
use IterTools\Multi;
use IterTools\Single;
$letters = 'ABCDEFGHI';
$numbers = '123456789';
foreach (Multi::zip(Single::string($letters), Single::string($numbers)) as [$letter, $number]) { $battleshipMove = new BattleshipMove($letter, $number)
}
// A1, B2, C3
#### Chain Strings
use IterTools\Multi;
use IterTools\Single;
$letters = 'abc';
$numbers = '123';
foreach (Multi::chain(Single::string($letters), Single::string($numbers)) as $character) { print($character);
}
// a, b, c, 1, 2, 3
## ?????? ?????????
??? ???????, ??????? ?????????? ????????? ????????? ????????? ??? ????????? ??????????,
?? ????????? ????????? ??????????? ?????? ??? ?????????? ?????:
* scalars: ?????????? ?????? ?? ????;
* objects: ?????? ??????? ?????? ?????????? ?????????;
* arrays: ?????????? ????????????????.
? ??????, ???? ????? ??????????? ???????????? ????? ?????????? ????? (????? ???????? `$strict`) ??? `$strict` ????????????? ? `false`
???? ???? ????? ????? ? ???????? ????? `Coercive`, ?? ????? ???????? ? ????????? ?????? ?????????:
* scalars: ?????????? ???????? ?? ????????;
* objects: ?????????? ????????????????;
* arrays: ?????????? ????????????????.
?????????
---------
IterTools PHP ????????????? ????????? ??????????:
* PSR-1 - Basic coding standard (http://www.php-fig.org/psr/psr-1/)
* PSR-4 - Autoloader (http://www.php-fig.org/psr/psr-4/)
* PSR-12 - Extended coding style guide (http://www.php-fig.org/psr/psr-12/)
????????
-------
IterTools PHP ???????????????? ?? ???????? MIT License.
|