Skip to content

Commit e84c8e3

Browse files
committed
Product2 implements Map.Entry, Into takes Map.Entry
1 parent 8cfcc2d commit e84c8e3

File tree

4 files changed

+30
-13
lines changed

4 files changed

+30
-13
lines changed

CHANGELOG.md

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

1616
### Changed
1717
- `Tuple2-8` now implement `Product2-8`
18-
- `Into2-8` now accept a product of the same cardinality, instead of requiring a tuple
18+
- `Into` now accepts `Map.Entry`
19+
- `Into3-8` now accept a product of the same cardinality, instead of requiring a tuple
1920
- `CoProduct2-8#project` now return generalized products
2021
- `Choice2-8#project` return tuples
2122

src/main/java/com/jnape/palatable/lambda/adt/product/Product2.java

Lines changed: 17 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import com.jnape.palatable.lambda.adt.hlist.Tuple2;
44

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

78
/**
@@ -14,7 +15,7 @@
1415
* @param <_2> The second element type
1516
* @see Tuple2
1617
*/
17-
public interface Product2<_1, _2> {
18+
public interface Product2<_1, _2> extends Map.Entry<_1, _2> {
1819

1920
/**
2021
* Retrieve the first element.
@@ -41,4 +42,19 @@ public interface Product2<_1, _2> {
4142
default <R> R into(BiFunction<? super _1, ? super _2, ? extends R> fn) {
4243
return fn.apply(_1(), _2());
4344
}
45+
46+
@Override
47+
default _1 getKey() {
48+
return _1();
49+
}
50+
51+
@Override
52+
default _2 getValue() {
53+
return _2();
54+
}
55+
56+
@Override
57+
default _2 setValue(_2 value) {
58+
throw new UnsupportedOperationException();
59+
}
4460
}
Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,43 +1,43 @@
11
package com.jnape.palatable.lambda.functions.builtin.fn2;
22

3-
import com.jnape.palatable.lambda.adt.product.Product2;
43
import com.jnape.palatable.lambda.functions.Fn1;
54
import com.jnape.palatable.lambda.functions.Fn2;
65

6+
import java.util.Map;
77
import java.util.function.BiFunction;
88

99
/**
10-
* Given a <code>{@link BiFunction}&lt;A, B, C&gt;</code> and a <code>{@link Product2}&lt;A, B&gt;</code>, destructure
11-
* the product and apply the slots as arguments to the function, returning the result.
10+
* Given a <code>{@link BiFunction}&lt;A, B, C&gt;</code> and a <code>{@link Map.Entry}&lt;A, B&gt;</code>, destructure
11+
* the entry and apply the key and value as arguments to the function, returning the result.
1212
*
1313
* @param <A> the first argument type
1414
* @param <B> the second argument type
1515
* @param <C> the result type
1616
*/
17-
public final class Into<A, B, C> implements Fn2<BiFunction<? super A, ? super B, ? extends C>, Product2<A, B>, C> {
17+
public final class Into<A, B, C> implements Fn2<BiFunction<? super A, ? super B, ? extends C>, Map.Entry<A, B>, C> {
1818

1919
private static final Into INSTANCE = new Into();
2020

2121
private Into() {
2222
}
2323

2424
@Override
25-
public C apply(BiFunction<? super A, ? super B, ? extends C> fn, Product2<A, B> product) {
26-
return product.into(fn);
25+
public C apply(BiFunction<? super A, ? super B, ? extends C> fn, Map.Entry<A, B> entry) {
26+
return fn.apply(entry.getKey(), entry.getValue());
2727
}
2828

2929
@SuppressWarnings("unchecked")
3030
public static <A, B, C> Into<A, B, C> into() {
3131
return INSTANCE;
3232
}
3333

34-
public static <A, B, C> Fn1<Product2<A, B>, C> into(
34+
public static <A, B, C> Fn1<Map.Entry<A, B>, C> into(
3535
BiFunction<? super A, ? super B, ? extends C> fn) {
3636
return Into.<A, B, C>into().apply(fn);
3737
}
3838

3939
public static <A, B, C> C into(BiFunction<? super A, ? super B, ? extends C> fn,
40-
Product2<A, B> product) {
41-
return Into.<A, B, C>into(fn).apply(product);
40+
Map.Entry<A, B> entry) {
41+
return Into.<A, B, C>into(fn).apply(entry);
4242
}
4343
}

src/test/java/com/jnape/palatable/lambda/functions/recursion/TrampolineTest.java

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

33
import com.jnape.palatable.lambda.adt.hlist.Tuple2;
4-
import com.jnape.palatable.lambda.adt.product.Product2;
54
import org.junit.Test;
65

76
import java.math.BigInteger;
7+
import java.util.Map;
88
import java.util.function.Function;
99

1010
import static com.jnape.palatable.lambda.adt.hlist.HList.tuple;
@@ -17,7 +17,7 @@
1717

1818
public class TrampolineTest {
1919

20-
private static final Function<Product2<BigInteger, BigInteger>, RecursiveResult<Tuple2<BigInteger, BigInteger>, BigInteger>> FACTORIAL =
20+
private static final Function<Map.Entry<BigInteger, BigInteger>, RecursiveResult<Tuple2<BigInteger, BigInteger>, BigInteger>> FACTORIAL =
2121
into((x, acc) -> x.compareTo(ONE) > 0 ? recurse(tuple(x.subtract(ONE), x.multiply(acc))) : terminate(acc));
2222

2323
@Test

0 commit comments

Comments
 (0)