Skip to content

Commit 8ac2620

Browse files
committed
Adding $, function application represented as a higher-order Fn2
1 parent 6748e7b commit 8ac2620

File tree

3 files changed

+66
-1
lines changed
  • src
    • main/java/com/jnape/palatable/lambda/functions/builtin/fn2
    • test/java/com/jnape/palatable/lambda/functions/builtin/fn2

3 files changed

+66
-1
lines changed

CHANGELOG.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,8 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
55

66
## [Unreleased]
77

8-
There are currently no unreleased changes
8+
### Added
9+
- `$`, function application represented as a higher-order `Fn2`
910

1011
## [5.2.0] - 2020-02-12
1112

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
package com.jnape.palatable.lambda.functions.builtin.fn2;
2+
3+
import com.jnape.palatable.lambda.functions.Fn1;
4+
import com.jnape.palatable.lambda.functions.Fn2;
5+
6+
/**
7+
* Function application, represented as a higher-order {@link Fn2} that receives an {@link Fn1} and its argument, and
8+
* applies it. Useful for treating application as a combinator, e.g.:
9+
* <pre>
10+
* {@code
11+
* List<Fn1<Integer, Integer>> fns = asList(x -> x + 1, x -> x, x -> x - 1);
12+
* List<Integer> args = asList(0, 1, 2);
13+
* Iterable<Integer> results = zipWith($(), fns, args); // [1, 1, 1]
14+
* }
15+
* </pre>
16+
*
17+
* @param <A> the applied {@link Fn1 Fn1's} input type
18+
* @param <B> the applied {@link Fn1 Fn1's} output type
19+
*/
20+
public final class $<A, B> implements Fn2<Fn1<? super A, ? extends B>, A, B> {
21+
private static final $<?, ?> INSTANCE = new $<>();
22+
23+
private $() {
24+
}
25+
26+
@Override
27+
public B checkedApply(Fn1<? super A, ? extends B> fn, A a) {
28+
return fn.apply(a);
29+
}
30+
31+
@SuppressWarnings("unchecked")
32+
public static <A, B> $<A, B> $() {
33+
return ($<A, B>) INSTANCE;
34+
}
35+
36+
public static <A, B> Fn1<A, B> $(Fn1<? super A, ? extends B> fn) {
37+
return $.<A, B>$().apply(fn);
38+
}
39+
40+
public static <A, B> B $(Fn1<? super A, ? extends B> fn, A a) {
41+
return $.<A, B>$(fn).apply(a);
42+
}
43+
}
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.fn2;
2+
3+
import org.junit.Test;
4+
5+
import static com.jnape.palatable.lambda.functions.Fn2.fn2;
6+
import static com.jnape.palatable.lambda.functions.builtin.fn2.$.$;
7+
import static org.junit.Assert.assertEquals;
8+
9+
public class $Test {
10+
11+
@Test
12+
public void application() {
13+
assertEquals((Integer) 1, $(x -> x + 1, 0));
14+
assertEquals((Integer) 1, $.<Integer, Integer>$(x -> x + 1).apply(0));
15+
}
16+
17+
@Test
18+
public void curryingInference() {
19+
assertEquals((Integer) 1, $($(fn2(Integer::sum), 0), 1));
20+
}
21+
}

0 commit comments

Comments
 (0)