|
|
|
|
|
- Overview
- Lambda procedure
- Example
- Syntaxes
- Remarks
- Lambda expression
- Example
- Syntaxes
- Remark
- Typed lambda procedure
- Example
- Syntax
- Remarks
Lambda: Expressions and procedures
A lambda ("lambda function") is a more concise way to write internal procedures. Lambdas are useful for all functions that take callback procedures as parameters. This means it is possible to enter the function code directly instead of the parameter. In this case: - the procedure has no name,
- the code of the procedure is directly where the name of the procedure should be.
WLanguage offers several syntaxes for using lambdas. A lambda procedure is an anonymous internal procedure, for which the parameters and the return value do not need to be typed. Example
arrFile is array of strings
fListFile(fDataDir() + "\*.*", (Directory,Queue)=>{arrFile.Add(Queue)} )
In this code, the lambda procedure corresponds to the following: (Directory,File)=>{arrFile.Add(File)} Syntaxes - Lambda procedure that expects no parameters:
() => { <WLanguage code of procedure>} - Lambda procedure that expects only one parameter:
- full syntax:
(<Parameter 1>) => { <WLanguage code of procedure>} - simplified syntax:
<Parameter 1> => { <WLanguage code of procedure>}
- Lambda procedure that expects several parameters:
(<Parameter 1>, ..., <Parameter N>) => { <WLanguage code of procedure>}
In these syntaxes: - <Parameter X> corresponds to the name of the parameter.
- <WLanguage code of procedure> corresponds to the WLanguage code of the procedure. It is enclosed between { and }. Several statements can be used
Remarks - This syntax can be used on WLanguage functions (fListFile, etc.) but also on the procedures of the project. To do so, the formal parameter to which the lambda procedure is passed must be of type "Procedure".
- The procedure is anonymous, so it has no name and cannot be called elsewhere or recursively.
- A lambda procedure does not need to retrieve all the parameters:
- unused parameters do not trigger a warning at compile time,
- omitted parameters at the end of the prototype do not trigger an error at runtime.
A lambda expression is a simplified lambda procedure when the WLanguage code to run corresponds to: Example - Example using a lambda procedure:
arrValue is array of int = [1,2,3,4,5]
Trace(CountValues(arrValue, x=>{RETURN modulo(x, 2)=1}))
PROCEDURE CountValues(arrValue is array of int,
Condition is procedure(x int):boolean)
let n = 0
FOR EACH x OF arrValue
IF Condition(x) THEN
n++
END
END
RETURN n
- Simplification as lambda expression:
arrValue is array of int = [1,2,3,4,5]
Trace(CountValues(arrValue, x=>modulo(x,2)=1))
PROCEDURE CountValues(arrValue is array of int,
Condition is procedure(x int):boolean)
let n = 0
FOR EACH x OF arrValue
IF Condition(x) THEN
n++
END
END
RETURN n
Syntaxes - Lambda expression that expects no parameters:
() => <WLanguage expression> - Lambda expression that expects only one parameter:
- full syntax:
(<Parameter 1>) => <WLanguage expression> - simplified syntax:
(<Parameter 1>) => <WLanguage expression>
- Lambda expression that expects several parameters:
(<Parameter 1>, ..., <Parameter N) => <WLanguage expression>
In these syntaxes: - <Parameter X> corresponds to the name of the parameter.
- <WLanguage expression> corresponds to the WLanguage expression to return.
Remark This syntax can be used on WLanguage functions ( fListFile, etc.) but also on the procedures of the project. To do so, the formal parameter to which the lambda expression is passed must be of type "Procedure". A typed lambda procedure is an anonymous internal procedure, for which the parameters and the return value can be typed. Example
arrFile is array of strings
fListFile(fDataDir() + "\*.*", ...
PROCEDURE(Directory is string, Queue is string):string {arrFile.Add(Queue)})
Syntax PROCEDURE (<Parameter 1>, ..., <Parameter N>): <Type of return> { <WLanguage code of procedure>} In this syntax: - <Parameter X> corresponds to the name of the parameter(s) of the lambda procedure. This parameter can be typed or untyped. If a parameter is typed, its syntax is
- <Type of return> corresponds to the type returned by the lambda procedure.
- <WLanguage code of procedure> corresponds to the WLanguage code of the procedure. It is enclosed between { and }.
Remarks - This syntax can be used on WLanguage functions (fListFile, etc.) but also on the procedures of the project. To do so, the formal parameter to which the lambda procedure is passed must be of type "Procedure".
- The procedure is anonymous, so it has no name and cannot be called elsewhere or recursively.
- A lambda procedure does not need to retrieve all the parameters:
- unused parameters do not trigger a warning at compile time,
- omitted parameters at the end of the prototype do not trigger an error at runtime.
This page is also available for…
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|