Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tuples() to convert stream to [key, value] tuples #147

Merged
merged 4 commits into from
Jun 15, 2024
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions src/Standard.php
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
use function min;
use function mt_getrandmax;
use function mt_rand;
use function array_keys;

/**
* Concrete pipeline with sensible default callbacks.
Expand Down Expand Up @@ -1053,6 +1054,39 @@ private static function flipKeysAndValues(iterable $previous): iterable
}
}

/**
* @return $this
*/
public function tuples()
{
if ($this->empty()) {
// No-op: null.
return $this;
}

if (is_array($this->pipeline)) {
$this->pipeline = array_map(
fn($key, $value) => [$key, $value],
array_keys($this->pipeline),
$this->pipeline
);

return $this;
}


$this->pipeline = self::toTuples($this->pipeline);

return $this;
}

private static function toTuples(iterable $previous): iterable
{
foreach ($previous as $key => $value) {
yield [$key, $value];
}
}

private function feedRunningVariance(Helper\RunningVariance $variance, ?callable $castFunc): self
{
if (null === $castFunc) {
Expand Down
109 changes: 109 additions & 0 deletions tests/TuplesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,109 @@
<?php
/**
* Copyright 2017, 2018 Alexey Kopytko <alexey@kopytko.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

declare(strict_types=1);

namespace Tests\Pipeline;

use ArrayIterator;
use PHPUnit\Framework\TestCase;

use Pipeline\Standard;
use IteratorIterator;

use function Pipeline\fromArray;
use function round;
use function sqrt;
use function Pipeline\take;
use function Pipeline\map;

/**
* @covers \Pipeline\Standard
*
* @internal
*/
final class TuplesTest extends TestCase
{
public static function provideArrays(): iterable
{
yield 'empty_array' => [[], []];
yield 'basic_key_values' => [['a' => 1, 'b' => 2, 'c' => 3], [['a', 1], ['b', 2], ['c', 3]]];
yield 'numeric_keys' => [[10, 20, 30], [[0, 10], [1, 20], [2, 30]]];

yield 'mixed_keys_values' => [
['name' => 'Alice', 1 => 'Bob'],
[['name', 'Alice'], [1, 'Bob']],
];

yield 'null_values' => [['x' => null, 'y' => null], [['x', null], ['y', null]]];

yield 'empty_string_key' => [['' => 'value'], [['', 'value']]];

yield 'nested_arrays' => [['outer' => ['inner1' => 'value1', 'inner2' => 'value2']], [['outer', ['inner1' => 'value1', 'inner2' => 'value2']]]];
}

public static function provideIterables(): iterable
{
foreach (self::provideArrays() as $name => $item) {
yield $item;

$iteratorItem = $item;
$iteratorItem[0] = new ArrayIterator($iteratorItem[0]);

yield "$name(ArrayIterator)" => $iteratorItem;

$iteratorItem = $item;
$iteratorItem[0] = new IteratorIterator(new ArrayIterator($iteratorItem[0]));

yield "$name(IteratorIterator)" => $iteratorItem;

$iteratorItem = $item;
$iteratorItem[0] = fromArray($iteratorItem[0]);

yield "$name(Pipeline)" => $iteratorItem;
}

yield 'generator' => [map(function () {
yield '1' => 2;
yield '2' => 3;
}), [['1', 2], ['2', 3]]];
}

/**
* @dataProvider provideIterables
*/
public function testTuples(iterable $input, array $expected, bool $preserve_keys = false): void
{
$pipeline = take($input);

$pipeline->tuples();

$this->assertSame(
$expected,
$pipeline->toArray($preserve_keys)
);
}

public function testNoop(): void
{
$pipeline = new Standard();

$pipeline->tuples();

$this->assertSame([], $pipeline->toArray());
}
}
Loading