ClosureClosures for Ring

Getting started

Install the package and make the first closure.

Install#

Install the package with RingPM, the Ring package manager, in one step:

Shell
ringpm install closure from ringpackages

Quick start#

Ring
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:

Ring
inc = increment()
dec = decrement()

? invoke(dec)	# 99
? invoke(inc)	# 11
? invoke(dec)	# 98

Variables of the closure#

The first parameter of closure() is the list of the variables to keep. An item is:

Closure - Closures for Ring, made with ringdoc.