Getting started
Install the package and make the first closure.
Install#
Install the package with RingPM, the Ring package manager, in one step:
ringpm install closure from ringpackagesQuick start#
load "closure.ring"
func main
inc = increment()
? invoke(inc) # 11
? invoke(inc) # 12
func increment
x = 10
return closure([:x], func {
x++
return x
})increment() creates the variable x and returns a closure that keeps it. Each call of invoke(inc) adds 1 to the x of that closure, even after increment() has returned.
Two closures#
Every call of increment() or decrement() makes a new closure with its own variables:
inc = increment()
dec = decrement()
? invoke(dec) # 99
? invoke(inc) # 11
? invoke(dec) # 98Variables of the closure#
The first parameter of closure() is the list of the variables to keep. An item is:
- A variable name (
:xor"x"): the value of that variable in the function that calledclosure()is copied into the closure. - A list
[name, value]: a variable with a given start value, for example[:count, 0].