Skip to content

Instantly share code, notes, and snippets.

@timyates
Created April 10, 2012 13:36
Show Gist options
  • Select an option

  • Save timyates/2351423 to your computer and use it in GitHub Desktop.

Select an option

Save timyates/2351423 to your computer and use it in GitHub Desktop.
A self contained Iterator from a Map in Groovy
def val = 1
def a = [ hasNext:{ true },
next:{ val++ } ] as Iterator
assert a.take( 2 ).collect() == [ 1, 2 ]
def a = [ val:1,
hasNext:{ true },
next:{ this.val++ } ] as Iterator
assert a.take( 2 ).collect() == [ 1, 2 ]
// groovy.lang.MissingPropertyException: No such property: val for class: ConsoleScript1
import java.lang.reflect.Proxy
def a
a = [ val:1,
hasNext:{ true },
next:{ Proxy.getInvocationHandler( a ).delegate.val++ } ] as Iterator
assert a.take( 2 ).collect() == [ 1, 2 ]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment