Skip to content

Commit 5d451d9

Browse files
committed
Lens#both for dually focusing with two lenses at once
1 parent c72ef91 commit 5d451d9

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
2424
- `MagnetizeBy` and `Magnetize`, for grouping elements by pairwise predicate tests
2525
- `Both`, for dually applying two functions and producing a `Tuple2` of their results
2626
- `Lens#both`, for dually focusing with two lenses at once
27+
- `IfThenElse`, an expression form for `if` statements
2728

2829
### Deprecated
2930
- `Either#trying` in favor of `Try#trying`
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.jnape.palatable.lambda.functions.builtin.fn4;
2+
3+
import com.jnape.palatable.lambda.functions.Fn1;
4+
import com.jnape.palatable.lambda.functions.Fn2;
5+
import com.jnape.palatable.lambda.functions.Fn3;
6+
import com.jnape.palatable.lambda.functions.Fn4;
7+
8+
import java.util.function.Function;
9+
10+
public final class IfThenElse<A, B> implements Fn4<Function<? super A, Boolean>, Function<? super A, ? extends B>, Function<? super A, ? extends B>, A, B> {
11+
12+
private static final IfThenElse INSTANCE = new IfThenElse();
13+
14+
private IfThenElse() {
15+
}
16+
17+
@Override
18+
public B apply(Function<? super A, Boolean> predicate, Function<? super A, ? extends B> thenCase,
19+
Function<? super A, ? extends B> elseCase, A a) {
20+
return predicate.apply(a) ? thenCase.apply(a) : elseCase.apply(a);
21+
}
22+
23+
@SuppressWarnings("unchecked")
24+
public static <A, B> IfThenElse<A, B> ifThenElse() {
25+
return INSTANCE;
26+
}
27+
28+
public static <A, B> Fn3<Function<? super A, ? extends B>, Function<? super A, ? extends B>, A, B> ifThenElse(
29+
Function<? super A, Boolean> predicate) {
30+
return IfThenElse.<A, B>ifThenElse().apply(predicate);
31+
}
32+
33+
public static <A, B> Fn2<Function<? super A, ? extends B>, A, B> ifThenElse(
34+
Function<? super A, Boolean> predicate, Function<? super A, ? extends B> thenCase) {
35+
return IfThenElse.<A, B>ifThenElse(predicate).apply(thenCase);
36+
}
37+
38+
public static <A, B> Fn1<A, B> ifThenElse(
39+
Function<? super A, Boolean> predicate, Function<? super A, ? extends B> thenCase,
40+
Function<? super A, ? extends B> elseCase) {
41+
return IfThenElse.<A, B>ifThenElse(predicate, thenCase).apply(elseCase);
42+
}
43+
44+
public static <A, B> B ifThenElse(
45+
Function<? super A, Boolean> predicate, Function<? super A, ? extends B> thenCase,
46+
Function<? super A, ? extends B> elseCase,
47+
A a) {
48+
return ifThenElse(predicate, thenCase, elseCase).apply(a);
49+
}
50+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.jnape.palatable.lambda.functions.builtin.fn4;
2+
3+
import com.jnape.palatable.lambda.functions.Fn1;
4+
import com.jnape.palatable.lambda.functions.specialized.Predicate;
5+
import org.junit.Test;
6+
7+
import static com.jnape.palatable.lambda.functions.builtin.fn4.IfThenElse.ifThenElse;
8+
import static org.junit.Assert.assertEquals;
9+
10+
public class IfThenElseTest {
11+
12+
@Test
13+
public void standardLogic() {
14+
Predicate<Integer> even = x -> x % 2 == 0;
15+
Fn1<Integer, Integer> inc = x -> x + 1;
16+
Fn1<Integer, Integer> dec = x -> x - 1;
17+
18+
assertEquals((Integer) 3, ifThenElse(even, inc, dec, 2));
19+
assertEquals((Integer) 0, ifThenElse(even, inc, dec, 1));
20+
}
21+
}

0 commit comments

Comments
 (0)