Created
November 19, 2012 14:18
-
-
Save mdread/4110880 to your computer and use it in GitHub Desktop.
function zipAll for iterables
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
public static <A, B> Iterable<Pair<A, B>> zipAll(Iterable<A> iter1, Iterable<B> iter2, A def1, B def2){ | |
List<Pair<A, B>> zipped = new ArrayList<TitleParser.Pair<A,B>>(); | |
Iterator<A> these = iter1.iterator(); | |
Iterator<B> those = iter2.iterator(); | |
while(these.hasNext() && those.hasNext()){ | |
zipped.add(new Pair<A, B>(these.next(), those.next())); | |
} | |
while(these.hasNext()){ | |
zipped.add(new Pair<A, B>(these.next(), def2)); | |
} | |
while(those.hasNext()){ | |
zipped.add(new Pair<A, B>(def1, those.next())); | |
} | |
return zipped; | |
} | |
public static class Pair<A, B> { | |
public final A left; | |
public final B right; | |
public Pair(A left, B right) { | |
this.left = left; | |
this.right = right; | |
} | |
@Override | |
public String toString() { | |
return "(" + left + ", " + right + ")"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment