Hello
I have already added a new package called Closure
It's useful for functional programming where we can create an anonymous function that capture specific variables.
Using closure() capture variables by value. For lists/objects we can change this behaviour (optional) using the Ref() function. The first parameter to closure() is a list of variables to capture we can type
(1) :variable_name
Or
(2) :variable_name = value
Or
(3) :variable_name = ref(list/object)
The second parameter is an anonymous function that could use the captured variables
Install
ringpm install closure from ringpackages
Example:
load "closure.ring"
func main
inc = increment()
? isClosure(inc) # Prints 1
? invoke(inc) # Prints 11
? invoke(inc) # Prints 12
for m = 1 to 1000
invoke(inc)
next
? invoke(inc)
dec = decrement()
? invoke(dec)
? invoke(inc)
? invoke(dec)
func increment
x = 10
return closure([:x], func {
x++
return x
})
func decrement
x = 100
return closure([:x], func {
x--
return x
})
Output:
1
11
12
1013
99
1014
98
Greetings,
Mahmoud