New Package: Closure

36 views
Skip to first unread message

Mahmoud Fayed

unread,
Jan 21, 2026, 1:06:36 PM (12 days ago) Jan 21
to The Ring Programming Language
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

Mansour Ayouni

unread,
Jan 21, 2026, 7:46:22 PM (11 days ago) Jan 21
to Mahmoud Fayed, The Ring Programming Language
Hello Mahmoud,

Thank you for adding this feature to Ring!

Now we can let anonymous functions carry with them their context, act on it even if the outer function has terminated, and enable some sophisticated programming scenarios.

I would like to share this nice explanation of closures for those who need it:

All the best,
Mansour

--

---
You received this message because you are subscribed to the Google Groups "The Ring Programming Language" group.
To unsubscribe from this group and stop receiving emails from it, send an email to ring-lang+...@googlegroups.com.
To view this discussion visit https://groups.google.com/d/msgid/ring-lang/e0d97106-70f5-4f33-8b32-1e14e87c6e66n%40googlegroups.com.

Mahmoud Fayed

unread,
Jan 21, 2026, 10:50:48 PM (11 days ago) Jan 21
to The Ring Programming Language
Hello Mansour

You are welcome :D

Greetings,
Mahmoud

Reply all
Reply to author
Forward
0 new messages