Skip to content

Commit 53aa865

Browse files
committed
Adding test dependency on commons-lang3 for reflectionEquals/HashCode/ToString
zip/zipWith: Adding ZippingIterable, DyadicFunction and Tuple2
1 parent 456a319 commit 53aa865

7 files changed

Lines changed: 175 additions & 1 deletion

File tree

‎pom.xml‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,10 @@
4343
</developer>
4444
</developers>
4545

46+
<properties>
47+
<commons-lang3.version>3.1</commons-lang3.version>
48+
</properties>
49+
4650
<dependencies>
4751
<dependency>
4852
<groupId>junit</groupId>
@@ -52,6 +56,12 @@
5256
<groupId>org.mockito</groupId>
5357
<artifactId>mockito-all</artifactId>
5458
</dependency>
59+
<dependency>
60+
<groupId>org.apache.commons</groupId>
61+
<artifactId>commons-lang3</artifactId>
62+
<version>${commons-lang3.version}</version>
63+
<scope>test</scope>
64+
</dependency>
5565
</dependencies>
5666

5767
</project>
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package com.jnape.palatable.lambda;
2+
3+
import com.jnape.palatable.lambda.tuples.Tuple2;
4+
5+
public abstract class DyadicFunction<A, B, C> extends MonadicFunction<Tuple2<A, B>, C> {
6+
@Override
7+
public final C apply(Tuple2<A, B> args) {
8+
return apply(args._1, args._2);
9+
}
10+
11+
public abstract C apply(A a, B b);
12+
}
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
package com.jnape.palatable.lambda.iterables;
2+
3+
import com.jnape.palatable.lambda.DyadicFunction;
4+
import com.jnape.palatable.lambda.iterators.ImmutableIterator;
5+
import com.jnape.palatable.lambda.tuples.Tuple2;
6+
7+
import java.util.Iterator;
8+
9+
import static com.jnape.palatable.lambda.tuples.Tuple2.tuple;
10+
11+
public class ZippingIterable<A, B, C> implements Iterable<C> {
12+
13+
private final DyadicFunction<? super A, ? super B, ? extends C> zipper;
14+
private final Iterable<A> as;
15+
private final Iterable<B> bs;
16+
17+
public ZippingIterable(DyadicFunction<? super A, ? super B, ? extends C> zipper, Iterable<A> as,
18+
Iterable<B> bs) {
19+
this.zipper = zipper;
20+
this.as = as;
21+
this.bs = bs;
22+
}
23+
24+
@Override
25+
public Iterator<C> iterator() {
26+
final Iterator<A> asIterator = as.iterator();
27+
final Iterator<B> bsIterator = bs.iterator();
28+
29+
return new ImmutableIterator<C>() {
30+
@Override
31+
public boolean hasNext() {
32+
return asIterator.hasNext() && bsIterator.hasNext();
33+
}
34+
35+
@Override
36+
public C next() {
37+
return zipper.apply(asIterator.next(), bsIterator.next());
38+
}
39+
};
40+
}
41+
42+
public static <A, B, C> ZippingIterable<A, B, C> zipWith(DyadicFunction<? super A, ? super B, ? extends C> zipper,
43+
Iterable<A> as,
44+
Iterable<B> bs) {
45+
return new ZippingIterable<A, B, C>(zipper, as, bs);
46+
}
47+
48+
public static <A, B> ZippingIterable<A, B, Tuple2<A, B>> zip(Iterable<A> as, Iterable<B> bs) {
49+
return new ZippingIterable<A, B, Tuple2<A, B>>(
50+
new DyadicFunction<A, B, Tuple2<A, B>>() {
51+
@Override
52+
public Tuple2<A, B> apply(A a, B b) {
53+
return tuple(a, b);
54+
}
55+
},
56+
as,
57+
bs
58+
);
59+
}
60+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.jnape.palatable.lambda.tuples;
2+
3+
import static java.lang.String.format;
4+
5+
public class Tuple2<_1, _2> {
6+
7+
public final _1 _1;
8+
public final _2 _2;
9+
10+
public Tuple2(_1 _1, _2 _2) {
11+
this._1 = _1;
12+
this._2 = _2;
13+
}
14+
15+
@Override
16+
public String toString() {
17+
return format("(%s, %s)", _1, _2);
18+
}
19+
20+
public static <_1, _2> Tuple2<_1, _2> tuple(_1 _1, _2 _2) {
21+
return new Tuple2<_1, _2>(_1, _2);
22+
}
23+
}
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package com.jnape.palatable.lambda.iterables;
2+
3+
import com.jnape.palatable.lambda.DyadicFunction;
4+
import com.jnape.palatable.lambda.tuples.Tuple2;
5+
import org.junit.Test;
6+
7+
import static com.jnape.palatable.lambda.iterables.ZippingIterable.zip;
8+
import static com.jnape.palatable.lambda.iterables.ZippingIterable.zipWith;
9+
import static com.jnape.palatable.lambda.tuples.Tuple2.tuple;
10+
import static java.util.Arrays.asList;
11+
import static org.junit.Assert.assertThat;
12+
import static testsupport.matchers.IterableMatcher.iterates;
13+
14+
public class ZippingIterableTest {
15+
16+
@Test
17+
public void zipsTwoIterablesTogetherWithFunction() {
18+
Iterable<Integer> oneThroughFive = asList(1, 2, 3, 4, 5);
19+
Iterable<Integer> sixThroughTen = asList(6, 7, 8, 9, 10);
20+
21+
DyadicFunction<Integer, Integer, Integer> add = new DyadicFunction<Integer, Integer, Integer>() {
22+
@Override
23+
public Integer apply(Integer a, Integer b) {
24+
return a + b;
25+
}
26+
};
27+
ZippingIterable<Integer, Integer, Integer> sums = zipWith(add, oneThroughFive, sixThroughTen);
28+
29+
assertThat(sums, iterates(7, 9, 11, 13, 15));
30+
}
31+
32+
@Test
33+
@SuppressWarnings("unchecked")
34+
public void usesTuplingFunctionByDefault() {
35+
Iterable<String> names = asList("Angie", "Bob", "Chelsea");
36+
Iterable<Integer> ages = asList(24, 18, 37);
37+
38+
ZippingIterable<String, Integer, Tuple2<String, Integer>> namesAndAges = zip(names, ages);
39+
assertThat(namesAndAges, iterates(tuple("Angie", 24), tuple("Bob", 18), tuple("Chelsea", 37)));
40+
}
41+
42+
@Test
43+
@SuppressWarnings("unchecked")
44+
public void zipsAsymmetricallySizedIterables() {
45+
Iterable<String> men = asList("Jack", "Sonny");
46+
Iterable<String> women = asList("Jill", "Cher", "Madonna");
47+
48+
ZippingIterable<String, String, Tuple2<String, String>> couples = zip(men, women);
49+
assertThat(couples, iterates(tuple("Jack", "Jill"), tuple("Sonny", "Cher")));
50+
}
51+
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package com.jnape.palatable.lambda.tuples;
2+
3+
import org.junit.Test;
4+
5+
import static org.hamcrest.core.Is.is;
6+
import static org.junit.Assert.assertThat;
7+
8+
public class Tuple2Test {
9+
10+
@Test
11+
public void hasTwoSlots() {
12+
Tuple2<String, Integer> stringIntTuple = new Tuple2<String, Integer>("foo", 1);
13+
14+
assertThat(stringIntTuple._1, is("foo"));
15+
assertThat(stringIntTuple._2, is(1));
16+
}
17+
}

‎src/test/java/testsupport/matchers/IterableMatcher.java‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import java.util.Iterator;
77

88
import static java.util.Arrays.asList;
9+
import static org.apache.commons.lang3.builder.EqualsBuilder.reflectionEquals;
910

1011
public class IterableMatcher<Element> extends BaseMatcher<Iterable<Element>> {
1112

@@ -38,7 +39,7 @@ private boolean iteratesExpectedElementsInOrder(Iterable actual) {
3839
Iterator expectedIterator = expected.iterator();
3940

4041
while (actualIterator.hasNext() && expectedIterator.hasNext())
41-
if (!actualIterator.next().equals(expectedIterator.next()))
42+
if (!reflectionEquals(actualIterator.next(), expectedIterator.next()))
4243
return false;
4344

4445
return actualIterator.hasNext() == expectedIterator.hasNext();

0 commit comments

Comments
 (0)