Repeated Parameter Forwarding

Revision as of 23:20, 10 April 2013 by Lchrisman (talk | contribs) (Basic idea)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

new to Analytica 4.5

Repeated parameter forwarding is a language extension that enables you to pass a list of computed results to a repeated function parameter. To understand repeated parameter forwarding, you must first understand repeated parameters, hence the Repeated parameters section of Function Parameter Qualifiers is a prerequisite to the content on this page.

Basic idea

Consider the following User-Defined Function:

Function TreeNode( X ; Children : ... )
Definition: Array(TreeNodeIdx,[X,\Children))

The call TreeNode( 1, 2, 3, 4 ) is used to create this data structure:

SmallTree.png

The repeated parameter, «children», allows you to list as many children as you'd like, without having to make use of square brackets in the syntax. Hence, we could built a multi-level tree using:

TreeNode( 1, TreeNode(2,5,6), TreeNode( 3, 7,8,9, TreeNode(10,11,12,13) ), 4 )

MultiLayerTree.png

Now suppose you have some expression -- I'll just write F( ) -- that computes and returns a list of "nodes", and you want to create a tree node having this set of nodes as the children, with 7 as the root. Your expression doesn't know what F( ) will return, or even how many children it will return, but for illustration let's pretend it returns [1,2,3]. Your first attempt might be

TreeNode(a,F())

This produces an error, because you cannot pass a list value to a repeated parameter; however, logically what you are actually doing here is producing the wrong tree, which would actually be OneChildTree.png The call is providing one instance of the repeated parameter, hence one child node. The value of that child node is the list of values. What we really want is to be able to pass three instances of the «children» parameter to the function, basically

TreeNode(7, Slice(F(),1), Slice(F(),2), Slice(F(),3) )

This would be one way of doing it if we actually knew that F() would return 3 values, but part of the point here is that we can't assume we know how many items F() is going to return.

Repeated parameter forwarding solves the problem. To forward the result of F() to the repeated parameter, we preface it with three dots

TreeNode( 7, ...F( ) )

The three dots (which matches the three dots used when declaring a repeated parameter) indicates that we want each item in the computed result to be passed as one instance of the repeated parameter. When you use this syntax, the expression following the three dots must produce a list or a 1-D array.

Comments


You are not allowed to post comments.