Groovy web console

subscribe to the feed Subscribe
to this
site
NaNHighOrderFn2 (via #groovywebconsole)
tweet this snippet Tweet
this
script

NaNHighOrderFn2

Published 4 months ago by NaNLABS
Actions  ➤ Edit in console Back to console Show/hide line numbers View recent scripts
def sum = {a,b-> return a+b }

def prod = {a,b-> return a*b }
 
def list = [1,2,3,4,5]
 
 
def visitor = { l, g, initValue ->
l.inject(initValue) { result, i -> g(result,i) }
}

def holder = [:]
holder.sum = visitor.ncurry(1,sum,0)
holder.prod = visitor.ncurry(1,prod,1)

def main ={ methodName, l->
holder."$methodName"(l)
}

main("sum",list)
main("prod",list)