Skip to main content

  | मौसम…

411 समाचार
Public archive edition · Attributed source reporting with original dates · AI drafts are excluded

411 REFERENCE · Wikibooks

OpenSCAD User Manual/Lambdas

Reference material by Wikibooks contributors. This retains its source identity and is not 411 news reporting.

part of the section on Values, Data Types, and Constants Function Literal A function literal is a way to encapsulate a calculation in an object that can then be used in place of a literal value in an expression, function or module call, or even be assigned to a variable or returned by a function. These objects are called anonymous functions, closures, or lambdas. 2021.01 definition of a lambda function function( param<default>, arg ) expression; keyword literal "function" (parameter,argument) Zero or more arguments, optionally with a default value parameter and argument are synonyms. value an expression that calculates a value which may be of any type. the lack of a name and of the equals sign Function literals can be assigned to variables and passed around like any value. Calling the function uses the normal function call syntax with parenthesis. func = function (x) x * x; echo(func(5)); // ECHO: 25 When called as part of an expression lambdas are calculated before other operators. The following script defines three lambdas: the first is assigned to the variable "selector" which, when called, will create one of the two inner lambdas to return as its result: // assign a lambda to variable "selector" selector = function (which) which == "add" ? function (x) x + x // lambda for addition : function (x) x * x; // and for multiplication // display the value of "selector" for "add" echo(selector("add")); // ECHO: function(x) ((x + x)) // call the function via selector to add 5+5 echo(selector("add")(5)); // ECHO: 10 // .. and for "mul" echo(selector("mul")); // ECHO: function(x) ((x * x)) // call the function via selector to do 5 x 5 echo(selector("mul")(5)); // ECHO: 25