-
Notifications
You must be signed in to change notification settings - Fork 239
Expand file tree
/
Copy pathIter.php
More file actions
51 lines (43 loc) · 1.24 KB
/
Iter.php
File metadata and controls
51 lines (43 loc) · 1.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
namespace MathPHP\Util;
use MathPHP\Exception;
/**
* @internal
*/
class Iter
{
/**
* Zip - Make an iterator that aggregates items from multiple iterators
* Similar to Python's zip function
* @internal
*
* @param iterable ...$iterables
*
* @return \MultipleIterator
*/
public static function zip(iterable ...$iterables): \MultipleIterator
{
$zippedIterator = new \MultipleIterator();
foreach ($iterables as $iterable) {
$zippedIterator->attachIterator(self::makeIterator($iterable));
}
return $zippedIterator;
}
/**
* @param iterable $iterable
*
* @return \Iterator|\IteratorIterator|\ArrayIterator
*/
public static function makeIterator(iterable $iterable): \Iterator
{
switch (true) {
case $iterable instanceof \Iterator:
return $iterable;
case $iterable instanceof \Traversable:
return new \IteratorIterator($iterable);
case \is_array($iterable):
return new \ArrayIterator($iterable);
}
throw new \LogicException(\gettype($iterable) . ' type is not an expected iterable type (Iterator|Traversable|array)');
}
}