|
1 | 1 | package io.reflectoring.function;
|
2 | 2 |
|
3 |
| -import java.util.function.Function; |
4 |
| -import java.util.function.IntUnaryOperator; |
5 |
| -import java.util.function.LongUnaryOperator; |
6 |
| -import java.util.function.UnaryOperator; |
| 3 | +import java.util.Optional; |
| 4 | +import java.util.OptionalInt; |
| 5 | +import java.util.function.*; |
| 6 | +import java.util.stream.DoubleStream; |
7 | 7 | import java.util.stream.IntStream;
|
8 | 8 | import java.util.stream.LongStream;
|
9 | 9 | import org.junit.jupiter.api.Assertions;
|
@@ -54,4 +54,64 @@ void longUnaryOperator() {
|
54 | 54 | final long[] result = input.map(distance).toArray();
|
55 | 55 | Assertions.assertArrayEquals(new long[] {931410L, 1862820L, 2794230L}, result);
|
56 | 56 | }
|
| 57 | + |
| 58 | + @Test |
| 59 | + void doubleUnaryOperator() { |
| 60 | + DoubleUnaryOperator circleArea = radius -> radius * radius * Math.PI; |
| 61 | + DoubleUnaryOperator doubleIt = area -> area * 4; |
| 62 | + DoubleUnaryOperator scaling = circleArea.andThen(doubleIt); |
| 63 | + |
| 64 | + Assertions.assertEquals(153.93D, circleArea.applyAsDouble(7), 0.01); |
| 65 | + Assertions.assertEquals(615.75D, scaling.applyAsDouble(7), 0.01); |
| 66 | + |
| 67 | + final DoubleStream input = DoubleStream.of(7d, 14d, 21d); |
| 68 | + final double[] result = input.map(circleArea).toArray(); |
| 69 | + Assertions.assertArrayEquals(new double[] {153.93D, 615.75D, 1385.44D}, result, 0.01); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + void binaryOperator() { |
| 74 | + LongUnaryOperator factorial = |
| 75 | + n -> { |
| 76 | + long result = 1L; |
| 77 | + for (int i = 1; i <= n; i++) { |
| 78 | + result *= i; |
| 79 | + } |
| 80 | + return result; |
| 81 | + }; |
| 82 | + // Calculate permutations |
| 83 | + BinaryOperator<Long> npr = (n, r) -> factorial.applyAsLong(n) / factorial.applyAsLong(n - r); |
| 84 | + // Verify permutations |
| 85 | + // 3P2 means the number of permutations of 2 that can be achieved from a choice of 3. |
| 86 | + final Long result3P2 = npr.apply(3L, 2L); |
| 87 | + Assertions.assertEquals(6L, result3P2); |
| 88 | + |
| 89 | + // Add two prices |
| 90 | + BinaryOperator<Double> addPrices = Double::sum; |
| 91 | + // Apply discount |
| 92 | + UnaryOperator<Double> applyDiscount = total -> total * 0.9; // 10% discount |
| 93 | + // Apply tax |
| 94 | + UnaryOperator<Double> applyTax = total -> total * 1.07; // 7% tax |
| 95 | + // Composing the final operation |
| 96 | + BiFunction<Double, Double, Double> finalCost = |
| 97 | + addPrices.andThen(applyDiscount).andThen(applyTax); |
| 98 | + |
| 99 | + // Prices of two items |
| 100 | + double item1 = 50.0; |
| 101 | + double item2 = 100.0; |
| 102 | + // Calculate final cost |
| 103 | + double cost = finalCost.apply(item1, item2); |
| 104 | + // Verify the final calculated cost |
| 105 | + Assertions.assertEquals(144.45D, cost, 0.01); |
| 106 | + } |
| 107 | + |
| 108 | + @Test |
| 109 | + void intBinaryOperator() { |
| 110 | + IntBinaryOperator add = Integer::sum; |
| 111 | + Assertions.assertEquals(10, add.applyAsInt(4, 6)); |
| 112 | + |
| 113 | + IntStream input = IntStream.of(2, 3, 4); |
| 114 | + OptionalInt result = input.reduce(add); |
| 115 | + Assertions.assertEquals(OptionalInt.of(9), result); |
| 116 | + } |
57 | 117 | }
|
0 commit comments