DEV Community

Discussion on: Never write another loop again

Collapse
 
lito profile image
Lito

Adding Arrow functions and First Class Callable Syntax:

<?php

class integer
{
    public function doubleAllPositiveValue(array $numbers): array
    {
        return array_map(
            $this->getDoubleValueCallback(...),
            $this->keepOnlyPositiveValue($numbers)
        );
    }

    private function getDoubleValueCallback(int $number): int
    {
        return $number * 2;
    }

    private function keepOnlyPositiveValue(array $numbers): array
    {
        return array_filter($numbers, fn ($number) => is_integer($number) && ($number > 0));
    }
}
Enter fullscreen mode Exit fullscreen mode
Collapse
 
klnjmm profile image
Jimmy Klein

Thank you πŸ™
I don’t do it because the problem is still the same, the order of the instructions is still weird with array functions.

Collapse
 
lito profile image
Lito

Laravel collections don't solve anything in terms of iterations in the code, they simply change the place of the loops, and in many cases in a much less efficient way. I only use Laravel Collections with my own data in a few places in a controlled way. When you work with large amounts of data they are a huge bottleneck.