Skip to content

Commit 29fe86e

Browse files
committed
Tupler2: create a tuple from 2 inputs
1 parent 471a93c commit 29fe86e

4 files changed

Lines changed: 38 additions & 9 deletions

File tree

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
package com.jnape.palatable.lambda.builtin.dyadic;
2+
3+
import com.jnape.palatable.lambda.DyadicFunction;
4+
import com.jnape.palatable.lambda.tuples.Tuple2;
5+
6+
import static com.jnape.palatable.lambda.tuples.Tuple2.tuple;
7+
8+
public final class Tupler2<A, B> extends DyadicFunction<A, B, Tuple2<A, B>> {
9+
10+
@Override
11+
public final Tuple2<A, B> apply(A a, B b) {
12+
return tuple(a, b);
13+
}
14+
15+
public static <A, B> Tupler2<A, B> tupler() {
16+
return new Tupler2<A, B>();
17+
}
18+
}

src/main/java/com/jnape/palatable/lambda/builtin/monadic/Identity.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,10 +2,10 @@
22

33
import com.jnape.palatable.lambda.MonadicFunction;
44

5-
public class Identity<A> extends MonadicFunction<A, A> {
5+
public final class Identity<A> extends MonadicFunction<A, A> {
66

77
@Override
8-
public A apply(A a) {
8+
public final A apply(A a) {
99
return a;
1010
}
1111

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

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,21 +2,16 @@
22

33
import com.jnape.palatable.lambda.DyadicFunction;
44
import com.jnape.palatable.lambda.MonadicFunction;
5+
import com.jnape.palatable.lambda.builtin.dyadic.Tupler2;
56
import com.jnape.palatable.lambda.tuples.Tuple2;
67

78
import static com.jnape.palatable.lambda.functions.ZipWith.zipWith;
8-
import static com.jnape.palatable.lambda.tuples.Tuple2.tuple;
99

1010
public final class Zip<A, B> extends DyadicFunction<Iterable<A>, Iterable<B>, Iterable<Tuple2<A, B>>> {
1111

1212
@Override
1313
public final Iterable<Tuple2<A, B>> apply(final Iterable<A> as, final Iterable<B> bs) {
14-
return zipWith(new DyadicFunction<A, B, Tuple2<A, B>>() {
15-
@Override
16-
public Tuple2<A, B> apply(A a, B b) {
17-
return tuple(a, b);
18-
}
19-
}, as, bs);
14+
return zipWith(Tupler2.<A, B>tupler(), as, bs);
2015
}
2116

2217
public static <A, B> DyadicFunction<Iterable<A>, Iterable<B>, Iterable<Tuple2<A, B>>> zip() {
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
package com.jnape.palatable.lambda.builtin.dyadic;
2+
3+
import org.junit.Test;
4+
5+
import static com.jnape.palatable.lambda.tuples.Tuple2.tuple;
6+
import static org.hamcrest.core.Is.is;
7+
import static org.junit.Assert.assertThat;
8+
9+
public class Tupler2Test {
10+
11+
@Test
12+
public void createsTupleOfTwoThings() {
13+
Tupler2<String, Integer> tupler = new Tupler2<String, Integer>();
14+
assertThat(tupler.apply("a", 1), is(tuple("a", 1)));
15+
}
16+
}

0 commit comments

Comments
 (0)