Skip to content

Commit

Permalink
refactor: Use Match operation in other operations.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Dec 26, 2020
1 parent fa01f65 commit f676c19
Show file tree
Hide file tree
Showing 7 changed files with 44 additions and 53 deletions.
20 changes: 19 additions & 1 deletion docs/pages/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -806,12 +806,30 @@ Signature: ``Collection::groupBy(?callable $callback = null);``
has
~~~

Check if the collection has a value. The value must be provided as a callback.
Check if the collection has a value.

Interface: `Hasable`_

Signature: ``Collection::has(callable $callback);``

.. code-block:: php
Collection::fromIterable(range('A', 'C'))
->has(
static function ($value, $key, Iterator $iterator) {
return 'A';
}
)
->current(); // true
Collection::fromIterable(range('A', 'C'))
->has(
static function ($value, $key, Iterator $iterator) {
return 'D';
}
)
->current(); // false
head
~~~~

Expand Down
6 changes: 3 additions & 3 deletions spec/loophp/collection/CollectionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -1213,15 +1213,15 @@ public function it_can_has(): void
{
$this::fromIterable(range('A', 'C'))
->has(
static function ($key, $value) {
static function ($value, $key) {
return 'A';
}
)
->shouldIterateAs([true]);

$this::fromIterable(range('A', 'C'))
->has(
static function ($key, $value) {
static function ($value, $key) {
return 'Z';
}
)
Expand All @@ -1245,7 +1245,7 @@ static function ($value, $key) {

$this::empty()
->has(
static function ($key, $value) {
static function ($value, $key) {
return $value;
}
)
Expand Down
3 changes: 2 additions & 1 deletion src/Contract/Operation/Hasable.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace loophp\collection\Contract\Operation;

use Iterator;
use loophp\collection\Contract\Collection;

/**
Expand All @@ -14,7 +15,7 @@
interface Hasable
{
/**
* @psalm-param callable(TKey, T): bool $callback
* @psalm-param callable(T, TKey, Iterator<TKey, T>): bool $callback
*
* @psalm-return \loophp\collection\Collection<int, bool>
*/
Expand Down
12 changes: 5 additions & 7 deletions src/Operation/Falsy.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,24 @@ final class Falsy extends AbstractOperation
*/
public function __invoke(): Closure
{
$mapCallback =
$matchCallback =
/**
* @param mixed $value
* @psalm-param T $value
*/
static fn ($value): bool => !(bool) $value;
static fn ($value): bool => (bool) $value;

$dropWhileCallback =
$mapCallback =
/**
* @param mixed $value
* @psalm-param T $value
*/
static fn ($value): bool => true === $value;
static fn ($value): bool => !(bool) $value;

/** @psalm-var Closure(Iterator<TKey, T>): Generator<int, bool> $pipe */
$pipe = Pipe::of()(
Match::of()(static fn () => true)($matchCallback),
Map::of()($mapCallback),
DropWhile::of()($dropWhileCallback),
Append::of()(true),
Head::of()
);

// Point free style.
Expand Down
31 changes: 8 additions & 23 deletions src/Operation/Has.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,44 +16,29 @@
final class Has extends AbstractOperation
{
/**
* @psalm-return Closure(callable(TKey, T): T): Closure(Iterator<TKey, T>): Generator<int, bool>
* @psalm-return Closure(callable(T, TKey, Iterator<TKey, T>): T): Closure(Iterator<TKey, T>): Generator<int, bool>
*/
public function __invoke(): Closure
{
return
/**
* @psalm-param callable(TKey, T): T $callback
* @psalm-param callable(T, TKey, Iterator<TKey, T>): T $callback
*
* @psalm-return Closure(Iterator<TKey, T>): Generator<int, bool>
*/
static function (callable $callback): Closure {
$mapCallback =
$matcher =
/**
* @param mixed $value
* @psalm-param T $value
*
* @param mixed $key
* @psalm-param TKey $key
* @psalm-return T
*/
static fn ($value, $key): bool => $callback($key, $value) === $value;
static fn ($value) => $value;

$dropWhileCallback =
/**
* @param mixed $value
* @psalm-param T $value
*/
static fn ($value): bool => false === $value;

/** @psalm-var Closure(Iterator<TKey, T>): Generator<int, bool> $pipe */
$pipe = Pipe::of()(
Map::of()($mapCallback),
DropWhile::of()($dropWhileCallback),
Append::of()(false),
Head::of()
);
/** @psalm-var Closure(Iterator<TKey, T>): Generator<int, bool> $match */
$match = Match::of()($matcher)($callback);

// Point free style.
return $pipe;
return $match;
};
}
}
13 changes: 2 additions & 11 deletions src/Operation/Nullsy.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,19 +29,10 @@ public function __invoke(): Closure
*/
static fn ($value): bool => in_array($value, [null, [], 0, false, ''], true);

$dropWhileCallback =
/**
* @param mixed $value
* @psalm-param T $value
*/
static fn ($value): bool => true === $value;

/** @psalm-var Closure(Iterator<TKey, T>): Generator<int, bool> $pipe */
$pipe = Pipe::of()(
Map::of()($mapCallback),
DropWhile::of()($dropWhileCallback),
Append::of()(true),
Head::of()
Match::of()(static fn () => false)($mapCallback),
Map::of()(static fn ($value) => !$value),
);

// Point free style.
Expand Down
12 changes: 5 additions & 7 deletions src/Operation/Truthy.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,26 +20,24 @@ final class Truthy extends AbstractOperation
*/
public function __invoke(): Closure
{
$mapCallback =
$matchCallback =
/**
* @param mixed $value
* @psalm-param T $value
*/
static fn ($value): bool => (bool) $value;
static fn ($value): bool => !(bool) $value;

$dropWhileCallback =
$mapCallback =
/**
* @param mixed $value
* @psalm-param T $value
*/
static fn ($value): bool => true === $value;
static fn ($value): bool => !(bool) $value;

/** @psalm-var Closure(Iterator<TKey, T>): Generator<int, bool> $pipe */
$pipe = Pipe::of()(
Match::of()(static fn () => true)($matchCallback),
Map::of()($mapCallback),
DropWhile::of()($dropWhileCallback),
Append::of()(true),
Head::of()
);

// Point free style.
Expand Down

0 comments on commit f676c19

Please sign in to comment.