Skip to content

Commit

Permalink
Update First operation.
Browse files Browse the repository at this point in the history
Make it simpler to use.
  • Loading branch information
drupol committed Sep 7, 2020
1 parent f20942c commit 79a1f5c
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 77 deletions.
40 changes: 0 additions & 40 deletions spec/loophp/collection/CollectionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -836,49 +836,9 @@ public function it_can_get_the_first_item(): void
->first()
->shouldIterateAs([0 => 1]);

$this::fromIterable(range(1, 10))
->first(
static function ($value) {
return 0 === $value % 5;
}
)
->shouldIterateAs([4 => 5]);

$this::fromIterable([])
->first()
->shouldIterateAs([]);

$generator = static function (): Generator {
yield 'a' => 'a';

yield 'b' => 'b';

yield 'c' => 'c';

yield 'a' => 'd';

yield 'b' => 'e';

yield 'c' => 'f';
};

$this::fromIterable($generator())
->first(static function ($value, $key) {
return 'b' === $key;
})
->shouldIterateAs(['b' => 'b']);

$output = static function (): Generator {
yield 'b' => 'b';

yield 'b' => 'e';
};

$this::fromIterable($generator())
->first(static function ($value, $key) {
return 'b' === $key;
}, 2)
->shouldIterateAs($output());
}

public function it_can_get_the_last_item(): void
Expand Down
4 changes: 2 additions & 2 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -344,9 +344,9 @@ public function filter(callable ...$callbacks): CollectionInterface
return $this->run(Filter::of()(...$callbacks));
}

public function first(?callable $callback = null, int $size = 1): CollectionInterface
public function first(): CollectionInterface
{
return $this->run(First::of()($callback)($size));
return $this->run(First::of());
}

public function flatten(int $depth = PHP_INT_MAX): CollectionInterface
Expand Down
6 changes: 2 additions & 4 deletions src/Contract/Operation/Firstable.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,11 +14,9 @@
interface Firstable
{
/**
* Get the first item from the collection passing the given truth test.
*
* @psalm-param null|callable(T, TKey):(bool) $callback
* Get the first item from the collection.
*
* @psalm-return \loophp\collection\Contract\Collection<TKey, T>
*/
public function first(?callable $callback = null, int $size = 1): Collection;
public function first(): Collection;
}
37 changes: 6 additions & 31 deletions src/Operation/First.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,37 +17,12 @@ final class First extends AbstractOperation
{
public function __invoke(): Closure
{
return
/**
* @psalm-param callable(T, TKey):(bool)|null $callback
*/
static function (?callable $callback = null): Closure {
return static function (int $size = 1) use ($callback): Closure {
return
/**
* @psalm-param Iterator<TKey, T> $iterator
*/
static function (Iterator $iterator) use ($callback, $size): Generator {
$defaultCallback =
/**
* @param mixed $value
* @param mixed $key
* @psalm-param T $value
* @psalm-param TKey $key
* @psalm-param Iterator<TKey, T> $iterator
*/
static function ($value, $key, Iterator $iterator): bool {
return true;
};
return static function (Iterator $iterator): Generator {
if (!$iterator->valid()) {
return yield from [];
}

$callback = $callback ?? $defaultCallback;

return yield from Compose::of()(
Filter::of()($callback),
Limit::of()($size)(0)
)($iterator);
};
};
};
return yield $iterator->key() => $iterator->current();
};
}
}

0 comments on commit 79a1f5c

Please sign in to comment.