Skip to content

Commit 1a09821

Browse files
committed
Adding Fn3/Fn4 static factory methods
1 parent 57d9fba commit 1a09821

5 files changed

Lines changed: 129 additions & 1 deletion

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
66
## [Unreleased]
77
### Added
88
- `Tuple6` through `Tuple8`
9+
- `Fn3#fn3` and `Fn4#fn4` static factory methods
910

1011
## [2.0.0] - 2017-11-13
1112
### Changed

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,10 +61,38 @@ default Fn3<B, A, C, D> flip() {
6161
* Returns an <code>Fn2</code> that takes the first two arguments as a <code>Tuple2&lt;A, B&gt;</code> and the third
6262
* argument.
6363
*
64-
* @return an Fn2 taking a Tuple2 and the third argument
64+
* @return an Fn2 taking a Tuple2 `and the third argument
6565
*/
6666
@Override
6767
default Fn2<Tuple2<A, B>, C, D> uncurry() {
6868
return (ab, c) -> apply(ab._1(), ab._2(), c);
6969
}
70+
71+
/**
72+
* Static factory method for wrapping a curried {@link Fn1} in an {@link Fn3}.
73+
*
74+
* @param curriedFn1 the curried fn1 to adapt
75+
* @param <A> the first input argument type
76+
* @param <B> the second input argument type
77+
* @param <C> the third input argument type
78+
* @param <D> the output type
79+
* @return the Fn3
80+
*/
81+
static <A, B, C, D> Fn3<A, B, C, D> fn3(Fn1<A, Fn2<B, C, D>> curriedFn1) {
82+
return (a, b, c) -> curriedFn1.apply(a).apply(b, c);
83+
}
84+
85+
/**
86+
* Static factory method for wrapping a curried {@link Fn2} in an {@link Fn3}.
87+
*
88+
* @param curriedFn2 the curried fn2 to adapt
89+
* @param <A> the first input argument type
90+
* @param <B> the second input argument type
91+
* @param <C> the third input argument type
92+
* @param <D> the output type
93+
* @return the Fn3
94+
*/
95+
static <A, B, C, D> Fn3<A, B, C, D> fn3(Fn2<A, B, Fn1<C, D>> curriedFn2) {
96+
return (a, b, c) -> curriedFn2.apply(a, b).apply(c);
97+
}
7098
}

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

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,4 +82,49 @@ default Fn4<B, A, C, D, E> flip() {
8282
default Fn3<Tuple2<A, B>, C, D, E> uncurry() {
8383
return (ab, c, d) -> apply(ab._1(), ab._2(), c, d);
8484
}
85+
86+
/**
87+
* Static factory method for wrapping a curried {@link Fn1} in an {@link Fn4}.
88+
*
89+
* @param curriedFn1 the curried fn1 to adapt
90+
* @param <A> the first input argument type
91+
* @param <B> the second input argument type
92+
* @param <C> the third input argument type
93+
* @param <D> the fourth input argument type
94+
* @param <E> the output type
95+
* @return the Fn4
96+
*/
97+
static <A, B, C, D, E> Fn4<A, B, C, D, E> fn4(Fn1<A, Fn3<B, C, D, E>> curriedFn1) {
98+
return (a, b, c, d) -> curriedFn1.apply(a).apply(b, c, d);
99+
}
100+
101+
/**
102+
* Static factory method for wrapping a curried {@link Fn2} in an {@link Fn4}.
103+
*
104+
* @param curriedFn2 the curried fn2 to adapt
105+
* @param <A> the first input argument type
106+
* @param <B> the second input argument type
107+
* @param <C> the third input argument type
108+
* @param <D> the fourth input argument type
109+
* @param <E> the output type
110+
* @return the Fn4
111+
*/
112+
static <A, B, C, D, E> Fn4<A, B, C, D, E> fn4(Fn2<A, B, Fn2<C, D, E>> curriedFn2) {
113+
return (a, b, c, d) -> curriedFn2.apply(a, b).apply(c, d);
114+
}
115+
116+
/**
117+
* Static factory method for wrapping a curried {@link Fn3} in an {@link Fn4}.
118+
*
119+
* @param curriedFn3 the curried fn3 to adapt
120+
* @param <A> the first input argument type
121+
* @param <B> the second input argument type
122+
* @param <C> the third input argument type
123+
* @param <D> the fourth input argument type
124+
* @param <E> the output type
125+
* @return the Fn4
126+
*/
127+
static <A, B, C, D, E> Fn4<A, B, C, D, E> fn4(Fn3<A, B, C, Fn1<D, E>> curriedFn3) {
128+
return (a, b, c, d) -> curriedFn3.apply(a, b, c).apply(d);
129+
}
85130
}

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import static com.jnape.palatable.lambda.adt.hlist.HList.tuple;
66
import static org.hamcrest.core.Is.is;
7+
import static org.junit.Assert.assertEquals;
78
import static org.junit.Assert.assertThat;
89

910
public class Fn3Test {
@@ -28,4 +29,13 @@ public void flipsFirstAndSecondArgument() {
2829
public void uncurries() {
2930
assertThat(CHECK_MULTIPLICATION.uncurry().apply(tuple(2, 3), 6), is(true));
3031
}
32+
33+
@Test
34+
public void fn3() {
35+
Fn1<String, Fn2<String, String, String>> fn1 = a -> (b, c) -> a + b + c;
36+
assertEquals("abc", Fn3.fn3(fn1).apply("a", "b", "c"));
37+
38+
Fn2<String, String, Fn1<String, String>> fn2 = (a, b) -> c -> a + b + c;
39+
assertEquals("abc", Fn3.fn3(fn2).apply("a", "b", "c"));
40+
}
3141
}
Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.jnape.palatable.lambda.functions;
2+
3+
import org.junit.Test;
4+
5+
import static com.jnape.palatable.lambda.adt.hlist.HList.tuple;
6+
import static org.hamcrest.core.Is.is;
7+
import static org.junit.Assert.assertEquals;
8+
import static org.junit.Assert.assertThat;
9+
10+
public class Fn4Test {
11+
12+
private static final Fn4<String, String, String, String, String> APPEND =
13+
(s1, s2, s3, s4) -> s1 + s2 + s3 + s4;
14+
15+
@Test
16+
public void canBePartiallyApplied() {
17+
assertThat(APPEND.apply("a").apply("b").apply("c").apply("d"), is("abcd"));
18+
assertThat(APPEND.apply("a").apply("b").apply("c", "d"), is("abcd"));
19+
assertThat(APPEND.apply("a").apply("b", "c", "d"), is("abcd"));
20+
assertThat(APPEND.apply("a", "b", "c", "d"), is("abcd"));
21+
}
22+
23+
@Test
24+
public void flipsFirstAndSecondArgument() {
25+
assertThat(APPEND.flip().apply("a", "b", "c", "d"), is("bacd"));
26+
}
27+
28+
@Test
29+
public void uncurries() {
30+
assertThat(APPEND.uncurry().apply(tuple("a", "b"), "c", "d"), is("abcd"));
31+
}
32+
33+
@Test
34+
public void fn4() {
35+
Fn1<String, Fn3<String, String, String, String>> fn1 = a -> (b, c, d) -> a + b + c + d;
36+
assertEquals("abcd", Fn4.fn4(fn1).apply("a", "b", "c", "d"));
37+
38+
Fn2<String, String, Fn2<String, String, String>> fn2 = (a, b) -> (c, d) -> a + b + c + d;
39+
assertEquals("abcd", Fn4.fn4(fn2).apply("a", "b", "c", "d"));
40+
41+
Fn3<String, String, String, Fn1<String, String>> fn3 = (a, b, c) -> (d) -> a + b + c + d;
42+
assertEquals("abcd", Fn4.fn4(fn3).apply("a", "b", "c", "d"));
43+
}
44+
}

0 commit comments

Comments
 (0)