Created
April 10, 2012 13:36
-
-
Save timyates/2351423 to your computer and use it in GitHub Desktop.
A self contained Iterator from a Map in Groovy
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
| def val = 1 | |
| def a = [ hasNext:{ true }, | |
| next:{ val++ } ] as Iterator | |
| assert a.take( 2 ).collect() == [ 1, 2 ] |
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
| 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 |
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
| 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