Skip to content

Commit 222a818

Browse files
committed
Ensure the max_by sequence is valid
1 parent 7698850 commit 222a818

File tree

1 file changed

+4
-4
lines changed

1 file changed

+4
-4
lines changed

src/FnDispatcher.php

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -126,12 +126,12 @@ private function fn_max_by(array $args)
126126
{
127127
$this->validate('max_by', $args, [['array'], ['expression']]);
128128
$expr = $this->wrapExpression('max_by:1', $args[1], ['number', 'string']);
129-
$i = -1;
130-
return array_reduce($args[0], function ($carry, $item) use ($expr, &$i) {
131-
return ++$i
129+
$fn = function ($carry, $item, $index) use ($expr) {
130+
return $index
132131
? ($expr($carry) >= $expr($item) ? $carry : $item)
133132
: $item;
134-
});
133+
};
134+
return $this->reduce('max_by:1', $args[0], ['any'], $fn);
135135
}
136136

137137
private function fn_min(array $args)

0 commit comments

Comments
 (0)