Skip to content

Commit

Permalink
refactor: Leverage tacit programming (point free style)
Browse files Browse the repository at this point in the history
Point free style operation to avoid repeating closures that are already
existing in Compose operation.
  • Loading branch information
drupol committed Sep 23, 2020
1 parent 855aa63 commit 764a3c6
Show file tree
Hide file tree
Showing 32 changed files with 521 additions and 542 deletions.
17 changes: 4 additions & 13 deletions spec/loophp/collection/CollectionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -1392,24 +1392,15 @@ public function it_can_nullsy(): void

public function it_can_pack(): void
{
$input = array_combine(range('a', 'c'), range('a', 'c'));
$input = array_combine(range('A', 'C'), range('a', 'c'));

$this::fromIterable($input)
->pack()
->shouldIterateAs(
[
0 => [
0 => 'a',
1 => 'a',
],
1 => [
0 => 'b',
1 => 'b',
],
2 => [
0 => 'c',
1 => 'c',
],
['A', 'a'],
['B', 'b'],
['C', 'c'],
]
);
}
Expand Down
47 changes: 30 additions & 17 deletions src/Operation/Column.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,36 +16,49 @@
final class Column extends AbstractOperation
{
/**
* @psalm-return Closure(array-key): Closure(Iterator<TKey, T>): Generator<int, iterable<TKey, T>>
* @psalm-return Closure(T): Closure(Iterator<TKey, T>): Generator<int, iterable<TKey, T>>
*/
public function __invoke(): Closure
{
return
/**
* @param int|string $column
*
* @psalm-param array-key $column
* @param mixed $column
* @psalm-param T $column
*
* @psalm-return Closure(Iterator<TKey, T>): Generator<int, iterable<TKey, T>>
*/
static function ($column): Closure {
return
$filterCallbackBuilder =
/**
* @psalm-param Iterator<TKey, T> $iterator
* @psalm-param T $column
*
* @psalm-return Generator<int, iterable<TKey, T>>
* @param mixed $column
*/
static function (Iterator $iterator) use ($column): Generator {
/**
* @psalm-var array-key $key
* @psalm-var iterable<TKey, T> $value
*/
foreach (Transpose::of()($iterator) as $key => $value) {
if ($key === $column) {
return yield from $value;
}
}
static function ($column): Closure {
return
/**
* @psalm-param T $value
* @psalm-param TKey $key
* @psalm-param Iterator<TKey, T> $iterator
*
* @param mixed $value
* @param mixed $key
*/
static function ($value, $key, Iterator $iterator) use ($column): bool {
return $key === $column;
};
};

/** @psalm-var Closure(Iterator<TKey, T>): Generator<int, iterable<TKey, T>> $compose */
$compose = Compose::of()(
Transpose::of(),
Filter::of()($filterCallbackBuilder($column)),
First::of(),
Unwrap::of()
);

// Point free style.
return $compose;
};
}
}
25 changes: 9 additions & 16 deletions src/Operation/Compact.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,24 +29,17 @@ public function __invoke(): Closure
* @psalm-return Closure(Iterator<TKey, T>): Generator<TKey, T>
*/
static function (...$values): Closure {
return
/**
* @psalm-param Iterator<TKey, T> $iterator
*
* @psalm-return Generator<TKey, T>
*/
static function (Iterator $iterator) use ($values): Generator {
$filterCallback = static function (array $values): Closure {
return static function ($value) use ($values): bool {
return !in_array($value, $values, true);
};
};
$filterCallback = static function (array $values): Closure {
return static function ($value) use ($values): bool {
return !in_array($value, $values, true);
};
};

/** @psalm-var callable(Iterator<TKey, T>):Generator<TKey, T> $filter */
$filter = Filter::of()($filterCallback([] === $values ? [null, [], 0, false, ''] : $values));
/** @psalm-var Closure(Iterator<TKey, T>):Generator<TKey, T> $filter */
$filter = Filter::of()($filterCallback([] === $values ? [null, [], 0, false, ''] : $values));

return yield from $filter($iterator);
};
// Point free style.
return $filter;
};
}
}
19 changes: 6 additions & 13 deletions src/Operation/Current.php
Original file line number Diff line number Diff line change
Expand Up @@ -16,29 +16,22 @@
final class Current extends AbstractOperation
{
/**
* @psalm-return Closure(int): Closure(Iterator<TKey, T>): Generator<int, T>
* @psalm-return Closure(int): Closure(Iterator<TKey, T>): Generator<TKey, T>
*/
public function __invoke(): Closure
{
return
/**
* @psalm-param int $index
*
* @psalm-return Closure(Iterator<TKey, T>): Generator<int, T>
* @psalm-return Closure(Iterator<TKey, T>): Generator<TKey, T>
*/
static function (int $index): Closure {
return
/**
* @psalm-param Iterator<TKey, T> $iterator
*
* @psalm-return Generator<int, T>
*/
static function (Iterator $iterator) use ($index): Generator {
for ($i = 0; $i < $index; $i++, $iterator->next()) {
}
/** @psalm-var Closure(Iterator<TKey, T>): Generator<TKey, T> $limit */
$limit = Limit::of()(1)($index);

return yield $iterator->current();
};
// Point free style.
return $limit;
};
}
}
41 changes: 17 additions & 24 deletions src/Operation/Diff.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,26 @@ public function __invoke(): Closure
* @psalm-return Closure(Iterator<TKey, T>): Generator<TKey, T>
*/
static function (...$values): Closure {
return
/**
* @psalm-param Iterator<TKey, T> $iterator
*
* @psalm-return Generator<TKey, T>
*/
static function (Iterator $iterator) use ($values): Generator {
$filterCallbackFactory = static function (array $values): Closure {
return
/**
* @psalm-param T $value
* @psalm-param TKey $key
* @psalm-param Iterator<TKey, T> $iterator
*
* @param mixed $value
* @param mixed $key
*/
static function ($value, $key, Iterator $iterator) use ($values): bool {
return false === in_array($value, $values, true);
};
$filterCallbackFactory = static function (array $values): Closure {
return
/**
* @psalm-param T $value
* @psalm-param TKey $key
* @psalm-param Iterator<TKey, T> $iterator
*
* @param mixed $value
* @param mixed $key
*/
static function ($value, $key, Iterator $iterator) use ($values): bool {
return false === in_array($value, $values, true);
};
};

/** @psalm-var callable(Iterator<TKey, T>): Generator<TKey, T> $filter */
$filter = Filter::of()($filterCallbackFactory($values));
/** @psalm-var Closure(Iterator<TKey, T>): Generator<TKey, T> $filter */
$filter = Filter::of()($filterCallbackFactory($values));

return $filter($iterator);
};
// Point free style.
return $filter;
};
}
}
43 changes: 18 additions & 25 deletions src/Operation/DiffKeys.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,38 +24,31 @@ public function __invoke(): Closure
{
return
/**
* @psalm-param T ...$values
* @psalm-param TKey ...$values
*
* @psalm-return Closure(Iterator<TKey, T>): Generator<TKey, T>
*/
static function (...$values): Closure {
return
/**
* @psalm-param Iterator<TKey, T> $iterator
*
* @psalm-return Generator<TKey, T>
*/
static function (Iterator $iterator) use ($values): Generator {
$filterCallbackFactory = static function (array $values): Closure {
return
/**
* @psalm-param T $value
* @psalm-param TKey $key
* @psalm-param Iterator<TKey, T> $iterator
*
* @param mixed $value
* @param mixed $key
*/
static function ($value, $key, Iterator $iterator) use ($values): bool {
return false === in_array($key, $values, true);
};
$filterCallbackFactory = static function (array $values): Closure {
return
/**
* @psalm-param T $value
* @psalm-param TKey $key
* @psalm-param Iterator<TKey, T> $iterator
*
* @param mixed $value
* @param mixed $key
*/
static function ($value, $key, Iterator $iterator) use ($values): bool {
return false === in_array($key, $values, true);
};
};

/** @psalm-var callable(Iterator<TKey, T>): Generator<TKey, T> $filter */
$filter = Filter::of()($filterCallbackFactory($values));
/** @psalm-var Closure(Iterator<TKey, T>): Generator<TKey, T> $filter */
$filter = Filter::of()($filterCallbackFactory($values));

return $filter($iterator);
};
// Point free style.
return $filter;
};
}
}
4 changes: 4 additions & 0 deletions src/Operation/Drop.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,10 @@ static function (int ...$offsets): Closure {
* @psalm-return Generator<TKey, T>
*/
static function (Iterator $iterator) use ($offsets): Generator {
if (!$iterator->valid()) {
return yield from [];
}

return yield from new LimitIterator($iterator, (int) array_sum($offsets));
};
};
Expand Down
1 change: 1 addition & 0 deletions src/Operation/Explode.php
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ static function ($value) use ($explode): bool {
)
);

// Point free style.
return $split;
};
}
Expand Down
41 changes: 17 additions & 24 deletions src/Operation/Forget.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,33 +29,26 @@ public function __invoke(): Closure
* @psalm-return Closure(Iterator<TKey, T>): Generator<TKey, T>
*/
static function (...$keys): Closure {
return
/**
* @psalm-param Iterator<TKey, T> $iterator
*
* @psalm-return Generator<TKey, T>
*/
static function (Iterator $iterator) use ($keys): Generator {
$filterCallbackFactory = static function (array $keys): Closure {
return
/**
* @psalm-param T $value
* @psalm-param TKey $key
* @psalm-param Iterator<TKey, T> $iterator
*
* @param mixed $value
* @param mixed $key
*/
static function ($value, $key, Iterator $iterator) use ($keys): bool {
return false === in_array($key, $keys, true);
};
$filterCallbackFactory = static function (array $keys): Closure {
return
/**
* @psalm-param T $value
* @psalm-param TKey $key
* @psalm-param Iterator<TKey, T> $iterator
*
* @param mixed $value
* @param mixed $key
*/
static function ($value, $key, Iterator $iterator) use ($keys): bool {
return false === in_array($key, $keys, true);
};
};

/** @psalm-var callable(Iterator<TKey, T>): Generator<TKey, T> $filter */
$filter = Filter::of()($filterCallbackFactory($keys));
/** @psalm-var Closure(Iterator<TKey, T>): Generator<TKey, T> $filter */
$filter = Filter::of()($filterCallbackFactory($keys));

return $filter($iterator);
};
// Point free style.
return $filter;
};
}
}

0 comments on commit 764a3c6

Please sign in to comment.