Chain

From OpenEUO
Revision as of 16:39, 18 December 2010 by Ximan (Talk | contribs) (Created page with "== Calling Pattern == Call local c = sl.chain(a,b[,...]) Args a is a function b1..bN are functions Results c is a chain closure == Alternative Calling Pattern == Call local...")

(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Calling Pattern

Call

local c = sl.chain(a,b[,...])

Args

a is a function
b1..bN are functions

Results

c is a chain closure

Alternative Calling Pattern

Call

local c = sl.chain(a,t)

Args

a is a function
t is a table of functions with one-based, contiguous numeric keys

Results

c is a chain closure

Closure Methods

Example Usage

local b01 = function(n,a,b,c)
 print(n)
 return true
end
local b02 = function(a,b,c)
 print(a..' '..b..' '..c)
 return string.char(string.byte(a)+1), string.char(string.byte(b)+1), string.char(string.byte(c)+1)
end
local b05 = {b02,b02,b02}
local b00 = sl.chain(b01,b05)
local b03, b04 = b00.init('a','b','c')
print(b03)
print(b04)
b00.dispose()
--> a b c
    1
    b c d
    2
    c d e
    3
    3

Description

Calling chain returns a chain closure with two defined methods, dispose and initiate. The first argument, a, is the monitoring function, and the remaining functions (or a table of functions) make up the body of the execution chain.

Functions passed as the b arguments to chain are called in turn and their results are passed to the a function. If a returns true, the next argument in the chain is executed.

Upon Error

If none of the described argument patterns are matched, an error is reported and handled according to the operant error redirection mode.

See Also