Created
June 18, 2011 06:48
-
-
Save xnrghzjh/1032871 to your computer and use it in GitHub Desktop.
stepメソッドの展開
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 org.codehaus.groovy.runtime.* | |
println "オリジナル" | |
1.step(5,1) {print "$it "} | |
println "" | |
println "AST Browser 上" | |
1.step(5, 1, {this.print("$it ")}) | |
println "" | |
println "DefaultGroovyMethods上" | |
DefaultGroovyMethods.step(1,5,1,{this.print "$it "}) | |
println "" | |
//DefaultGroovyMethods からコピペと修正 | |
Integer.metaClass.myStep {to, stepNumber, closure -> | |
if (stepNumber > 0 && to > delegate) { | |
(delegate..to-1).each(){ closure.call(it) } | |
} else if (stepNumber < 0 && to < delegate) { | |
(delegate..to+1).each(){ closure.call(it) } | |
} else if(delegate != to) | |
throw new GroovyRuntimeException("Infinite loop in " + delegate + ".step(" + to + ", " + stepNumber + ")") | |
} | |
def p ={print "$it "} | |
println "myStep ++" | |
1.myStep(5, 1, p) | |
println "" | |
println "myStep --" | |
5.myStep(1, -1, p) | |
println "" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment