Product Documentation
Cadence SKILL Language Reference
Product Version IC23.1, November 2023

foreachs

foreachs( 
s_formalVar 
g_exprList 
g_expr1 
[ g_expr2 ... ] 
)
=> l_valueList / l_result
foreachs( 
( 
s_formalVar1... 
s_formalVarN 
) 
g_exprList1... 
g_exprListN 
g_expr1 
[ g_expr2 ... ] 
)
=> l_valueList / l_result
foreachs( 
s_formalVar 
g_exprTable 
g_expr1 
[ g_expr2 ... ] 
)
=> o_valueTable / l_result

Description

Evaluates one or more expressions for each element of a list of values. In the SKILL++ mode, this function always locally wraps the loop or iterator local variable, s_formalVar,in a let block while compiling the code. Local wrapping preserves the lexical scope of the loop variable. This function may work slower than its non-wrapped counterpart foreach. This is a syntax form.

The first form shown in the syntax above evaluates g_exprList, which returns a list l_valueList. It then assigns the first element from l_valueList to the formal variable s_formalVar and  executes the expressions g_expr1, g_expr2 ... in sequence. The function then assigns the second element from l_valueList and repeats the process until l_valueList is exhausted.

The second form shown in the syntax above can iterate over multiple lists to perform vector operations. Instead of a single formal variable, the first argument is a list of formal variables followed by a corresponding number of expressions for value lists and the expressions to be evaluated.

The third form shown in the syntax above can be used to process the elements of an association table. In this case, s_formalVar is assigned each key of the association table one by one, and the body expressions are evaluated each iteration. The syntax for association table processing is provided in this syntax statement.

Arguments

s_formalVar

Name of the local loop variable that must not be changed inside the loop

s_mappingFunction

One of map, mapc, mapcan, mapcar, or maplist

g_exprList

Expression whose value is a list of elements to assign to the formal variable s_formalVar

g_expr1, g_expr2

Expressions to execute

g_exprTable

Association table whose elements are to be processed

Value Returned

l_valueList

Value of the second argument, g_exprList

l_result

The result of the last expression evaluated

o_valueTable

Value of g_exprTable

Examples

If the loop variable of a foreach is also defined in the surrounding lexical scope, and then a function declared in that lexical scope is called within the foreach, it will access the loop variable rather than the same variable from the surrounding scope. For example:

procedure(LoopScopingIssue(x)
procedure(printArg()
printf("printArg: x is %L\n" x)
)
foreach(x '(1 2 3 4 5)
printf("local loop: x is %L\n" x)
printArg()
)
)
LoopScopingIssue(10) will output:
local loop: x is 1
printArg: x is 1
local loop: x is 2
printArg: x is 2
local loop: x is 3
printArg: x is 3
local loop: x is 4
printArg: x is 4
local loop: x is 5
printArg: x is 5

If the lexical scope is fully honored, you expect the call to printArg() to print the value of x passed into LoopScopingIssue.

If instead you use foreachs:

procedure(LoopScopingIssue(x)
procedure(printArg()
printf("printArg: x is %L\n" x)
)
foreachs(x '(1 2 3 4 5)
printf("local loop: x is %L\n" x)
printArg()
)
)
then LoopScopingIssue(10) will output:
local loop: x is 1
printArg: x is 10
local loop: x is 2
printArg: x is 10
local loop: x is 3
printArg: x is 10
local loop: x is 4
printArg: x is 10
local loop: x is 5
printArg: x is 10

Related Topics

Flow Control Functions


Return to top
 ⠀
X