Skip to content

Commit 124a44b

Browse files
committed
Fn1#widen, (a -> b) -> (z -> a -> b)
1 parent 3490ab1 commit 124a44b

File tree

9 files changed

+75
-0
lines changed

9 files changed

+75
-0
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/).
2323
- `Occurrences`, for counting the occurrences of the members of an `Iterable`
2424
- `Effect`, an `Fn0` returning `UNIT`
2525
- `Noop`, a no-op `Effect`
26+
- `Fn1#widen`, add an ignored argument to the beginning of any function to raise its arity by one
2627

2728
### Changed
2829
- `Tuple2-8` now implement `Product2-8`

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

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import java.util.function.Function;
99

1010
import static com.jnape.palatable.lambda.functions.Fn2.fn2;
11+
import static com.jnape.palatable.lambda.functions.builtin.fn1.Constantly.constantly;
1112

1213
/**
1314
* A function taking a single argument. This is the core function type that all other function types extend and
@@ -38,6 +39,16 @@ default Fn0<B> thunk(A a) {
3839
return __ -> apply(a);
3940
}
4041

42+
/**
43+
* Widen this function's argument list by prepending an ignored argument of any type to the front.
44+
*
45+
* @param <Z> the new first argument type
46+
* @return the widened function
47+
*/
48+
default <Z> Fn2<Z, A, B> widen() {
49+
return fn2(constantly(this));
50+
}
51+
4152
@Override
4253
default <C> Fn1<A, C> flatMap(Function<? super B, ? extends Monad<C, Fn1<A, ?>>> f) {
4354
return a -> f.apply(apply(a)).<Fn1<A, C>>coerce().apply(a);

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.util.function.Function;
88

99
import static com.jnape.palatable.lambda.functions.Fn3.fn3;
10+
import static com.jnape.palatable.lambda.functions.builtin.fn1.Constantly.constantly;
1011

1112
/**
1213
* A function taking two arguments.
@@ -31,6 +32,14 @@ public interface Fn2<A, B, C> extends Fn1<A, Fn1<B, C>> {
3132
*/
3233
C apply(A a, B b);
3334

35+
/**
36+
* @inheritDoc
37+
*/
38+
@Override
39+
default <Z> Fn3<Z, A, B, C> widen() {
40+
return fn3(constantly(this));
41+
}
42+
3443
/**
3544
* Same as normal composition, except that the result is an instance of {@link Fn2} for convenience.
3645
*

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.util.function.Function;
88

99
import static com.jnape.palatable.lambda.functions.Fn4.fn4;
10+
import static com.jnape.palatable.lambda.functions.builtin.fn1.Constantly.constantly;
1011

1112
/**
1213
* A function taking three arguments. Defined in terms of {@link Fn2}, so similarly auto-curried.
@@ -30,6 +31,14 @@ public interface Fn3<A, B, C, D> extends Fn2<A, B, Fn1<C, D>> {
3031
*/
3132
D apply(A a, B b, C c);
3233

34+
/**
35+
* @inheritDoc
36+
*/
37+
@Override
38+
default <Z> Fn4<Z, A, B, C, D> widen() {
39+
return fn4(constantly(this));
40+
}
41+
3342
/**
3443
* Partially apply this function by taking its first argument.
3544
*

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.util.function.Function;
88

99
import static com.jnape.palatable.lambda.functions.Fn5.fn5;
10+
import static com.jnape.palatable.lambda.functions.builtin.fn1.Constantly.constantly;
1011

1112
/**
1213
* A function taking four arguments. Defined in terms of {@link Fn3}, so similarly auto-curried.
@@ -32,6 +33,14 @@ public interface Fn4<A, B, C, D, E> extends Fn3<A, B, C, Fn1<D, E>> {
3233
*/
3334
E apply(A a, B b, C c, D d);
3435

36+
/**
37+
* @inheritDoc
38+
*/
39+
@Override
40+
default <Z> Fn5<Z, A, B, C, D, E> widen() {
41+
return fn5(constantly(this));
42+
}
43+
3544
/**
3645
* Partially apply this function by taking its first argument.
3746
*

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.util.function.Function;
88

99
import static com.jnape.palatable.lambda.functions.Fn6.fn6;
10+
import static com.jnape.palatable.lambda.functions.builtin.fn1.Constantly.constantly;
1011

1112
/**
1213
* A function taking five arguments. Defined in terms of {@link Fn4}, so similarly auto-curried.
@@ -34,6 +35,14 @@ public interface Fn5<A, B, C, D, E, F> extends Fn4<A, B, C, D, Fn1<E, F>> {
3435
*/
3536
F apply(A a, B b, C c, D d, E e);
3637

38+
/**
39+
* @inheritDoc
40+
*/
41+
@Override
42+
default <Z> Fn6<Z, A, B, C, D, E, F> widen() {
43+
return fn6(constantly(this));
44+
}
45+
3746
/**
3847
* Partially apply this function by taking its first argument.
3948
*

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.util.function.Function;
88

99
import static com.jnape.palatable.lambda.functions.Fn7.fn7;
10+
import static com.jnape.palatable.lambda.functions.builtin.fn1.Constantly.constantly;
1011

1112
/**
1213
* A function taking six arguments. Defined in terms of {@link Fn5}, so similarly auto-curried.
@@ -36,6 +37,14 @@ public interface Fn6<A, B, C, D, E, F, G> extends Fn5<A, B, C, D, E, Fn1<F, G>>
3637
*/
3738
G apply(A a, B b, C c, D d, E e, F f);
3839

40+
/**
41+
* @inheritDoc
42+
*/
43+
@Override
44+
default <Z> Fn7<Z, A, B, C, D, E, F, G> widen() {
45+
return fn7(constantly(this));
46+
}
47+
3948
/**
4049
* Partially apply this function by taking its first argument.
4150
*

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
import java.util.function.Function;
88

99
import static com.jnape.palatable.lambda.functions.Fn8.fn8;
10+
import static com.jnape.palatable.lambda.functions.builtin.fn1.Constantly.constantly;
1011

1112
/**
1213
* A function taking six arguments. Defined in terms of {@link Fn6}, so similarly auto-curried.
@@ -38,6 +39,14 @@ public interface Fn7<A, B, C, D, E, F, G, H> extends Fn6<A, B, C, D, E, F, Fn1<G
3839
*/
3940
H apply(A a, B b, C c, D d, E e, F f, G g);
4041

42+
/**
43+
* @inheritDoc
44+
*/
45+
@Override
46+
default <Z> Fn8<Z, A, B, C, D, E, F, G, H> widen() {
47+
return fn8(constantly(this));
48+
}
49+
4150
/**
4251
* Partially apply this function by taking its first argument.
4352
*

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,9 @@
1111

1212
import java.util.function.Function;
1313

14+
import static com.jnape.palatable.lambda.adt.Maybe.just;
15+
import static com.jnape.palatable.lambda.functions.builtin.fn2.ReduceLeft.reduceLeft;
16+
import static java.util.Arrays.asList;
1417
import static org.junit.Assert.assertEquals;
1518

1619
@RunWith(Traits.class)
@@ -41,4 +44,10 @@ public void thunk() {
4144
Fn1<Integer, String> toString = Object::toString;
4245
assertEquals("1", toString.thunk(1).apply());
4346
}
47+
48+
@Test
49+
public void widen() {
50+
Fn1<Integer, Integer> addOne = x -> x + 1;
51+
assertEquals(just(4), reduceLeft(addOne.widen().toBiFunction(), asList(1, 2, 3)));
52+
}
4453
}

0 commit comments

Comments
 (0)