Search in sources :

Example 1 with DoubleBinaryOperator

use of com.annimon.stream.function.DoubleBinaryOperator in project Lightweight-Stream-API by aNNiMON.

the class ReduceTest method testReduceWithIdentity.

@Test
public void testReduceWithIdentity() {
    double result = DoubleStream.of(0.012, -3.772, 3.039, 19.84, 100d).reduce(0d, new DoubleBinaryOperator() {

        @Override
        public double applyAsDouble(double left, double right) {
            return left + right;
        }
    });
    assertThat(result, closeTo(119.119, 0.0001));
}
Also used : DoubleBinaryOperator(com.annimon.stream.function.DoubleBinaryOperator) Test(org.junit.Test)

Example 2 with DoubleBinaryOperator

use of com.annimon.stream.function.DoubleBinaryOperator in project Lightweight-Stream-API by aNNiMON.

the class CustomTest method testCustomIntermediateOperator_Zip.

@Test
@SuppressWarnings("unchecked")
public void testCustomIntermediateOperator_Zip() {
    final DoubleBinaryOperator op = new DoubleBinaryOperator() {

        @Override
        public double applyAsDouble(double left, double right) {
            return left * right;
        }
    };
    DoubleStream s1 = DoubleStream.of(1.01, 2.02, 3.03);
    IntStream s2 = IntStream.range(2, 5);
    DoubleStream result = s1.custom(new CustomOperators.ZipWithIntStream(s2, op));
    assertThat(result, elements(arrayContaining(closeTo(2.02, 0.00001), closeTo(6.06, 0.00001), closeTo(12.12, 0.00001))));
}
Also used : DoubleBinaryOperator(com.annimon.stream.function.DoubleBinaryOperator) DoubleStream(com.annimon.stream.DoubleStream) IntStream(com.annimon.stream.IntStream) CustomOperators(com.annimon.stream.CustomOperators) Test(org.junit.Test)

Example 3 with DoubleBinaryOperator

use of com.annimon.stream.function.DoubleBinaryOperator in project Lightweight-Stream-API by aNNiMON.

the class ReduceTest method testReduceWithIdentityOnEmptyStream.

@Test
public void testReduceWithIdentityOnEmptyStream() {
    double result = DoubleStream.empty().reduce(Math.PI, new DoubleBinaryOperator() {

        @Override
        public double applyAsDouble(double left, double right) {
            return left + right;
        }
    });
    assertThat(result, closeTo(Math.PI, 0.00001));
}
Also used : DoubleBinaryOperator(com.annimon.stream.function.DoubleBinaryOperator) Test(org.junit.Test)

Aggregations

DoubleBinaryOperator (com.annimon.stream.function.DoubleBinaryOperator)3 Test (org.junit.Test)3 CustomOperators (com.annimon.stream.CustomOperators)1 DoubleStream (com.annimon.stream.DoubleStream)1 IntStream (com.annimon.stream.IntStream)1