Using loops instead of higher order functions

  • You are here: Free PHP » Uncategorized » Using loops instead of higher order functions

I came across a great article from Kirstian Poslek, titled “One reduce() to rule them all”. It explains the reduce() function quite well.

I often feel though that using higher order functions are used in many cases where simple loops might be more legible.

The article ends with an example of a function that takes an array, and returns the maximum, minimum and average values. This is the sample from the article:

const data = [115, 26, 99];

const callbackFunction = function(
    accumulator,
    currentElement,
    currentIndex,
    array
) {
    // Get the maximum by checking first if there is a maximum from the previous step
    const maximum = accumulator.maximum
        ? // If there is, then check if the current element is higher than the previous maximum
          accumulator.maximum < currentElement
            ? currentElement
            : accumulator.maximum
        : // If there isn't, use the current element right away
          currentElement;

    // Get the minimum by checking first if there is a minimum from the previous step
    const minimum = accumulator.minimum
        ? // If there is, then check if the current element is lower than the previous maximum
          accumulator.minimum > currentElement
            ? currentElement
            : accumulator.minimum
        : // If there isn't, use the current element right away
          currentElement;

    // Get the average by checking if we're at the last step (where it we can finally calculate the average)
    const average =
        currentIndex === array.length - 1
            ? (accumulator.average + currentElement) / array.length
            : // If we're not at the last step, check if there even is a value from the previous step
            accumulator.average
                ? accumulator.average + currentElement
                : currentElement;

    // Return the value for the next element
    return {
        maximum,
        minimum,
        average
    };

Truncated by Planet PHP, read more at the original (another 3831 bytes)

Powered by Gewgley