Skip to content

Instantly share code, notes, and snippets.

@mdread
Created November 19, 2012 14:18
Show Gist options
  • Save mdread/4110880 to your computer and use it in GitHub Desktop.
Save mdread/4110880 to your computer and use it in GitHub Desktop.
function zipAll for iterables
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