Skip to content

Commit 25a7e0f

Browse files
committed
adding adapt methods for Fn1 and Fn2 from Function and BiFunction, respectively
1 parent 993c322 commit 25a7e0f

4 files changed

Lines changed: 41 additions & 0 deletions

File tree

src/main/java/com/jnape/palatable/lambda/functions/Fn1.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -113,4 +113,17 @@ default <Z> Fn1<Z, B> compose(Function<? super Z, ? extends A> before) {
113113
default <C> Fn1<A, C> andThen(Function<? super B, ? extends C> after) {
114114
return a -> after.apply(apply(a));
115115
}
116+
117+
/**
118+
* Static factory method for wrapping a {@link Function} in an {@link Fn1}. Useful for avoid explicit casting when
119+
* using method references as {@link Fn1}s.
120+
*
121+
* @param function the function to adapt
122+
* @param <A> the input argument type
123+
* @param <B> the output type
124+
* @return the Fn1
125+
*/
126+
static <A, B> Fn1<A, B> adapt(Function<A, B> function) {
127+
return function::apply;
128+
}
116129
}

src/main/java/com/jnape/palatable/lambda/functions/Fn2.java

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,4 +65,18 @@ default Fn1<Tuple2<A, B>, C> uncurry() {
6565
default BiFunction<A, B, C> toBiFunction() {
6666
return this::apply;
6767
}
68+
69+
/**
70+
* Static factory method for wrapping a {@link BiFunction} in an {@link Fn2}. Useful for avoid explicit casting when
71+
* using method references as {@link Fn2}s.
72+
*
73+
* @param biFunction the biFunction to adapt
74+
* @param <A> the first input argument type
75+
* @param <B> the second input argument type
76+
* @param <C> the output type
77+
* @return the Fn2
78+
*/
79+
static <A, B, C> Fn2<A, B, C> adapt(BiFunction<A, B, C> biFunction) {
80+
return biFunction::apply;
81+
}
6882
}

src/test/java/com/jnape/palatable/lambda/functions/Fn1Test.java

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
import org.hamcrest.MatcherAssert;
44
import org.junit.Test;
55

6+
import java.util.function.Function;
7+
68
import static org.hamcrest.core.Is.is;
79
import static org.junit.Assert.assertEquals;
810

@@ -32,4 +34,10 @@ public void thenIsJustAnAliasForFmap() {
3234

3335
MatcherAssert.assertThat(add2.then(toString).apply(2), is(toString.apply(add2.apply(2))));
3436
}
37+
38+
@Test
39+
public void adapt() {
40+
Function<String, Integer> parseInt = Integer::parseInt;
41+
assertEquals((Integer) 1, Fn1.adapt(parseInt).apply("1"));
42+
}
3543
}

src/test/java/com/jnape/palatable/lambda/functions/Fn2Test.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,4 +34,10 @@ public void toBiFunction() {
3434
BiFunction<String, Integer, Boolean> biFunction = CHECK_LENGTH.toBiFunction();
3535
assertEquals(true, biFunction.apply("abc", 3));
3636
}
37+
38+
@Test
39+
public void adapt() {
40+
BiFunction<String, String, String> format = String::format;
41+
assertEquals("foo bar", Fn2.adapt(format).apply("foo %s", "bar"));
42+
}
3743
}

0 commit comments

Comments
 (0)