Skip to content

Instantly share code, notes, and snippets.

@activedecay
Created March 13, 2017 20:23
Show Gist options
  • Save activedecay/f5c960128d43a889f797b9ffc224b10b to your computer and use it in GitHub Desktop.
Save activedecay/f5c960128d43a889f797b9ffc224b10b to your computer and use it in GitHub Desktop.
why aren't itardators itardabtable? oh cuz java.
public static <T> Iterable<T> in(final Iterator<T> iterator) {
assert iterator != null;
class SingleUseIterable implements Iterable<T> {
private boolean used = false;
@Override
public Iterator<T> iterator() {
if (used) {
throw new IllegalStateException("SingleUseIterable already invoked");
}
used = true;
return iterator;
}
}
return new SingleUseIterable();
}
@activedecay
Copy link
Author

now write

for (x : in(iterator)) {...}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment