Search in sources :

Example 1 with IntBinaryOperator

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

the class ReduceTest method testReduce.

@Test
public void testReduce() {
    assertFalse(IntStream.empty().reduce(new IntBinaryOperator() {

        @Override
        public int applyAsInt(int left, int right) {
            throw new IllegalStateException();
        }
    }).isPresent());
    assertEquals(IntStream.of(42).reduce(new IntBinaryOperator() {

        @Override
        public int applyAsInt(int left, int right) {
            throw new IllegalStateException();
        }
    }).getAsInt(), 42);
    assertEquals(IntStream.of(41, 42).reduce(new IntBinaryOperator() {

        @Override
        public int applyAsInt(int left, int right) {
            if (right > left)
                return right;
            return left;
        }
    }).getAsInt(), 42);
}
Also used : IntBinaryOperator(com.annimon.stream.function.IntBinaryOperator) Test(org.junit.Test)

Example 2 with IntBinaryOperator

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

the class CustomTest method testCustomIntermediateOperator_Zip.

@Test
public void testCustomIntermediateOperator_Zip() {
    final IntBinaryOperator op = new IntBinaryOperator() {

        @Override
        public int applyAsInt(int left, int right) {
            return left + right;
        }
    };
    IntStream s1 = IntStream.of(1, 3, 5, 7, 9);
    IntStream s2 = IntStream.of(2, 4, 6, 8);
    int[] expected = { 3, 7, 11, 15 };
    IntStream result = s1.custom(new CustomOperators.Zip(s2, op));
    assertThat(result.toArray(), is(expected));
}
Also used : IntBinaryOperator(com.annimon.stream.function.IntBinaryOperator) IntStream(com.annimon.stream.IntStream) CustomOperators(com.annimon.stream.CustomOperators) Test(org.junit.Test)

Aggregations

IntBinaryOperator (com.annimon.stream.function.IntBinaryOperator)2 Test (org.junit.Test)2 CustomOperators (com.annimon.stream.CustomOperators)1 IntStream (com.annimon.stream.IntStream)1