Skip to content

Instantly share code, notes, and snippets.

@xnrghzjh
Created June 18, 2011 06:48
Show Gist options
  • Save xnrghzjh/1032871 to your computer and use it in GitHub Desktop.
Save xnrghzjh/1032871 to your computer and use it in GitHub Desktop.
stepメソッドの展開
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