|
| 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 | +} |
0 commit comments