I am having trouble nesting multiple Closures in Groovy. Nesting only one works like a charm:
def nestedClosure = { Closure cl -> doSomething { cl() } }
But how can I programatically nest multiple closures from e.g. a list of Closures?
Suppose this example:
Closure main = { println "Hello world" }
Closure consumer1 = { x -> println("1"); x(); println("1 END") }
Closure consumer2 = { x -> println("2"); x(); println("2 END") }
Closure consumer3 = { x -> println("3"); x(); println("3 END") }
I want to somehow chain and nest these consumers to get the output of:
1
2
3
Hello world
3 END
2 END
1 END
Tried currying, insane loops and googling for quite a while now, but can't seem to get any working idea.