Skip to content

Instantly share code, notes, and snippets.

@timyates
Created October 30, 2013 14:56
Show Gist options
  • Save timyates/7234041 to your computer and use it in GitHub Desktop.
Save timyates/7234041 to your computer and use it in GitHub Desktop.
Composable Iterators with Guava and Groovy
@Grab( 'com.google.guava:guava:15.0' )
import com.google.common.collect.Iterators
Iterator.metaClass {
leftShift = { Iterator b ->
Iterators.concat( delegate, b )
}
leftShift = { Iterable b ->
Iterators.concat( delegate, b.iterator() )
}
}
def a = [ 1, 2, 3 ].iterator()
def b = [ 4, 5, 6 ].iterator()
def c = [ 7, 8, 9 ].iterator()
def composed = a << b << c << [ 'a', 'b', 'c' ]
assert composed.collect() == [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 'a', 'b', 'c' ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment