/** * A very fast reduce by. If performance is your thing, this seems to be as fast a plain for loop * when benchmarking with JMH. * * @param array array of items to reduce by * @param reduceBy reduceBy interface * @return the final value */ public static long reduceBy(final int[] array, ReduceBy reduceBy) { long sum = 0; for (int v : array) { sum = reduceBy.reduce(sum, v); } return sum; }
/** * @param array array of items to reduce by * @param length where to end in the array * @param reduceBy the function to do the reduce by * @return the reduction */ public static long reduceBy(final int[] array, final int length, ReduceBy reduceBy) { long sum = 0; for (int index = 0; index < length; index++) { int v = array[index]; sum = reduceBy.reduce(sum, v); } return sum; }