Groovy web console

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

HighOrderFn1

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) }
}
visitor(list,sum, 0)
visitor(list,prod, 1)