Skip to content

Commit

Permalink
Uses static closures
Browse files Browse the repository at this point in the history
  • Loading branch information
pitchart committed Mar 12, 2020
1 parent 131670d commit 8e9294d
Show file tree
Hide file tree
Showing 5 changed files with 36 additions and 36 deletions.
4 changes: 2 additions & 2 deletions src/Composition.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public function __construct(callable ...$functions)
public function __invoke()
{
if (empty($this->functions)) {
return (function ($value) {
return (static function ($value) {
return $value;
})(func_get_arg(0));
}
Expand All @@ -46,7 +46,7 @@ public function __invoke()

return array_reduce(
$functionList,
function ($carry, $item) {
static function ($carry, $item) {
return $item($carry);
},
call_user_func_array($first, func_get_args())
Expand Down
4 changes: 2 additions & 2 deletions src/Curry.php
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,8 @@ public function __invoke(...$arguments)
{
$function = $this->function;

return function (...$args) use ($function, $arguments) {
return (function ($arguments) use ($function) {
return static function (...$args) use ($function, $arguments) {
return (static function ($arguments) use ($function) {
if (count($arguments) < (new \ReflectionFunction($function))->getNumberOfRequiredParameters()) {
return new self($function, ...$arguments);
}
Expand Down
10 changes: 5 additions & 5 deletions src/Reducer/Termination/Operation.php
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,19 @@ public function __construct(string $operation)
{
if (self::$operators === null) {
self::$operators = [
'+' => function ($result, $current) {
'+' => static function ($result, $current) {
return $result+$current;
},
'-' => function ($result, $current) {
'-' => static function ($result, $current) {
return $result-$current;
},
'*' => function ($result, $current) {
'*' => static function ($result, $current) {
return $result*$current;
},
'/' => function ($result, $current) {
'/' => static function ($result, $current) {
return $result/$current;
},
'.' => function ($result, $current) {
'.' => static function ($result, $current) {
return $result.$current;
},
];
Expand Down
52 changes: 26 additions & 26 deletions src/Transducer/transducers.php
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ function transduce(callable $transducer, Termination $reducer, $iterable, $initi
function map(callable $callback, $sequence = null)
{
if ($sequence === null) {
return function (Reducer $reducer) use ($callback) {
return static function (Reducer $reducer) use ($callback) {
return new Reducer\Map($reducer, $callback);
};
}
Expand All @@ -78,7 +78,7 @@ function map(callable $callback, $sequence = null)
function filter(callable $callback, $sequence = null)
{
if ($sequence === null) {
return function (Reducer $reducer) use ($callback) {
return static function (Reducer $reducer) use ($callback) {
return new Reducer\Filter($reducer, $callback);
};
}
Expand All @@ -99,12 +99,12 @@ function filter(callable $callback, $sequence = null)
function keep(callable $callback, $sequence = null)
{
if ($sequence === null) {
return function (Reducer $reducer) use ($callback) {
return static function (Reducer $reducer) use ($callback) {
return new Reducer\Keep($reducer, $callback);
};
}
if (is_array($sequence)) {
return \array_values(\array_filter($sequence, function ($item) use ($callback) {
return \array_values(\array_filter($sequence, static function ($item) use ($callback) {
return $callback($item) !== null;
}));
}
Expand All @@ -121,14 +121,14 @@ function keep(callable $callback, $sequence = null)
function remove(callable $callback, $sequence = null)
{
if ($sequence === null) {
return function (Reducer $reducer) use ($callback) {
return new Reducer\Filter($reducer, function ($item) use ($callback) {
return static function (Reducer $reducer) use ($callback) {
return new Reducer\Filter($reducer, static function ($item) use ($callback) {
return !($callback($item));
});
};
}
if (is_array($sequence)) {
return \array_values(\array_filter($sequence, function ($item) use ($callback) {
return \array_values(\array_filter($sequence, static function ($item) use ($callback) {
return !$callback($item);
}));
}
Expand All @@ -145,7 +145,7 @@ function remove(callable $callback, $sequence = null)
function first(callable $callback, $sequence = null)
{
if ($sequence === null) {
return function (Reducer $reducer) use ($callback) {
return static function (Reducer $reducer) use ($callback) {
return new Reducer\First($reducer, $callback);
};
}
Expand All @@ -166,7 +166,7 @@ function first(callable $callback, $sequence = null)
function cat($sequence = null)
{
if ($sequence === null) {
return function (Reducer $reducer) {
return static function (Reducer $reducer) {
return new Reducer\Cat($reducer);
};
}
Expand Down Expand Up @@ -196,7 +196,7 @@ function mapcat(callable $callback, $sequence = null)
function flatten($sequence = null)
{
if ($sequence === null) {
return function (Reducer $reducer) {
return static function (Reducer $reducer) {
return new Reducer\Flatten($reducer);
};
}
Expand All @@ -213,7 +213,7 @@ function flatten($sequence = null)
function take(int $number, $sequence = null)
{
if ($sequence === null) {
return function (Reducer $reducer) use ($number) {
return static function (Reducer $reducer) use ($number) {
return new Reducer\Take($reducer, $number);
};
}
Expand All @@ -230,7 +230,7 @@ function take(int $number, $sequence = null)
function take_while(callable $callback, $sequence = null)
{
if ($sequence === null) {
return function (Reducer $reducer) use ($callback) {
return static function (Reducer $reducer) use ($callback) {
return new Reducer\TakeWhile($reducer, $callback);
};
}
Expand All @@ -247,7 +247,7 @@ function take_while(callable $callback, $sequence = null)
function take_nth(int $frequency, $sequence = null)
{
if ($sequence === null) {
return function (Reducer $reducer) use ($frequency) {
return static function (Reducer $reducer) use ($frequency) {
return new Reducer\TakeNth($reducer, $frequency);
};
}
Expand All @@ -264,7 +264,7 @@ function take_nth(int $frequency, $sequence = null)
function drop(int $number, $sequence = null)
{
if ($sequence === null) {
return function (Reducer $reducer) use ($number) {
return static function (Reducer $reducer) use ($number) {
return new Reducer\Drop($reducer, $number);
};
}
Expand All @@ -281,7 +281,7 @@ function drop(int $number, $sequence = null)
function drop_while(callable $callback, $sequence = null)
{
if ($sequence === null) {
return function (Reducer $reducer) use ($callback) {
return static function (Reducer $reducer) use ($callback) {
return new Reducer\DropWhile($reducer, $callback);
};
}
Expand All @@ -299,7 +299,7 @@ function drop_while(callable $callback, $sequence = null)
function paginate($page = 1, $numberOfItems = 10, $sequence = null)
{
if ($sequence === null) {
return function (Reducer $reducer) use ($page, $numberOfItems) {
return static function (Reducer $reducer) use ($page, $numberOfItems) {
return new Reducer\Paginate($reducer, $page, $numberOfItems);
};
}
Expand All @@ -316,7 +316,7 @@ function paginate($page = 1, $numberOfItems = 10, $sequence = null)
function replace(array $map, $sequence = null)
{
if ($sequence === null) {
return function (Reducer $reducer) use ($map) {
return static function (Reducer $reducer) use ($map) {
return new Reducer\Replace($reducer, $map);
};
}
Expand All @@ -331,7 +331,7 @@ function replace(array $map, $sequence = null)
function distinct($sequence = null, ?callable $callback = null)
{
if ($sequence === null) {
return function (Reducer $reducer) use ($callback) {
return static function (Reducer $reducer) use ($callback) {
return new Reducer\Distinct($reducer, $callback);
};
}
Expand All @@ -346,7 +346,7 @@ function distinct($sequence = null, ?callable $callback = null)
function dedupe($sequence = null, ?callable $callback = null)
{
if ($sequence === null) {
return function (Reducer $reducer) use ($callback) {
return static function (Reducer $reducer) use ($callback) {
return new Reducer\Dedupe($reducer, $callback);
};
}
Expand All @@ -363,7 +363,7 @@ function dedupe($sequence = null, ?callable $callback = null)
function partition(int $size, $sequence = null)
{
if ($sequence === null) {
return function (Reducer $reducer) use ($size) {
return static function (Reducer $reducer) use ($size) {
return new Reducer\Partition($reducer, $size);
};
}
Expand All @@ -380,7 +380,7 @@ function partition(int $size, $sequence = null)
function partition_by(callable $callback, $sequence = null)
{
if ($sequence === null) {
return function (Reducer $reducer) use ($callback) {
return static function (Reducer $reducer) use ($callback) {
return new Reducer\PartitionBy($reducer, $callback);
};
}
Expand All @@ -397,7 +397,7 @@ function partition_by(callable $callback, $sequence = null)
function group_by(callable $callback, $sequence = null)
{
if ($sequence === null) {
return function (Reducer $reducer) use ($callback) {
return static function (Reducer $reducer) use ($callback) {
return new Reducer\GroupBy($reducer, $callback);
};
}
Expand All @@ -407,14 +407,14 @@ function group_by(callable $callback, $sequence = null)

/**
* @param callable $callback
* @param null|iterable $sequence
* @param null $sequence
*
* @return null|array|\Closure|mixed
* @return array|\Closure|mixed
*/
function sort(callable $callback, $sequence = null)
{
if ($sequence === null) {
return function (Reducer $reducer) use ($callback) {
return static function (Reducer $reducer) use ($callback) {
return new Reducer\Sort($reducer, $callback);
};
}
Expand All @@ -436,7 +436,7 @@ function sort(callable $callback, $sequence = null)
function sort_by(callable $callback, $sequence = null)
{
if ($sequence === null) {
return function (Reducer $reducer) use ($callback) {
return static function (Reducer $reducer) use ($callback) {
return new Reducer\SortBy($reducer, $callback);
};
}
Expand Down
2 changes: 1 addition & 1 deletion src/transform.php
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ function curry(callable $function, ...$arguments)
*/
function comparator(callable $callback)
{
return function ($first, $second) use ($callback) {
return static function ($first, $second) use ($callback) {
$first = ($callback)($first);
$second = ($callback)($second);
if ($first == $second) {
Expand Down

0 comments on commit 8e9294d

Please sign in to comment.