Skip to content

Commit

Permalink
refactor: Minor code style update.
Browse files Browse the repository at this point in the history
  • Loading branch information
drupol committed Dec 15, 2020
1 parent 65769b4 commit 54d0f8e
Show file tree
Hide file tree
Showing 11 changed files with 69 additions and 70 deletions.
8 changes: 2 additions & 6 deletions src/Operation/Append.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,9 @@ public function __invoke(): Closure
* @psalm-return Generator<int|TKey, T>
*/
static function (Iterator $iterator) use ($items): Generator {
foreach ($iterator as $key => $value) {
yield $key => $value;
}
yield from $iterator;

foreach ($items as $key => $item) {
yield $key => $item;
}
return yield from $items;
};
}
}
8 changes: 1 addition & 7 deletions src/Operation/Collapse.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,7 @@ static function (Iterator $iterator): Generator {
continue;
}

/**
* @psalm-var TKey $subKey
* @psalm-var T $subValue
*/
foreach ($value as $subKey => $subValue) {
yield $subKey => $subValue;
}
yield from $value;
}
};
}
Expand Down
8 changes: 1 addition & 7 deletions src/Operation/Flatten.php
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,7 @@ static function (Iterator $iterator) use ($depth): Generator {
$value = $flatten(new IterableIterator($value));
}

/**
* @psalm-var TKey $subKey
* @psalm-var T $subValue
*/
foreach ($value as $subKey => $subValue) {
yield $subKey => $subValue;
}
yield from $value;
}
};
}
Expand Down
21 changes: 10 additions & 11 deletions src/Operation/Flip.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,15 @@ final class Flip extends AbstractOperation
*/
public function __invoke(): Closure
{
return
/**
* @psalm-param Iterator<TKey, T> $iterator
*
* @psalm-return Generator<T, TKey>
*/
static function (Iterator $iterator): Generator {
foreach ($iterator as $key => $value) {
yield $value => $key;
}
};
$callbackForKeys = static fn ($carry, $key, $value) => $value;
$callbackForValues = static fn ($carry, $key, $value) => $key;

/**
* @var Closure(Iterator<TKey, T>): Generator<T, TKey>
*/
$associate = Associate::of()($callbackForKeys)($callbackForValues);

// Point free style.
return $associate;
}
}
19 changes: 8 additions & 11 deletions src/Operation/Keys.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,13 @@ final class Keys extends AbstractOperation
*/
public function __invoke(): Closure
{
return
/**
* @psalm-param Iterator<TKey, T> $iterator
*
* @psalm-return Generator<int, TKey>
*/
static function (Iterator $iterator): Generator {
foreach ($iterator as $key => $value) {
yield $key;
}
};
/** @psalm-var Closure(Iterator<TKey, T>): Generator<int, TKey> $pipe */
$pipe = Pipe::of()(
Flip::of(),
Normalize::of()
);

// Point free style.
return $pipe;
}
}
8 changes: 2 additions & 6 deletions src/Operation/Merge.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,14 +31,10 @@ public function __invoke(): Closure
* @psalm-return Generator<TKey, T>
*/
static function (Iterator $iterator) use ($sources): Generator {
foreach ($iterator as $key => $value) {
yield $key => $value;
}
yield from $iterator;

foreach ($sources as $source) {
foreach ($source as $key => $value) {
yield $key => $value;
}
yield from $source;
}
};
}
Expand Down
8 changes: 2 additions & 6 deletions src/Operation/Prepend.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,9 @@ public function __invoke(): Closure
* @psalm-return Generator<int|TKey, T>
*/
static function (Iterator $iterator) use ($items): Generator {
foreach ($items as $key => $item) {
yield $key => $item;
}
yield from $items;

foreach ($iterator as $key => $value) {
yield $key => $value;
}
return yield from $iterator;
};
}
}
27 changes: 22 additions & 5 deletions src/Operation/Transpose.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,13 +31,30 @@ public function __invoke(): Closure
static function (Iterator $iterator): Generator {
$mit = new MultipleIterator(MultipleIterator::MIT_NEED_ANY);

foreach ($iterator as $collectionItem) {
$mit->attachIterator(new IterableIterator($collectionItem));
foreach ($iterator as $iterableIterator) {
$mit->attachIterator(new IterableIterator($iterableIterator));
}

foreach ($mit as $key => $value) {
yield current($key) => $value;
}
$callbackForKeys =
/**
* @psalm-param array $carry
* @psalm-param array<int, TKey> $key
*
* @psalm-return TKey
*/
static fn (array $carry, array $key) => current($key);

$callbackForValues =
/**
* @psalm-param array $carry
* @psalm-param array<int, TKey> $key
* @psalm-param array<int, T> $value
*
* @psalm-return array<int, T>
*/
static fn (array $carry, array $key, array $value): array => $value;

return yield from Associate::of()($callbackForKeys)($callbackForValues)($mit);
};
}
}
1 change: 1 addition & 0 deletions src/Operation/Unzip.php
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ static function (array $carry, iterable $value): array {
Unwrap::of()
);

// Point free style.
return $pipe;
}
}
4 changes: 3 additions & 1 deletion src/Operation/Wrap.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,11 @@ public function __invoke(): Closure
$mapCallback =
/**
* @psalm-param T $value
* @psalm-param TKey $key
*
* @param mixed $value
*
* @psalm-param TKey $key
*
* @param mixed $key
*
* @psalm-return array<TKey, T>
Expand Down
27 changes: 17 additions & 10 deletions src/Operation/Zip.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,24 @@ public function __invoke(): Closure
*
* @psalm-return Closure(Iterator<TKey, T>): Generator<int, list<T>>
*/
static fn (iterable ...$iterables): Closure => static function (Iterator $iterator) use ($iterables): Generator {
$mit = new MultipleIterator(MultipleIterator::MIT_NEED_ANY);
$mit->attachIterator($iterator);
static fn (iterable ...$iterables): Closure =>
/**
* @psalm-param Iterator<TKey, T>: Generator<int, list<T>>
*
* @psalm-return Generator<int, list<T>>
*/
static function (Iterator $iterator) use ($iterables): Generator {
$mit = new MultipleIterator(MultipleIterator::MIT_NEED_ANY);
$mit->attachIterator($iterator);

foreach ($iterables as $iterableIterator) {
$mit->attachIterator(new IterableIterator($iterableIterator));
}
foreach ($iterables as $iterableIterator) {
$mit->attachIterator(new IterableIterator($iterableIterator));
}

foreach ($mit as $values) {
yield $values;
}
};
// @todo: Why "yield from $mit" doesn't work here?
foreach ($mit as $values) {
yield $values;
}
};
}
}

0 comments on commit 54d0f8e

Please sign in to comment.