LAMBDAP

The LAMBDAP function creates small, inline procedures that can be passed around as IDL strings. These Lambda procedures can be used to make procedure calls, or used as inputs to other IDL routines that require user procedures.

Note: LAMBDAP can only create procedures from a scalar string. To dynamically create procedures from a string array, you can use the COMPILE_CODE routine.

Examples

Here we create a multi-statement Lambda procedure, and pass it to the CURVEFIT routine:

X = [0.0:9.0]

Y = [12.0, 11.0, 10.2, 9.4, 8.7, 8.1, 7.5, 6.9, 6.5, 6.1]

A = [10.0,-0.1,2.0]

L = LAMBDAP('x,a,f,pder: ' + $

'bx = EXP(A[1]*X) & F = A[0]*bx + A[2] & ' + $

'pder = [[bx], [A[0]*X*bx], [REPLICATE(1.0, 10)]]')

yfit = CURVEFIT(X, Y, 1/Y, A, SIGMA, FUNCTION_NAME=L)

PRINT, A

IDL prints:

9.9112015 -0.10088314 2.0777268

We can also make normal procedure calls on the LambdaP variable:

P = LAMBDAP("arg: PRINT, 'Value of ' + " + $

"SCOPE_VARNAME(arg,LEVEL=-1) + ' is: ' + STRTRIM(arg,2)")

myvar = 3.14

P, myvar

IDL prints:

Value of 'MYVAR' is: 3.14000

Notice that for the previous two examples we passed in a string containing the IDL statements. If our code is short enough, we can also eliminate the quotes and just directly pass in code:

P2 = LAMBDAP(msg: void = DIALOG_MESSAGE(msg))

P2,"hello"

Syntax

Result = LAMBDAP( Code )

Return Value

A string giving IDL's internal name for the LambdaP procedure. You should ignore the actual value, but instead, you should pass the string into your other IDL routines. You can also make direct procedure calls on the returned string and IDL will call your Lambda routine.

Arguments

Code

A string containing your Lambda code, or the actual code itself. The string or code should have the following form:

"arg1, arg2, arg3,... : statement & statement & statement..."

Your Lambda routine must accept at least one argument, although it does not necessarily need to use that argument. The code can contain any number of IDL statements. This can include flow-control statements such as if/then, while, or for loops, as long as the statements are separated by "&" characters.

Note: If you use actual code within the LAMBDAP call, then all of your statements must fit on a single line. You can use "&" to separate multiple statements, but you cannot use "$" to continue the code onto the next line. If your code is too long to easily fit on a single line, use a string containing the code. You can then break the string across multiple lines, as in the first example at the top.

Note: The actual names of the arguments do not matter, as long as they are valid IDL variable names, and they match the variable names within the code. When the Lambda procedure is called, the input variables will be passed into your Lambda procedure in the same order as the call.

Note: If you call Lambda again with the identical Code, then for efficiency a new Lambda procedure will not be created. Instead, the existing Lambda routine will be returned. If for some reason you must have a new routine you should change the code string, for example by changing the names of the arguments.

Keywords

None.

Implementation

LAMBDAP creates a new procedure with the following form:

PRO IDL$LAMBDAPxxx, arg1, arg2,...

COMPILE_OPT IDL2, hidden

statement & statement & statement & ...

END

The "xxx" is filled in with a unique number for each call to Lambda.

IDL then compiles the newly-created routine, and returns the name of the routine in the Result.

If you call Lambda again with the identical Code string, IDL first looks in its list to see if this routine already exists, and returns the same routine name. This makes it efficient to do nested Lambda calls and avoids the overhead of creating extra routines.

Note: Since Lambda procedures are dynamically created at runtime, the routines are not saved within IDL SAVE files. To use the Lambda procedure in a future IDL session, you must re-run the code that creates the Lambda procedure.

Note: RESOLVE_ALL cannot resolve calls to LAMBDAP procedures, and will throw an error with the name of the "unresolved" variable. You should either set the SKIP_ROUTINES keyword to the name of your Lambda variable, or use CONTINUE_ON_ERROR to suppress the errors.

Version History

8.4

Introduced

See Also

LAMBDA, Static Methods and Properties, Filter method, Map method, Reduce method