public static long factorial(final int n) { if (n > 20 || n < 0) { throw new IllegalArgumentException(n + " is out of range"); } return LongStream.rangeClosed(2, n).reduce(1, (a, b) -> a * b); }
default long reduce(LongPipeline pipeline, long identity, LongBinaryOperator op) { requireNonNull(pipeline); requireNonNull(identity); requireNonNull(op); return optimize(pipeline).getAsLongStream().reduce(identity, op); }
default OptionalLong reduce(LongPipeline pipeline, LongBinaryOperator op) { requireNonNull(pipeline); requireNonNull(op); return optimize(pipeline).getAsLongStream().reduce(op); }
@Override public Object getExpectedValue(int start, int length) { if (length == 0) { return null; } return LongStream.range(start, start + length).reduce(start, (x, y) -> x | y); }
@Override public Object getExpectedValue(int start, int length) { if (length == 0) { return null; } return LongStream.range(start, start + length).reduce(start, (x, y) -> x & y); }
@Override public long reduce(long identity, LongBinaryOperator op) { return finallyClose(() -> stream().reduce(identity, op)); }
@Override public OptionalLong reduce(LongBinaryOperator op) { return finallyClose(() -> stream().reduce(op)); }
@Test public void testStd() throws InterruptedException, ExecutionException { double avgSq = LongStream.range(0, COUNT).map(l -> l * l).reduce((l1, l2) -> l1 + l2).getAsLong() / (double) COUNT; double avg = (COUNT - 1) / 2.0; double std = Math.sqrt(avgSq - avg * avg); assertEquals(std, AsyncAggregationClient .std(TABLE, new LongColumnInterpreter(), new Scan().addColumn(CF, CQ)).get().doubleValue(), DELTA); }
this.integersJDK.stream().mapToLong(each -> each + 1).reduce(0, (accum, each) -> accum + each)); this.integersJDK.stream().mapToLong(each -> each + 2).reduce(0, (accum, each) -> { Assert.assertTrue(each >= 0); return accum + each; this.integersJDK.stream().mapToLong(each -> each + 3).reduce(0, (accum, each) -> { long result = accum + each; Assert.assertTrue(each >= 0); this.integersJDK.parallelStream().mapToLong(each -> each + 1).reduce(0, (accum, each) -> accum + each)); this.integersJDK.parallelStream().mapToLong(each -> each + 2).reduce(0, (accum, each) -> { Assert.assertTrue(each >= 0); return accum + each; this.integersJDK.parallelStream().mapToLong(each -> each + 3).reduce(0, (accum, each) -> { long result = accum + each; Assert.assertTrue(each >= 0);
this.integersJDK.stream().mapToLong(each -> each + 1).reduce(0, (accum, each) -> accum + each)); this.integersJDK.stream().mapToLong(each -> each + 2).reduce(0, (accum, each) -> { Assert.assertTrue(each >= 0); return accum + each; this.integersJDK.stream().mapToLong(each -> each + 3).reduce(0, (accum, each) -> { long result = accum + each; Assert.assertTrue(each >= 0); this.integersJDK.parallelStream().mapToLong(each -> each + 1).reduce(0, (accum, each) -> accum + each)); this.integersJDK.parallelStream().mapToLong(each -> each + 2).reduce(0, (accum, each) -> { Assert.assertTrue(each >= 0); return accum + each; this.integersJDK.parallelStream().mapToLong(each -> each + 3).reduce(0, (accum, each) -> { long result = accum + each; Assert.assertTrue(each >= 0);
@Test public void testMedianWithWeight() throws InterruptedException, ExecutionException { long halfSum = LongStream.range(0, COUNT).map(l -> l * l).reduce((l1, l2) -> l1 + l2).getAsLong() / 2; long median = 0L; long sum = 0L; for (int i = 0; i < COUNT; i++) { sum += i * i; if (sum > halfSum) { median = i - 1; break; } } assertEquals(median, AsyncAggregationClient .median(TABLE, new LongColumnInterpreter(), new Scan().addColumn(CF, CQ).addColumn(CF, CQ2)) .get().longValue()); } }
checksum = zip.stream().mapToLong(ZipEntry::getCrc).reduce((l1, l2) -> l1 ^ l2).getAsLong();
@VisibleForTesting static Consumer<List<BatchElement<Integer, TimestampRange>>> consumer(TimestampService delegate) { return batch -> { long totalTimestamps = batch.stream().mapToLong(BatchElement::argument).reduce(0, Math::addExact); long startInclusive = 0; long endExclusive = 0; for (BatchElement<Integer, TimestampRange> element : batch) { int timestampsRequired = element.argument(); if (element.argument() <= endExclusive - startInclusive) { element.result().set(createExclusiveRange(startInclusive, startInclusive + timestampsRequired)); startInclusive += timestampsRequired; totalTimestamps -= timestampsRequired; } else { TimestampRange requested = getFreshTimestampsFromDelegate( delegate, Ints.saturatedCast(totalTimestamps)); startInclusive = requested.getLowerBound(); endExclusive = Math.addExact(requested.getUpperBound(), 1); int toTake = Math.min(Ints.checkedCast(endExclusive - startInclusive), timestampsRequired); element.result().set(createExclusiveRange(startInclusive, startInclusive + toTake)); startInclusive += toTake; totalTimestamps -= timestampsRequired; } } }; }
public static long cartesianProductSize(List<? extends Set<?>> listOfSets) { if (listOfSets.isEmpty()) { return 0L; } else { return listOfSets.stream().mapToLong(s -> s.size()).reduce(1, (l, r) -> LongMath.checkedMultiply(l, r)); } }
default long reduce(LongPipeline pipeline, long identity, LongBinaryOperator op) { requireNonNull(pipeline); requireNonNull(identity); requireNonNull(op); return optimize(pipeline).getAsLongStream().reduce(identity, op); }
protected long getTimes(TimesGetter getter) { long times = cacheManagerMap.values().stream() .mapToLong(cacheManager -> getTimes(cacheManager, getter)) .reduce(Math::addExact).orElse(0); return times; }
default OptionalLong reduce(LongPipeline pipeline, LongBinaryOperator op) { requireNonNull(pipeline); requireNonNull(op); return optimize(pipeline).getAsLongStream().reduce(op); }
protected long getTimes(CacheManager cacheManager, TimesGetter getter) { long times = cacheManager.getCacheNames().parallelStream() .map(name -> cacheManager.getCache(name)) .filter(cache -> cache instanceof MonitorCache) .map(cache -> (MonitorCache) cache) .mapToLong(getter::get) .reduce(Math::addExact).orElse(0); return times; }
@Test public void testStd() throws InterruptedException, ExecutionException { double avgSq = LongStream.range(0, COUNT).map(l -> l * l).reduce((l1, l2) -> l1 + l2).getAsLong() / (double) COUNT; double avg = (COUNT - 1) / 2.0; double std = Math.sqrt(avgSq - avg * avg); assertEquals(std, AsyncAggregationClient .std(TABLE, new LongColumnInterpreter(), new Scan().addColumn(CF, CQ)).get().doubleValue(), DELTA); }