Groovy can work a lot like Javascript. You can have private vars and functions via closure.
You can also curry functions with closures.
class FunctionTests {
def privateAccessWithClosure = {
def privVar = 'foo'
def privateFunc = { x -> println "${privVar} ${x}"}
return {x -> privateFunc(x) }
}
def addTogether = { x, y ->
return x + y
}
def curryAdd = { x ->
return { y-> addTogether(x,y)}
}
public static void main(String[] args) {
def test = new FunctionTests()
test.privateAccessWithClosure()('bar')
def curried = test.curryAdd(5)
println curried(5)
}
}
output:
foo bar
10