Skip to content

Commit

Permalink
refactor: Leverage tacit programming.
Browse files Browse the repository at this point in the history
Refactor some operations, in Point Free Style fashion.
  • Loading branch information
drupol committed Oct 8, 2020
1 parent bca5780 commit 69541f6
Show file tree
Hide file tree
Showing 8 changed files with 94 additions and 105 deletions.
2 changes: 1 addition & 1 deletion spec/loophp/collection/CollectionSpec.php
Original file line number Diff line number Diff line change
Expand Up @@ -887,7 +887,7 @@ public function it_can_get(): void
{
$this::fromIterable(range('A', 'E'))
->get(4)
->shouldIterateAs(['E']);
->shouldIterateAs([4 => 'E']);

$this::fromIterable(range('A', 'E'))
->get('unexistent key', 'default')
Expand Down
23 changes: 7 additions & 16 deletions src/Operation/FoldLeft.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,14 @@ static function (callable $callback): Closure {
* @psalm-return Closure(Iterator<TKey, T>): Generator<TKey, T>
*/
static function ($initial = null) use ($callback): Closure {
return
/**
* @psalm-param Iterator<TKey, T> $iterator
*
* @psalm-return Generator<TKey, T>
*/
static function (Iterator $iterator) use ($callback, $initial): Generator {
/** @psalm-var Generator<TKey, T> $iterator */
$iterator = Last::of()(ScanLeft::of()($callback)($initial)($iterator));
/** @psalm-var Closure(Iterator<TKey, T>): Generator<TKey, T> $pipe */
$pipe = Pipe::of()(
ScanLeft::of()($callback)($initial),
Last::of()
);

/** @psalm-var Generator<int, TKey> $key */
$key = (Key::of()(0)($iterator));
/** @psalm-var Generator<int, T> $current */
$current = (Current::of()(0)($iterator));

return yield $key->current() => $current->current();
};
// Point free style.
return $pipe;
};
};
}
Expand Down
23 changes: 7 additions & 16 deletions src/Operation/FoldLeft1.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,23 +29,14 @@ public function __invoke(): Closure
* @psalm-return Closure(Iterator<TKey, T>): Generator<int|TKey, null|T>
*/
static function (callable $callback): Closure {
return
/**
* @psalm-param Iterator<TKey, T> $iterator
*
* @psalm-return Generator<int|TKey, null|T>
*/
static function (Iterator $iterator) use ($callback): Generator {
/** @psalm-var Generator<TKey, T> $iterator */
$iterator = Last::of()(ScanLeft1::of()($callback)($iterator));
/** @psalm-var Closure(Iterator<TKey, T>): Generator<int|TKey, T|null> $pipe */
$pipe = Pipe::of()(
ScanLeft1::of()($callback),
Last::of()
);

/** @psalm-var Generator<int, TKey> $key */
$key = (Key::of()(0)($iterator));
/** @psalm-var Generator<int, T> $current */
$current = (Current::of()(0)($iterator));

return yield $key->current() => $current->current();
};
// Point free style.
return $pipe;
};
}
}
23 changes: 7 additions & 16 deletions src/Operation/FoldRight.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,23 +37,14 @@ static function (callable $callback): Closure {
* @psalm-return Closure(Iterator<TKey, T>): Generator<TKey, T>
*/
static function ($initial = null) use ($callback): Closure {
return
/**
* @psalm-param Iterator<TKey, T> $iterator
*
* @psalm-return Generator<TKey, T>
*/
static function (Iterator $iterator) use ($callback, $initial): Generator {
/** @psalm-var Generator<TKey, T> $iterator */
$iterator = ScanRight::of()($callback)($initial)($iterator);
/** @psalm-var Closure(Iterator<TKey, T>): Generator<TKey, T> $pipe */
$pipe = Pipe::of()(
ScanRight::of()($callback)($initial),
Head::of()
);

/** @psalm-var Generator<int, TKey> $key */
$key = (Key::of()(0)($iterator));
/** @psalm-var Generator<int, T> $current */
$current = (Current::of()(0)($iterator));

return yield $key->current() => $current->current();
};
// Point free style.
return $pipe;
};
};
}
Expand Down
28 changes: 10 additions & 18 deletions src/Operation/FoldRight1.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,34 +18,26 @@
final class FoldRight1 extends AbstractOperation
{
/**
* @psalm-return Closure(callable(T|null, T, TKey, Iterator<TKey, T>):(T|null)): Closure(Iterator<TKey, T>): Generator<TKey, T>
* @psalm-return Closure(callable(T|null, T, TKey, Iterator<TKey, T>):(T|null)): Closure(Iterator<TKey, T>): Generator<int|TKey, T|null>
*/
public function __invoke(): Closure
{
return
/**
* @psalm-param callable(T|null, T, TKey, Iterator<TKey, T>):(T|null) $callback
*
* @psalm-return Closure(Iterator<TKey, T>): Generator<TKey, T>
* @psalm-return Closure(Iterator<TKey, T>): Generator<int|TKey, T|null>
*/
static function (callable $callback): Closure {
return
/**
* @psalm-param Iterator<TKey, T> $iterator
*
* @psalm-return Generator<TKey, T>
*/
static function (Iterator $iterator) use ($callback): Generator {
/** @psalm-var Generator<TKey, T> $iterator */
$iterator = ScanRight1::of()($callback)(Reverse::of()($iterator));
/** @psalm-var Closure(Iterator<TKey, T>): Generator<int|TKey, T|null> $pipe */
$pipe = Pipe::of()(
Reverse::of(),
ScanRight1::of()($callback),
Head::of()
);

/** @psalm-var Generator<int, TKey> $key */
$key = (Key::of()(0)($iterator));
/** @psalm-var Generator<int, T> $current */
$current = (Current::of()(0)($iterator));

return yield $key->current() => $current->current();
};
// Point free style.
return $pipe;
};
}
}
39 changes: 25 additions & 14 deletions src/Operation/Get.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,48 @@
*/
final class Get extends AbstractOperation
{
/**
* @psalm-return Closure(T|TKey): Closure(T): Closure(Iterator<TKey, T>): Generator<int|TKey, T>
*/
public function __invoke(): Closure
{
return
/**
* @psalm-param array-key $keyToGet
*
* @param mixed $keyToGet
* @psalm-param T|TKey $keyToGet
*
* @psalm-return Closure(T): Closure(Iterator<TKey, T>): Generator<int|TKey, T>
*/
static function ($keyToGet): Closure {
return
/**
* @param mixed $default
* @psalm-param T $default
*
* @param mixed $default
* @psalm-return Closure(Iterator<TKey, T>): Generator<int|TKey, T>
*/
static function ($default) use ($keyToGet): Closure {
return
$filterCallback =
/**
* @psalm-param Iterator<TKey, T> $iterator
* @param mixed $value
* @psalm-param T $value
*
* @psalm-return Generator<int, T>
* @param mixed $key
* @psalm-param TKey $key
*/
static function (Iterator $iterator) use ($keyToGet, $default): Generator {
foreach ($iterator as $key => $value) {
if ($key === $keyToGet) {
return yield $value;
}
}

return yield $default;
static function ($value, $key) use ($keyToGet): bool {
return $key === $keyToGet;
};

/** @psalm-var Closure(Iterator<TKey, T>): Generator<int|TKey, T> $pipe */
$pipe = Pipe::of()(
Filter::of()($filterCallback),
Append::of()($default),
Head::of()
);

// Point free style.
return $pipe;
};
};
}
Expand Down
28 changes: 17 additions & 11 deletions src/Operation/Has.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,27 @@ public function __invoke(): Closure
* @psalm-return Closure(Iterator<TKey, T>): Generator<int, bool>
*/
static function (callable $callback): Closure {
return
$mapCallback =
/**
* @psalm-param Iterator<TKey, T> $iterator
* @param mixed $value
* @psalm-param T $value
*
* @psalm-return Generator<int, bool>
* @param mixed $key
* @psalm-param TKey $key
*/
static function (Iterator $iterator) use ($callback): Generator {
foreach ($iterator as $key => $value) {
if ($callback($key, $value) === $value) {
return yield true;
}
}

return yield false;
static function ($value, $key) use ($callback): bool {
return $callback($key, $value) === $value;
};

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

// Point free style.
return $pipe;
};
}
}
33 changes: 20 additions & 13 deletions src/Operation/IfThenElse.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,38 +24,45 @@ public function __invoke(): Closure
{
return
/**
* @psalm-param callable(T, TKey): bool $condition
* @psalm-param callable(T, TKey):bool $condition
*
* @psalm-return Closure(callable(T, TKey): (T)): Closure(callable(T, TKey): (T)): Closure(Iterator<TKey, T>): Generator<TKey, T>
*/
static function (callable $condition): Closure {
return
/**
* @psalm-param callable(T, TKey): (T) $then
* @psalm-param callable(T, TKey):T $then
*
* @psalm-return Closure(callable(T, TKey): (T)): Closure(Iterator<TKey, T>): Generator<TKey, T>
*/
static function (callable $then) use ($condition): Closure {
return
/**
* @psalm-param callable(T, TKey): (T) $else
* @psalm-param callable(T, TKey):T $else
*
* @psalm-return Closure(Iterator<TKey, T>): Generator<TKey, T>
*/
static function (callable $else) use ($condition, $then): Closure {
return
/** @psalm-var Closure(Iterator<TKey, T>): Generator<TKey, T> $map */
$map = Map::of()(
/**
* @psalm-param Iterator<TKey, T> $iterator
* @param mixed $value
* @psalm-param T $value
*
* @psalm-return Generator<TKey, T>
* @param mixed $key
* @psalm-param TKey $key
*
* @psalm-return T
*/
static function (Iterator $iterator) use ($condition, $then, $else): Generator {
foreach ($iterator as $key => $value) {
yield $key => $condition($value, $key) ?
$then($value, $key) :
$else($value, $key);
}
};
static function ($value, $key) use ($condition, $then, $else) {
return $condition($value, $key) ?
$then($value, $key) :
$else($value, $key);
}
);

// Point free style.
return $map;
};
};
};
Expand Down

0 comments on commit 69541f6

Please sign in to comment.