Skip to content

Introduce iterable_values() #33

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

Merged
merged 1 commit into from
Feb 25, 2021
Merged
Show file tree
Hide file tree
Changes from all 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
19 changes: 19 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,25 @@ foreach (iterable_filter($generator(), $filter) as $item) {
}
```

iterable_values()
--------------

Works like an `array_values` with an `array` or a `Traversable`.

```php
use function BenTools\IterableFunctions\iterable_values;

$generator = function () {
yield 'a' => 'a';
yield 'b' => 'b';
};

foreach (iterable_values($generator()) as $key => $value) {
var_dump($key); // 0, 1
var_dump($value); // a, b
}
```

Iterable fluent interface
=========================

Expand Down
8 changes: 8 additions & 0 deletions src/IterableObject.php
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ public function map(callable $mapper): self
return new self(array_map($mapper, $this->iterable));
}

/**
* @return self<int, TValue>
*/
public function values(): self
{
return new self(new WithoutKeysTraversable($this->iterable));
}

/** @return Traversable<TKey, TValue> */
public function getIterator(): Traversable
{
Expand Down
39 changes: 39 additions & 0 deletions src/WithoutKeysTraversable.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

declare(strict_types=1);

namespace BenTools\IterableFunctions;

use Generator;
use IteratorAggregate;

/**
* @internal
*
* @template TKey
* @template TValue
* @implements IteratorAggregate<TKey, TValue>
*/
final class WithoutKeysTraversable implements IteratorAggregate
{
/** @var iterable<TKey, TValue> */
private $iterable;

/**
* @param iterable<TKey, TValue> $iterable
*/
public function __construct(iterable $iterable)
{
$this->iterable = $iterable;
}

/**
* @return Generator<TValue>
*/
public function getIterator(): Generator
{
foreach ($this->iterable as $value) {
yield $value;
}
}
}
16 changes: 16 additions & 0 deletions src/iterable-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,22 @@ function iterable_reduce(iterable $iterable, callable $reduce, $initial = null)
return $initial;
}

/**
* Yields iterable values (leaving out keys).
*
* @param iterable<TValue> $iterable
*
* @return iterable<int, TValue>
*
* @template TValue
*/
function iterable_values(iterable $iterable): iterable
{
$withoutKeys = iterable($iterable)->values();

return is_array($iterable) ? $withoutKeys->asArray() : $withoutKeys;
}

/**
* @param iterable<TKey, TValue>|null $iterable
*
Expand Down
8 changes: 8 additions & 0 deletions tests/IterableObjectTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -116,3 +116,11 @@ static function (string $value) {
})($input),
];
});

it('strips key through values()', function (): void {
$input = ['x' => 'zero', 'y' => 'one', 'z' => 'two'];

$iterableObject = iterable($input)->values();
assertInstanceOf(IterableObject::class, $iterableObject);
assertEquals(['zero', 'one', 'two'], $iterableObject->asArray());
});
48 changes: 48 additions & 0 deletions tests/IterableValuesTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace BenTools\IterableFunctions\Tests;

use ArrayIterator;
use PHPUnit\Framework\Assert;

use function BenTools\IterableFunctions\iterable_values;
use function it;
use function PHPUnit\Framework\assertSame;

it(
'gets only values of array',
function (): void {
$iterable = ['b' => true];

// phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedForeach
foreach (iterable_values($iterable) as $key => $value) {
}

if (! isset($key, $value)) {
Assert::fail('No values were returned');
}

assertSame(0, $key);
assertSame(true, $value);
}
);

it(
'gets values of Traversable object',
function (): void {
$iterable = new ArrayIterator(['b' => true]);

// phpcs:ignore Generic.CodeAnalysis.EmptyStatement.DetectedForeach
foreach (iterable_values($iterable) as $key => $value) {
}

if (! isset($key, $value)) {
Assert::fail('No values were returned');
}

assertSame(0, $key);
assertSame(true, $value);
}
);