@@ 12-47 (lines=36) @@ | ||
9 | ||
10 | use Zicht\Itertools\lib\ChainIterator; |
|
11 | ||
12 | trait ChainTrait |
|
13 | { |
|
14 | /** |
|
15 | * Returns an iterator that returns elements from the first iterable |
|
16 | * until it is exhausted, then proceeds to the next iterable, until |
|
17 | * all the iterables are exhausted. |
|
18 | * |
|
19 | * Used for creating consecutive sequences as a single sequence. |
|
20 | * |
|
21 | * Note that the keys of the returned ChainIterator follow 0, 1, etc, |
|
22 | * regardless of the keys given in the iterables. |
|
23 | * |
|
24 | * > iter\iterable([1, 2, 3], [4, 5, 6])->chain() |
|
25 | * 1 2 3 4 5 6 |
|
26 | * |
|
27 | * > iter\iterable('ABC', 'DEF')->chain() |
|
28 | * A B C D E F |
|
29 | * |
|
30 | * @param array|string|\Iterator $iterable |
|
31 | * @param array|string|\Iterator $iterable2 |
|
32 | * @return null|ChainIterator |
|
33 | */ |
|
34 | public function chain(/* $iterable, $iterable2, ... */) |
|
35 | { |
|
36 | if ($this instanceof \Iterator) { |
|
37 | $iterables = array_map( |
|
38 | '\Zicht\Itertools\conversions\mixed_to_iterator', |
|
39 | func_get_args() |
|
40 | ); |
|
41 | $reflectorClass = new \ReflectionClass('\Zicht\Itertools\lib\ChainIterator'); |
|
42 | return $reflectorClass->newInstanceArgs(array_merge([$this], $iterables)); |
|
43 | } |
|
44 | ||
45 | return null; |
|
46 | } |
|
47 | } |
|
48 |
@@ 12-42 (lines=31) @@ | ||
9 | ||
10 | use Zicht\Itertools\lib\ZipIterator; |
|
11 | ||
12 | trait ZipTrait |
|
13 | { |
|
14 | /** |
|
15 | * Returns an iterator where one or more iterables are zipped together |
|
16 | * |
|
17 | * This function returns a list of tuples, where the i-th tuple contains |
|
18 | * the i-th element from each of the argument sequences or iterables. |
|
19 | * |
|
20 | * The returned list is truncated in length to the length of the |
|
21 | * shortest argument sequence. |
|
22 | * |
|
23 | * > zip([1, 2, 3], ['a', 'b', 'c']) |
|
24 | * [1, 'a'] [2, 'b'] [3, 'c'] |
|
25 | * |
|
26 | * @param array|string|\Iterator $iterable |
|
27 | * @param array|string|\Iterator $iterable2 |
|
28 | * @return ZipIterator |
|
29 | */ |
|
30 | public function zip(/* $iterable, $iterable2, ... */) |
|
31 | { |
|
32 | if ($this instanceof \Iterator) { |
|
33 | $iterables = array_map( |
|
34 | '\Zicht\Itertools\conversions\mixed_to_iterator', |
|
35 | func_get_args() |
|
36 | ); |
|
37 | $reflectorClass = new \ReflectionClass('\Zicht\Itertools\lib\ZipIterator'); |
|
38 | return $reflectorClass->newInstanceArgs(array_merge([$this], $iterables)); |
|
39 | } |
|
40 | ||
41 | return null; |
|
42 | } |
|
43 | } |
|
44 |