Skip to content

Commit

Permalink
fix: prevent wrapping and unwrapping arrays when it's not needed
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Dec 15, 2022
1 parent 92f778c commit 937740d
Show file tree
Hide file tree
Showing 19 changed files with 49 additions and 49 deletions.
20 changes: 10 additions & 10 deletions src/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public function all(bool $normalize = true): array

public function append(mixed ...$items): CollectionInterface
{
return new self((new Operation\Append())()(...$items), [$this]);
return new self((new Operation\Append())()($items), [$this]);
}

public function apply(callable ...$callbacks): CollectionInterface
Expand Down Expand Up @@ -119,12 +119,12 @@ public function combinate(?int $length = null): CollectionInterface

public function combine(mixed ...$keys): CollectionInterface
{
return new self((new Operation\Combine())()(...$keys), [$this]);
return new self((new Operation\Combine())()($keys), [$this]);
}

public function compact(mixed ...$values): CollectionInterface
{
return new self((new Operation\Compact())()(...$values), [$this]);
return new self((new Operation\Compact())()($values), [$this]);
}

public function compare(callable $comparator, $default = null): mixed
Expand All @@ -134,7 +134,7 @@ public function compare(callable $comparator, $default = null): mixed

public function contains(mixed ...$values): bool
{
return (new Operation\Contains())()(...$values)($this)->current();
return (new Operation\Contains())()($values)($this)->current();
}

public function count(): int
Expand All @@ -154,12 +154,12 @@ public function cycle(): CollectionInterface

public function diff(mixed ...$values): CollectionInterface
{
return new self((new Operation\Diff())()(...$values), [$this]);
return new self((new Operation\Diff())()($values), [$this]);
}

public function diffKeys(mixed ...$keys): CollectionInterface
{
return new self((new Operation\DiffKeys())()(...$keys), [$this]);
return new self((new Operation\DiffKeys())()($keys), [$this]);
}

public function distinct(?callable $comparatorCallback = null, ?callable $accessorCallback = null): CollectionInterface
Expand Down Expand Up @@ -250,7 +250,7 @@ public function every(callable ...$callbacks): bool

public function explode(mixed ...$explodes): CollectionInterface
{
return new self((new Operation\Explode())()(...$explodes), [$this]);
return new self((new Operation\Explode())()($explodes), [$this]);
}

public function falsy(): bool
Expand Down Expand Up @@ -449,12 +449,12 @@ public function inits(): CollectionInterface

public function intersect(mixed ...$values): CollectionInterface
{
return new self((new Operation\Intersect())()(...$values), [$this]);
return new self((new Operation\Intersect())()($values), [$this]);
}

public function intersectKeys(mixed ...$keys): CollectionInterface
{
return new self((new Operation\IntersectKeys())()(...$keys), [$this]);
return new self((new Operation\IntersectKeys())()($keys), [$this]);
}

public function intersperse(mixed $element, int $every = 1, int $startAt = 0): CollectionInterface
Expand Down Expand Up @@ -595,7 +595,7 @@ public function pluck(mixed $pluck, mixed $default = null): CollectionInterface

public function prepend(mixed ...$items): CollectionInterface
{
return new self((new Operation\Prepend())()(...$items), [$this]);
return new self((new Operation\Prepend())()($items), [$this]);
}

public function product(iterable ...$iterables): CollectionInterface
Expand Down
6 changes: 3 additions & 3 deletions src/Operation/Append.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@
final class Append extends AbstractOperation
{
/**
* @return Closure(T...): Closure(iterable<TKey, T>): Generator<int|TKey, T>
* @return Closure(array<T>): Closure(iterable<TKey, T>): Generator<int|TKey, T>
*/
public function __invoke(): Closure
{
return
/**
* @param T ...$items
* @param array<T> $items
*
* @return Closure(iterable<TKey, T>): Generator<int|TKey, T>
*/
static fn (mixed ...$items): Closure =>
static fn (array $items): Closure =>
/**
* @param iterable<TKey, T> $iterable
*
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Coalesce.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ public function __invoke(): Closure
{
/** @var Closure(iterable<TKey, T>): Generator<TKey, T> $pipe */
$pipe = (new Pipe())()(
(new Compact())()(),
(new Compact())()([]),
(new Head())(),
);

Expand Down
6 changes: 3 additions & 3 deletions src/Operation/Combine.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,17 +20,17 @@ final class Combine extends AbstractOperation
/**
* @template U
*
* @return Closure(U...): Closure(iterable<TKey, T>): Generator<null|U, null|T>
* @return Closure(array<U>): Closure(iterable<TKey, T>): Generator<null|U, null|T>
*/
public function __invoke(): Closure
{
return
/**
* @param U ...$keys
* @param array<U> $keys
*
* @return Closure(iterable<TKey, T>): Generator<null|U, null|T>
*/
static function (mixed ...$keys): Closure {
static function (array $keys): Closure {
$buildMultipleIterable =
/**
* @param iterable<TKey, T> $iterable
Expand Down
6 changes: 3 additions & 3 deletions src/Operation/Compact.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@
final class Compact extends AbstractOperation
{
/**
* @return Closure(T...): Closure(iterable<TKey, T>): Generator<TKey, T>
* @return Closure(array<T>): Closure(iterable<TKey, T>): Generator<TKey, T>
*/
public function __invoke(): Closure
{
return
/**
* @param T ...$values
* @param array<T> $values
*
* @return Closure(iterable<TKey, T>): Generator<TKey, T>
*/
static function (mixed ...$values): Closure {
static function (array $values): Closure {
$filterCallback =
/**
* @param non-empty-array<int, (null|array|int|bool|string|T)> $values
Expand Down
6 changes: 3 additions & 3 deletions src/Operation/Contains.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@
final class Contains extends AbstractOperation
{
/**
* @return Closure(T ...$values): Closure(iterable<TKey, T>): Generator<TKey, bool>
* @return Closure(array<T>): Closure(iterable<TKey, T>): Generator<TKey, bool>
*/
public function __invoke(): Closure
{
return
/**
* @param T ...$values
* @param array<T> $values
*
* @return Closure(iterable<TKey, T>): Generator<TKey, bool>
*/
static fn (mixed ...$values): Closure => (new MatchOne())()(static fn (): bool => true)(
static fn (array $values): Closure => (new MatchOne())()(static fn (): bool => true)(
/**
* @param T $value
*/
Expand Down
6 changes: 3 additions & 3 deletions src/Operation/Diff.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@
final class Diff extends AbstractOperation
{
/**
* @return Closure(T...): Closure(iterable<TKey, T>): Generator<TKey, T>
* @return Closure(array<T>): Closure(iterable<TKey, T>): Generator<TKey, T>
*/
public function __invoke(): Closure
{
return
/**
* @param T ...$values
* @param array<T> $values
*
* @return Closure(iterable<TKey, T>): Generator<TKey, T>
*/
static fn (mixed ...$values): Closure => (new Filter())()(
static fn (array $values): Closure => (new Filter())()(
/**
* @param T $value
*/
Expand Down
6 changes: 3 additions & 3 deletions src/Operation/DiffKeys.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@
final class DiffKeys extends AbstractOperation
{
/**
* @return Closure(TKey...): Closure(iterable<TKey, T>): Generator<TKey, T>
* @return Closure(array<TKey>): Closure(iterable<TKey, T>): Generator<TKey, T>
*/
public function __invoke(): Closure
{
return
/**
* @param TKey ...$keys
* @param array<TKey> $keys
*
* @return Closure(iterable<TKey, T>): Generator<TKey, T>
*/
static fn (mixed ...$keys): Closure => (new Filter())()(
static fn (array $keys): Closure => (new Filter())()(
/**
* @param T $value
* @param TKey $key
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Equals.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ static function (iterable $iterable) use ($other): Generator {
return yield false;
}

$containsCallback = static fn (int $index, mixed $current): bool => (new Contains())()($current)($otherAggregate)->current();
$containsCallback = static fn (int $index, mixed $current): bool => (new Contains())()([$current])($otherAggregate)->current();

yield from (new Every())()($containsCallback)($iteratorAggregate);
};
Expand Down
6 changes: 3 additions & 3 deletions src/Operation/Explode.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@
final class Explode extends AbstractOperation
{
/**
* @return Closure(T...): Closure(iterable<TKey, T>): Generator<int, list<T>>
* @return Closure(array<T>): Closure(iterable<TKey, T>): Generator<int, list<T>>
*/
public function __invoke(): Closure
{
return
/**
* @param T ...$explodes
* @param array<T> $explodes
*
* @return Closure(iterable<TKey, T>): Generator<int, list<T>>
*/
static function (mixed ...$explodes): Closure {
static function (array $explodes): Closure {
/** @var Closure(iterable<TKey, T>): Generator<int, list<T>> $split */
$split = (new Split())()(Splitable::REMOVE)(
...array_map(
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Find.php
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ static function (callable ...$callbacks) use ($default): Closure {
/** @var Closure(iterable<TKey, T>): Generator<TKey, T|V> $pipe */
$pipe = (new Pipe())()(
(new Filter())()(...$callbacks),
(new Append())()($default),
(new Append())()([$default]),
(new Head())(),
);

Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Get.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ static function (mixed $default) use ($keyToGet): Closure {
*/
static fn (mixed $value, mixed $key): bool => $key === $keyToGet
),
(new Append())()($default),
(new Append())()([$default]),
(new Head())()
);

Expand Down
6 changes: 3 additions & 3 deletions src/Operation/Intersect.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@
final class Intersect extends AbstractOperation
{
/**
* @return Closure(T...): Closure(iterable<TKey, T>): Generator<TKey, T>
* @return Closure(array<T>): Closure(iterable<TKey, T>): Generator<TKey, T>
*/
public function __invoke(): Closure
{
return
/**
* @param T ...$values
* @param array<T> $values
*
* @return Closure(iterable<TKey, T>): Generator<TKey, T>
*/
static fn (mixed ...$values): Closure => (new Filter())()(
static fn (array $values): Closure => (new Filter())()(
/**
* @param T $value
*/
Expand Down
6 changes: 3 additions & 3 deletions src/Operation/IntersectKeys.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@
final class IntersectKeys extends AbstractOperation
{
/**
* @return Closure(TKey...): Closure(iterable<TKey, T>): Generator<TKey, T>
* @return Closure(array<TKey>): Closure(iterable<TKey, T>): Generator<TKey, T>
*/
public function __invoke(): Closure
{
return
/**
* @param TKey ...$keys
* @param array<TKey> $keys
*
* @return Closure(iterable<TKey, T>): Generator<TKey, T>
*/
static fn (mixed ...$keys): Closure => (new Filter())()(
static fn (array $keys): Closure => (new Filter())()(
/**
* @param T $value
* @param TKey $key
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/Lines.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public function __invoke(): Closure
{
/** @var Closure(iterable<TKey, T>): Generator<int, string> $pipe */
$pipe = (new Pipe())()(
(new Explode())()(PHP_EOL, "\n", "\r\n"),
(new Explode())()([PHP_EOL, "\n", "\r\n"]),
(new Map())()(
/**
* @param list<T> $value
Expand Down
6 changes: 3 additions & 3 deletions src/Operation/Prepend.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,17 +17,17 @@
final class Prepend extends AbstractOperation
{
/**
* @return Closure(T...): Closure(iterable<TKey, T>): iterable<int|TKey, T>
* @return Closure(array<T>): Closure(iterable<TKey, T>): iterable<int|TKey, T>
*/
public function __invoke(): Closure
{
return
/**
* @param T ...$items
* @param array<T> $items
*
* @return Closure(iterable<TKey, T>): iterable<int|TKey, T>
*/
static fn (mixed ...$items): Closure =>
static fn (array $items): Closure =>
/**
* @param iterable<TKey, T> $iterable
*
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/ScanLeft.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ static function (mixed $initial) use ($callback): Closure {
/** @var Closure(iterable<TKey, T>): Generator<TKey, V|W> $pipe */
$pipe = (new Pipe())()(
(new Reduction())()($callback)($initial),
(new Prepend())()($initial)
(new Prepend())()([$initial])
);

return $pipe;
Expand Down
2 changes: 1 addition & 1 deletion src/Operation/ScanLeft1.php
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ static function (iterable $iterable) use ($callback): Generator {
$pipe = (new Pipe())()(
(new Tail())(),
(new Reduction())()($callback)($initial),
(new Prepend())()($initial)
(new Prepend())()([$initial])
);

yield from $pipe($iteratorAggregate);
Expand Down
4 changes: 2 additions & 2 deletions src/Operation/Words.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,14 @@ public function __invoke(): Closure
{
/** @var Closure(iterable<TKey, T>): Generator<TKey, string> $pipe */
$pipe = (new Pipe())()(
(new Explode())()("\t", "\n", ' '),
(new Explode())()(["\t", "\n", ' ']),
(new Map())()(
/**
* @param list<string> $value
*/
static fn (array $value): string => implode('', $value)
),
(new Compact())()()
(new Compact())()([])
);

// Point free style.
Expand Down

0 comments on commit 937740d

Please sign in to comment.