nikic / iter

Iteration primitives using generators
Other
1.12k stars 76 forks source link

flatten and flatMap skip values on iterator_to_array #65

Closed stephanschuler closed 6 years ago

stephanschuler commented 6 years ago

Hey there.

Flatten and flatMap reuse already yielded keys, which is perfectly fine as a traversable but swallows data when converting to array.

Demo code 1:

print_r(iterator_to_array(\iter\flatten([0, 1, [2, 3], 4, 5])));

Result 1:

Array
(
    [0] => 2
    [1] => 3
    [3] => 4
    [4] => 5
)

Demo code 2:

foreach (\iter\flatten([0, 1, [2, 3], 4, 5]) as $key => $value) {
    print_r([$key => $value]);
}

Result 2:

Array
(
    [0] => 0
)
Array
(
    [1] => 1
)
Array
(
    [0] => 2
)
Array
(
    [1] => 3
)
Array
(
    [3] => 4
)
Array
(
    [4] => 5
)

Seems like

That seems to be true for both, yield $key => $value as well as yield from $iterable.

I'm not sure if that's to be considered a bug since "keep keys intact" could be a feature as well.

Regards, Stephan.

stephanschuler commented 6 years ago

Nevermind, most likely that's what toArray is for ...