clojure - Chain (compose) method calls in Groovy -


i have variable , several methods i'd call in sequence return value of 1 method input of another. pipeline. now, there way of chaining calls? in pseudo code looks like

def = [1, 2, 3] def b = calculation3(calculation2(calculation1(a))) 

as can see looks clojure-like , i'd end (using clojure syntax)

(->     calculation1    calculation2    calculation3) 

i hoping use with keyword passes variable a around not collecting results , passing them input next method.

the working solution i've found far 'closure composition' this. seems heavy handed me.

def = [1, 2, 3] def b = (class1.&calculation1 >> class1.&calculation2 >> class1.&calculation3)(a) 

any idea?

you write function it:

def chain(a, closure... fns) {     fns.tolist().inject(a) { v, c -> c(v) } } 

then call in code:

chain(a, class1.&calculation1,class1.&calculation2,class1.&calculation3) 

Comments

Popular posts from this blog

How to connect android app to App engine -

gcc - MinGW's ld cannot perform PE operations on non PE output file -

php - display validation error message next to the textbox in codeigniter -