ClosureClosures for Ring

Samples

The sample program of the package.

See the folder samples/UsingClosure. The name of the sample links to its source code in this documentation (below the table) and to the file on GitHub.

SampleDescriptionCode
test1.ringTwo closures (a counter that increments and a counter that decrements) that keep their own variables. Output: 1, 11, 12, 1013, 99, 1014, 98GitHub

Source code#

test1.ring#

samples/UsingClosure/test1.ring
samples/UsingClosure/test1.ring
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 
	})
	
Closure - Closures for Ring, made with ringdoc.