Skip to content

Commit c78ed71

Browse files
committed
adding Fn2#curry
1 parent c0dc472 commit c78ed71

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
2525
- `IO#memoize`, for memoizing an `IO` by caching its first successful result
2626
- `MonadError`, monads that can be thrown to and caught from
2727
- `Tuple2-8#fromIterable`, for populating a `TupleN` with the first `N` elements of an `Iterable`
28+
- `Fn2#curry`, for converting an `Fn1<Tuple2<A,B>,C>` to an `Fn2<A,B,C>`
2829

2930
## [4.0.0] - 2019-05-20
3031
### Changed

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

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,13 @@
11
package com.jnape.palatable.lambda.functions;
22

3+
import com.jnape.palatable.lambda.adt.hlist.Tuple2;
34
import com.jnape.palatable.lambda.adt.product.Product2;
45
import com.jnape.palatable.lambda.functor.Applicative;
56
import com.jnape.palatable.lambda.internal.Runtime;
67

78
import java.util.function.BiFunction;
89

10+
import static com.jnape.palatable.lambda.adt.hlist.HList.tuple;
911
import static com.jnape.palatable.lambda.functions.Fn3.fn3;
1012
import static com.jnape.palatable.lambda.functions.builtin.fn1.Constantly.constantly;
1113

@@ -153,6 +155,19 @@ static <A, B, C> Fn2<A, B, C> curried(Fn1<A, Fn1<B, C>> curriedFn1) {
153155
return (a, b) -> curriedFn1.apply(a).apply(b);
154156
}
155157

158+
/**
159+
* Static factory method for wrapping an uncurried {@link Fn1} in an {@link Fn2}.
160+
*
161+
* @param uncurriedFn1 the uncurried {@link Fn1} to adapt
162+
* @param <A> the first input argument type
163+
* @param <B> the second input argument type
164+
* @param <C> the output type
165+
* @return the {@link Fn2}
166+
*/
167+
static <A, B, C> Fn2<A, B, C> curry(Fn1<? super Tuple2<A, B>, ? extends C> uncurriedFn1) {
168+
return (a, b) -> uncurriedFn1.apply(tuple(a, b));
169+
}
170+
156171
/**
157172
* Static method to aid inference.
158173
*

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

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22

33
import org.junit.Test;
44

5+
import java.util.Map;
56
import java.util.function.BiFunction;
67

78
import static com.jnape.palatable.lambda.adt.hlist.HList.tuple;
9+
import static com.jnape.palatable.lambda.functions.builtin.fn2.Into.into;
810
import static org.hamcrest.core.Is.is;
911
import static org.junit.Assert.assertEquals;
1012
import static org.junit.Assert.assertThat;
@@ -52,4 +54,10 @@ public void fromBiFunction() {
5254
BiFunction<String, String, String> biFunction = String::format;
5355
assertEquals("foo bar", Fn2.fromBiFunction(biFunction).apply("foo %s", "bar"));
5456
}
57+
58+
@Test
59+
public void curry() {
60+
Fn1<Map.Entry<String, String>, String> uncurried = into((a, b) -> a + b);
61+
assertEquals("foobar", Fn2.curry(uncurried).apply("foo", "bar"));
62+
}
5563
}

0 commit comments

Comments
 (0)