A Java class:
import groovy.lang.Closure; // loop/recur class RecursionWrapper { private Closure<object> _target; public RecursionWrapper(Closure<object> target) { _target = target; } public Object recur() { return _target.trampoline(); } public Object recur(Object a) { return _target.trampoline(a); } public Object recur(Object... a) { return _target.trampoline(a); } }
A global function:
// loop/recur Object.metaClass.loop = {closure-> def cln = closure.clone() def trCln = cln.trampoline() def rw = new RecursionWrapper(trCln) cln.delegate = rw trCln }
No comments:
Post a Comment