Skip to content

Commit 876f547

Browse files
committed
Added BiFunction section
1 parent 1361184 commit 876f547

File tree

2 files changed

+43
-3
lines changed

2 files changed

+43
-3
lines changed
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package io.reflectoring.function;
2+
3+
import java.util.function.BiFunction;
4+
import java.util.function.Function;
5+
import org.junit.jupiter.api.Assertions;
6+
import org.junit.jupiter.api.Test;
7+
8+
public class FunctionTest {
9+
10+
@Test
11+
void simpleFunction() {
12+
Function<String, String> toUpper = s -> s == null ? null : s.toUpperCase();
13+
Assertions.assertEquals("JOY", toUpper.apply("joy"));
14+
Assertions.assertNull(toUpper.apply(null));
15+
}
16+
17+
@Test
18+
void functionComposition() {
19+
Function<String, String> toUpper = s -> s == null ? null : s.toUpperCase();
20+
Function<String, String> replaceVowels =
21+
s ->
22+
s == null
23+
? null
24+
: s.replace("A", "")
25+
.replace("E", "")
26+
.replace("I", "")
27+
.replace("O", "")
28+
.replace("U", "");
29+
Assertions.assertEquals("APPLE", toUpper.compose(replaceVowels).apply("apple"));
30+
Assertions.assertEquals("PPL", toUpper.andThen(replaceVowels).apply("apple"));
31+
}
32+
33+
@Test
34+
void biFunction() {
35+
BiFunction<Integer, Integer, Integer> bigger =
36+
(first, second) -> first > second ? first : second;
37+
Function<Integer, Integer> square = number -> number * number;
38+
39+
Assertions.assertEquals(10, bigger.apply(4, 10));
40+
Assertions.assertEquals(100, bigger.andThen(square).apply(4, 10));
41+
}
42+
}
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,14 @@
1-
package io.reflectoring.function.predicate;
1+
package io.reflectoring.function;
22

33
import java.util.Arrays;
44
import java.util.List;
55
import java.util.function.*;
66
import java.util.stream.DoubleStream;
77
import java.util.stream.IntStream;
88
import java.util.stream.LongStream;
9-
import lombok.extern.slf4j.Slf4j;
109
import org.junit.jupiter.api.Assertions;
1110
import org.junit.jupiter.api.Test;
1211

13-
@Slf4j
1412
public class PredicateTest {
1513
// C = Carpenter, W = Welder
1614
private final Object[][] workers = {

0 commit comments

Comments
 (0)