Private
constructorPrivate
Readonly
itemsThe array of items to be reduced.
Folds the elements of the array to a single value by applying a folder function.
Similar to reduce
, but the folder function is curried.
The result of the folding.
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.
The result of the folding with index information.
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.
The result of the folding from right to left.
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.
The result of the folding from right to left with index information.
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.
An Optional object that either contains the maximum element or is empty if the array is empty.
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.
A function to extract a numeric value from each element.
An Optional containing the element with the maximum numeric value, or an empty Optional if the array is empty.
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.
An Optional containing the element with the maximum comparable value, or an empty Optional if the array is empty.
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.
A function to extract a comparable value from each element.
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.
An Optional containing the element with the maximum comparable value, or an empty Optional if the array is empty.
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.
An Optional object that either contains the minimum element or is empty if the array is empty
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.
A function to extract a numeric value from each element.
An Optional containing the element with the minimum numeric value, or an empty Optional if the array is empty.
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.
An Optional containing the element with the minimum comparable value, or an empty Optional if the array is empty.
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.
A function to extract a comparable value from each element.
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.
An Optional containing the element with the minimum comparable value, or an empty Optional if the array is empty
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.
The final accumulated result.
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.
The result of the reduction with index information.
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.
The result of the reduction from right to left with index information.
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.
An array of intermediate results from the running fold.
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.
An array of intermediate results from the running fold with index information.
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.
An array of intermediate results from the running reduce.
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.
An array of intermediate results from the running reduce with index information.
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.
A function to extract a numeric value from each element.
The sum of the numeric values obtained by applying the selector function to each element.
const numbers = [1, 2, 3, 4, 5];
const sum = numbers.reducer().sumOf((num) => num);
console.log(sum); // Output: 15
Static
ofCreates a new Reducer instance from an array of items and a comparator function.
An array of items to be reduced.
A new Reducer instance.
const numbers = [3, 1, 4, 1, 5, 9, 2];
const numberReducer = Reducer.of(numbers);
Generated using TypeDoc
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