ONLINE HELP
 WINDEVWEBDEV AND WINDEV MOBILE

Help / WLanguage / WLanguage syntax / WLanguage procedures
  • Overview
  • Lambda procedure
  • Example
  • Syntaxes
  • Remarks
  • Lambda expression
  • Example
  • Syntaxes
  • Remark
  • Typed lambda procedure
  • Example
  • Syntax
  • Remarks
WINDEV
WindowsLinuxJavaReports and QueriesUser code (UMC)
WEBDEV
WindowsLinuxPHPWEBDEV - Browser code
WINDEV Mobile
AndroidAndroid Widget iPhone/iPadIOS WidgetApple WatchMac Catalyst
Others
Stored procedures
Overview
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.
Lambda procedure
A lambda procedure is an anonymous internal procedure, for which the parameters and the return value do not need to be typed.

Example

// Fill in a array with the names of all the files in the data directory
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.
Lambda expression
A lambda expression is a simplified lambda procedure when the WLanguage code to run corresponds to:
RETURN <Expression>

Example

  • Example using a lambda procedure:
    // count odd-numbered elements of an array 
    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:
    // count odd-numbered elements of an array 
    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".
Typed lambda procedure
A typed lambda procedure is an anonymous internal procedure, for which the parameters and the return value can be typed.

Example

// Fill in a array with 
// the names of all files in the data directory
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
    <Identifier> [is] <Type>
  • <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.
Minimum version required
  • Version 25
This page is also available for…
Comments
Click [Add] to post a comment

Last update: 09/19/2024

Send a report | Local help