method Name [pure] {entry | change} '~' Sequence function Name [pure] entry '~' Sequence
Adjunct procedures let you perform operations on an active sequence. For example you could use one to get the current state of a sequence or restart from a different place in a sequence.
They are included in the same file as the sequence after the main sequence. You can write them as either Method or Function procedures. The first parameter is always a sequence iterator and is designated as a tilda followed by the name of the primary sequence. After that you can declare additional parameters as is the case for any Method or Function.
Adjunct procedures can access and modify local variables declared in the primary sequence. Variables that are implicitly declared cannot be accessed. Globals can also be accessed either via use/alter commands in the primary sequence or one declare in the adjunct proecedure. Parameters in the primary sequence can also be accessed.
It is best shown how Adjunct procedures work with an example. We start with the signature for a generic sequence that iterates top down over a stack. This would be follwed by a code body to implement the sequence.
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: : sequence STACK pure: Scan the stack from the top down. entry Stack__type, &The stack to scan. Index = 0 word :Number of elements to start down from the top. exit *Item _type :At each element from the top down; 0 if empty. : :...............................................................................
Adjunct procedures can go after the primary Return command. In this case the Adjunct procedure tests to see if this is the last iteration. The code body would check the condition using local variables in the Stack sequence. The preamble for the procedure would look like this:
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: : function IS.LAST: See if the sequence has returned it's last value. entry ~Stack exit Last Bit :Set if the sequence has no more values. : :...............................................................................
The consumer of the sequence can then use the Is.Last function inside a sequence loop. Note that the It`First condition is intrinsic while there is no intrinsic condition to test for the last element in a sequence.
:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: : function FORM..Stack..cell: Format a stack of integers into a list. entry Stack..cell, &Stack to be formatted Format = "" string :A format descriptor exit List string :The formatted list: {Item, ...} : :............................................................................... IF $Empty: IF the stack is empty, List = "{}"; The result is an empty list. ELSE: ELSE there are elements to format, DO Item from $ with It: DO over each item top down, List != if It`First, "{", ", "; First is "{"; else add a comma. List != form( Item, Format ); Add an item to the list. List != if Is.Last( It ), "}"; Close off the list at the end. . - return
Now we can use the FORM..Stack..cell function to print a stack's content.
PUSH Stack, 'X' PUSH Stack, 'Y' PUSH Stack, 'Z' PRINT Stack|"=#h#"; The output is: {#5a, #59, #58}