Class Reducer<T>

A class representing a generic reducer for aggregating elements using a specified comparator.

Since

version 1.4.0

Author

Manuel Santos ney.br.santos@gmail.com

Type Parameters

  • T

    The type of elements to reduce.

Constructors

Properties

items: T[]

The array of items to be reduced.

Methods

  • Folds the elements of the array to a single value by applying a folder function. Similar to reduce, but the folder function is curried.

    Parameters

    • folder: ((current) => ((accumulator) => T))

      A curried function that takes the current element and returns a function to apply to the next element.

        • (current): ((accumulator) => T)
        • Parameters

          • current: T

          Returns ((accumulator) => T)

            • (accumulator): T
            • Parameters

              • accumulator: T

              Returns T

    • Optional initialValue: T

      The initial value for the folder function.

    Returns T

    The result of the folding.

    Example

    const numbers = [1, 2, 3, 4, 5];
    const reducer = Reducer.of(numbers);
    const product = reducer.fold((num) => (acc) => acc * num, 1);
    console.log(product); // Output: 120
  • Folds the elements of an array into a single value by applying a curried folder function with index information.

    Parameters

    • folder: ((current, index) => ((accumulator) => T))

      A curried function that takes the current element, the current index, and returns a function to apply to the next element.

        • (current, index): ((accumulator) => T)
        • Parameters

          • current: T
          • index: number

          Returns ((accumulator) => T)

            • (accumulator): T
            • Parameters

              • accumulator: T

              Returns T

    • Optional initialValue: T

      The initial value for the folder function.

    Returns T

    The result of the folding with index information.

    Example

    const numbers = [1, 2, 3, 4, 5];
    const reducer = Reducer.of(numbers);
    const product = reducer.foldIndexed((num, index) => (acc) => acc * (num + index), 1);
    console.log(product); // Output: 120 (1 * (1 + 0) * (2 + 1) * (3 + 2) * (4 + 3) * (5 + 4))
  • Folds the elements of the array from right to left to a single value by applying a folder function.

    Parameters

    • folder: ((current) => ((accumulator) => T))

      A curried function that takes the current element and returns a function to apply to the next element.

        • (current): ((accumulator) => T)
        • Parameters

          • current: T

          Returns ((accumulator) => T)

            • (accumulator): T
            • Parameters

              • accumulator: T

              Returns T

    • Optional initialValue: T

      The initial value for the folder function.

    Returns T

    The result of the folding from right to left.

    Example

    const numbers = [1, 2, 3, 4, 5];
    const reducer = Reducer.of(numbers);
    const result = reducer.foldRight((num) => (acc) => acc - num, 0);
    console.log(result); // Output: -5 (0 - 1 - 2 - 3 - 4 - 5)
  • Folds the elements of the array from right to left to a single value by applying a folder function with index information.

    Parameters

    • folder: ((current, index) => ((accumulator) => T))

      A curried function that takes the current element, the current index, and returns a function to apply to the next element.

        • (current, index): ((accumulator) => T)
        • Parameters

          • current: T
          • index: number

          Returns ((accumulator) => T)

            • (accumulator): T
            • Parameters

              • accumulator: T

              Returns T

    • Optional initialValue: T

      The initial value for the folder function.

    Returns T

    The result of the folding from right to left with index information.

    Example

    const numbers = [1, 2, 3, 4, 5];
    const result = Reducer.of(numbers).foldRightIndexed((num, index) => (acc) => acc - (num + index), 0);
    console.log(result); // Output: -9 (0 - (5 + 4) - (4 + 3) - (3 + 2) - (2 + 1) - (1 + 0))
  • Finds the maximum element in an array by using a comparator function to determine the order of the elements.

    Parameters

    • comparator: ((a, b) => number)
        • (a, b): number
        • Parameters

          Returns number

    Returns Optional<undefined | T>

    An Optional object that either contains the maximum element or is empty if the array is empty.

    Example

    const numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5];
    const reducer = Reducer.of(numbers);
    const maxResult = reducer.max((a, b) => a - b);
    console.log(maxResult); // Output: 9
  • Finds the maximum element in an array by comparing the results of a given selector function.

    Parameters

    • selector: ((item) => number)

      A function to extract a numeric value from each element.

        • (item): number
        • Parameters

          • item: T

          Returns number

    Returns Optional<undefined | T>

    An Optional containing the element with the maximum numeric value, or an empty Optional if the array is empty.

    Example

    const people = [
    { name: 'Alice', age: 30 },
    { name: 'Bob', age: 25 },
    { name: 'Charlie', age: 35 }
    ];
    const reducer = Reducer.of(people);
    const maxByAge = reducer.maxBy((person) => person.age);
    console.log(maxByAge); // Output: { name: 'Charlie', age: 35 }
  • Finds the maximum element in an array by applying a selector function to each element and comparing the results.

    Type Parameters

    Parameters

    • selector: ((item) => R)

      A function to extract a comparable value from each element.

        • (item): R
        • Parameters

          • item: T

          Returns R

    Returns Optional<undefined | T>

    An Optional containing the element with the maximum comparable value, or an empty Optional if the array is empty.

    Example

    const people = [
    { name: 'Alice', age: 30 },
    { name: 'Bob', age: 25 },
    { name: 'Charlie', age: 35 }
    ];
    const reducer = Reducer.of(people);
    const maxOfAge = reducer.maxOf((person) => person.age);
    console.log(maxOfAge); // Output: { name: 'Charlie', age: 35 }
  • Finds the maximum element in an array by applying a custom comparator function to the results of a selector function.

    Type Parameters

    • R

    Parameters

    • selector: ((item) => R)

      A function to extract a comparable value from each element.

        • (item): R
        • Parameters

          • item: T

          Returns R

    • comparator: ((a, b) => number)

      A function that compares two elements. Should return a negative value if the first element is smaller, a positive value if the first element is larger, or zero if they are equal.

        • (a, b): number
        • Parameters

          Returns number

    Returns Optional<undefined | T>

    An Optional containing the element with the maximum comparable value, or an empty Optional if the array is empty.

    Example

    const people = [
    { name: 'Alice', age: 30 },
    { name: 'Bob', age: 25 },
    { name: 'Charlie', age: 35 }
    ];
    const reducer = Reducer.of(people);
    const maxOfAge = reducer.maxOfWith((person) => person.age, (a, b) => a - b);
    console.log(maxOfAge); // Output: { name: 'Charlie', age: 35 }
  • Finds the minimum element in an array by using a comparator function to determine the order of the elements.

    Parameters

    • comparator: ((a, b) => number)
        • (a, b): number
        • Parameters

          Returns number

    Returns Optional<undefined | T>

    An Optional object that either contains the minimum element or is empty if the array is empty

    Example

    const numbers = [3, 1, 4, 1, 5, 9, 2, 6, 5, 3, 5];
    const reducer = Reducer.of(numbers);
    const minResult = reducer.min((a, b) => a - b);
    console.log(minResult); // Output: 1
  • Finds the minimum element in an array by comparing the results of a given selector function.

    Parameters

    • selector: ((item) => number)

      A function to extract a numeric value from each element.

        • (item): number
        • Parameters

          • item: T

          Returns number

    Returns Optional<undefined | T>

    An Optional containing the element with the minimum numeric value, or an empty Optional if the array is empty.

    Example

    const people = [
    { name: 'Alice', age: 30 },
    { name: 'Bob', age: 25 },
    { name: 'Charlie', age: 35 }
    ];
    const reducer = Reducer.of(people);
    const minByAge = reducer.minBy((person) => person.age);
    console.log(minByAge); // Output: { name: 'Bob', age: 25 }
  • Finds the minimum element in an array by applying a selector function to each element and comparing the results.

    Type Parameters

    Parameters

    • selector: ((item) => R)

      A function to extract a comparable value from each element.

        • (item): R
        • Parameters

          • item: T

          Returns R

    Returns Optional<undefined | T>

    An Optional containing the element with the minimum comparable value, or an empty Optional if the array is empty.

    Example

    const people = [
    { name: 'Alice', age: 30 },
    { name: 'Bob', age: 25 },
    { name: 'Charlie', age: 35 }
    ];
    const reducer = Reducer.of(people);
    const minOfAge = reducer.minOf((person) => person.age);
    console.log(minOfAge); // Output: { name: 'Bob', age: 25 }
  • Finds the minimum element in an array by applying a custom comparator function to the results of a selector function.

    Type Parameters

    • R

    Parameters

    • selector: ((item) => R)

      A function to extract a comparable value from each element.

        • (item): R
        • Parameters

          • item: T

          Returns R

    • comparator: ((a, b) => number)

      A function that compares two elements. Should return a negative value if the first element is smaller, a positive value if the first element is larger, or zero if they are equal.

        • (a, b): number
        • Parameters

          Returns number

    Returns Optional<undefined | T>

    An Optional containing the element with the minimum comparable value, or an empty Optional if the array is empty

    Example

    const people = [
    { name: 'Alice', age: 30 },
    { name: 'Bob', age: 25 },
    { name: 'Charlie', age: 35 }
    ];
    const reducer = Reducer.of(people);
    const minOfAge = reducer.minOfWith((person) => person.age, (a, b) => a - b);
    console.log(minOfAge); // Output: { name: 'Bob', age: 25 }
  • Reduces the items in the array to a single value using the specified reducer function.

    Parameters

    • reducerFunction: ((accumulator, current, index) => T)

      A function that takes an accumulator and the current item and produces a new accumulator value.

        • (accumulator, current, index): T
        • Parameters

          • accumulator: T
          • current: T
          • index: number

          Returns T

    • Optional initialValue: T

      The initial value of the accumulator.

    Returns T

    The final accumulated result.

    Example

    const numbers = [3, 1, 4, 1, 5, 9, 2];
    const numberReducer = Reducer.of(numbers);

    const sum = numberReducer.reduce((acc, num) => acc + num);
    // sum = 25

    const product = numberReducer.reduce((acc, num) => acc * num, 1);
    // product = 1080
  • Reduces the elements of an array to a single value by applying a reducer function that takes an accumulator, the current element, and the current index. It allows for index-based operations during the reduction process.

    Parameters

    • reducer: ((accumulator, current, index) => T)

      A function that takes an accumulator, the current element, and the current index, and returns a new accumulator.

        • (accumulator, current, index): T
        • Parameters

          • accumulator: T
          • current: T
          • index: number

          Returns T

    • Optional initialValue: T

      The initial value of the accumulator.

    Returns T

    The result of the reduction with index information.

    Example

    const numbers = [1, 2, 3, 4, 5];
    const reducer = Reducer.of(numbers);
    const product = reducer.reduceIndexed((acc, num, index) => acc * (num + index), 1);
    console.log(product); // Output: 120 (1 * (1 + 0) * (2 + 1) * (3 + 2) * (4 + 3) * (5 + 4))
  • Reduces the elements of the array from right to left to a single value by applying a reducer function with index information.

    Parameters

    • reducer: ((accumulator, current, index) => T)

      A function that takes an accumulator, the current element, and the current index, and returns a new accumulator.

        • (accumulator, current, index): T
        • Parameters

          • accumulator: T
          • current: T
          • index: number

          Returns T

    • Optional initialValue: T

      The initial value of the accumulator.

    Returns T

    The result of the reduction from right to left with index information.

    Example

    const numbers = [1, 2, 3, 4, 5];
    const reducer = (acc, num, index) => acc - (num + index);
    const result = Reducer.of(numbers).reduceRightIndexed(reducer);
    console.log(result); // Output: -9

    In this example, we have an array of numbers [1, 2, 3, 4, 5]. The reducer function subtracts each number from the
    accumulator, where the accumulator is initially set to the last element of the array. The index of each element
    is also subtracted from the accumulator. The result is -9, which is obtained by performing the following
    calculations: 5 - (4 + 3) - (3 + 2) - (2 + 1) - (1 + 0).
  • Performs a running fold (scan) on the elements of the array from left to right.

    Parameters

    • folder: ((current) => ((accumulator) => T))

      A curried function that takes the current element and returns a function to apply to the next element.

        • (current): ((accumulator) => T)
        • Parameters

          • current: T

          Returns ((accumulator) => T)

            • (accumulator): T
            • Parameters

              • accumulator: T

              Returns T

    • Optional initialValue: T

      The initial value for the folder function.

    Returns T[]

    An array of intermediate results from the running fold.

    Example

    const numbers = [1, 2, 3, 4, 5];
    const result = Reducer.of(numbers).runningFold((num) => (acc) => acc + num);
    console.log(result); // Output: [0, 1, 3, 6, 10, 15] (0, 0 + 1, (0 + 1) + 2, ((0 + 1) + 2) + 3, (((0 + 1) + 2) + 3) + 4, ((((0 + 1) + 2) + 3) + 4) + 5)
  • Performs a running fold (scan) on the elements of the array from left to right with index information.

    Parameters

    • folder: ((current, index) => ((accumulator) => T))

      A curried function that takes the current element, the current index, and returns a function to apply to the next element.

        • (current, index): ((accumulator) => T)
        • Parameters

          • current: T
          • index: number

          Returns ((accumulator) => T)

            • (accumulator): T
            • Parameters

              • accumulator: T

              Returns T

    • Optional initialValue: T

      The initial value for the folder function.

    Returns T[]

    An array of intermediate results from the running fold with index information.

    Example

    const numbers = [1, 2, 3, 4, 5];
    const reducer = Reducer.of(numbers);
    const result = reducer.runningFoldIndexed((num, index) => (acc) => acc + (num + index), 0);
    console.log(result); // Output: [0, 1, 4, 9, 16, 25] (0, 0 + (1 + 0), (0 + (1 + 0)) + (2 + 1), ((0 + (1 + 0)) + (2 + 1)) + (3 + 2), (((0 + (1 + 0)) + (2 + 1)) + (3 + 2)) + (4 + 3), ((((0 + (1 + 0)) + (2 + 1)) + (3 + 2)) + (4 + 3)) + (5 + 4))
  • Performs a running reduce on the elements of the array from left to right.

    Parameters

    • reducer: ((accumulator, current) => T)

      A function that takes an accumulator and the current element and returns a new accumulator.

        • (accumulator, current): T
        • Parameters

          • accumulator: T
          • current: T

          Returns T

    • Optional initialValue: T

      The initial value of the accumulator.

    Returns T[]

    An array of intermediate results from the running reduce.

    Example

    const numbers = [1, 2, 3, 4, 5];
    const reducer = Reducer.of(numbers);
    const result = reducer.runningReduce((acc, num) => acc + num);
    console.log(result); // Output: [1, 3, 6, 10, 15] (1, 1 + 2, (1 + 2) + 3, ((1 + 2) + 3) + 4, (((1 + 2) + 3) + 4) + 5)
  • Performs a running reduce operation on the elements of an array from left to right, while also providing the index information. It takes a reducer function, which takes an accumulator, the current element, and the current index, and returns a new accumulator. The method returns an array of intermediate results from the running reduce operation.

    Parameters

    • reducer: ((accumulator, current, index) => T)

      A function that takes an accumulator, the current element, and the current index and returns a new accumulator.

        • (accumulator, current, index): T
        • Parameters

          • accumulator: T
          • current: T
          • index: number

          Returns T

    • Optional initialValue: T

      The initial value of the accumulator.

    Returns T[]

    An array of intermediate results from the running reduce with index information.

    Example

    const numbers = [1, 2, 3, 4, 5];
    const reducer = (acc, num, index) => acc + (num + index);
    const result = Reducer.of(numbers).runningReduceIndexed(reducer, 0);
    console.log(result); // Output: [1, 4, 9, 16, 25] (1, 1 + (2 + 1), (1 + (2 + 1)) + (3 + 2), ((1 + (2 + 1)) + (3 + 2)) + (4 + 3), (((1 + (2 + 1)) + (3 + 2)) + (4 + 3)) + (5 + 4))
  • Sums the values by applying a selector function to each element in the array.

    Parameters

    • selector: ((item) => number)

      A function to extract a numeric value from each element.

        • (item): number
        • Parameters

          • item: T

          Returns number

    Returns number

    The sum of the numeric values obtained by applying the selector function to each element.

    Example

    const numbers = [1, 2, 3, 4, 5];
    const sum = numbers.reducer().sumOf((num) => num);
    console.log(sum); // Output: 15
  • Creates a new Reducer instance from an array of items and a comparator function.

    Type Parameters

    • T

      The type of elements to reduce.

    Parameters

    • items: T[]

      An array of items to be reduced.

    Returns Reducer<T>

    A new Reducer instance.

    Static

    Example

    const numbers = [3, 1, 4, 1, 5, 9, 2];
    const numberReducer = Reducer.of(numbers);

Generated using TypeDoc