An anonymous function in programming is a special kind of functions that are declared at the place of use and do not receive a unique identifier for access to them. Supported in many programming languages .
Usually, when creating anonymous functions, they are either called directly, or a function reference is assigned to a variable , with which you can then indirectly call this function. If an anonymous function refers to variables not contained in its body (capture), then such a function is called closure . A lambda expression is a typical syntax construct for many languages to define an anonymous function.
Syntax
The syntax for writing anonymous functions for different programming languages is very different in most cases.
| Tongue | Add Record Example |
|---|---|
| AS3 | function ( x : int , y : int ): int { return x + y ;}
|
| C # | ( x , y ) => x + y
|
| C ++ | Introduced in C ++ 11. The grip and body must be present. Full form [1] : [ capture ] ( parameters ) mutable exceptions attributes -> return _type { body }
[] ( int x , int y ) { return x + y ; }
In C ++ 14, the ability to use lambda functions with auto lambda = [] ( auto x , auto y ) { return x + y ;};
int a ; auto f = [ a ] () { return a ;}
[ a = std :: string {}] () { return a ;}
|
| CoffeeScript | (x, y) -> x + y
|
| PascalABC.NET | ( x , y ) -> x + y
|
| D | // short form with auto type inference
auto a = (( x , y ) => x + y ) ( 2 , 3 );
// long form of writing (block with curly brackets) with automatic type inference
auto aa = ( x , y ) { return x + y ; } ( 2 , 3 );
// compiler auto-detection of anonymous function type: function or delegate
auto b = ( int x , int y ) => x + y ;
auto bb = ( int x , int y ) { return x + y ; };
// functions do not have access to external variables
auto c = function ( int x , int y ) => x + y ;
auto cc = function ( int x , int y ) { return x + y ; };
// delegates have access to external variables
auto d = delegate ( int x , int y ) => x + y ;
auto dd = delegate ( int x , int y ) { return x + y ; };
// delegate that takes an int variable and returns a double value
auto f = delegate double ( int x ) { return 5.0 / x ; };
|
| Delphi (since 2009 version) | function ( x , y : integer ) : integer
begin
result : = x + y ;
end ;
|
| Erlang | fun ( X , Y ) -> X + Y end
|
| Scala | Without indicating the context, the type of variables must be specified: ( x : Int , y : Int ) => x + y
But in places where the type can be inferred, you can use abbreviated forms: ( 1 to 100 ) reduce (( a , b ) => a + b )
Or even shorter, using the '_' auto-substitution: ( 1 to 100 ) reduce ( _ + _ )
|
| GNU Octave | @ ( x , y ) x + y
|
| Groovy | { x , y -> x + y }
|
| Haskell | \ x y -> x + y
|
| Java (since version 8) | // with no parameter
() -> System . out . println ( "Hello, world." );
// with a single parameter (This example is an identity function).
a -> a
// with a single expression
( a , b ) -> a + b
// with explicit type information
( Long id , String name ) -> "id:" + id + ", name:" + name
// with a code block
( a , b ) -> { return a + b ;}
// with multiple statements in the lambda body. It requires a code block.
// This example also includes a nested lambda expression as well as a closure.
( id , newPrice ) -> {
Optional < Product > mayBeProduct = findProduct ( id );
mayBeProduct . ifPresent ( product -> product . setPrice ( newPrice ));
return mayBeProduct . get ();
}
|
| Javascript | // ES3 +
function ( x , y ) { return x * y }
// ES6 + (ES2015 +)
( x , y ) => x + y ;
|
| Lua | function ( x , y ) return x + y end
|
| Maple | ( x , y ) -> x + y
|
| Mathematica | # 1 + # 2 &
or Function [ # 1 + # 2 ]
or Function [{ x , y }, x + y ]
|
| MATLAB | f = @ ( x , y ) x + y
|
| Maxima | lambda ([ x, y ] , x + y )
|
| Perl | sub { return $ _ [ 0 ] + $ _ [ 1 ] }
|
| Php | // PHP 7.4
fn ( $ x ) => $ x + $ y ;
In PHP 7.4, short lambdas were added. [9] // PHP 5.3
function ( $ x , $ y ) use ( $ a , & $ b ) { return $ x + $ y ; }
$ a, $ b are captured variables, while the variable $ b is also closed [10] [11] . // PHP 4> = 4.0.1, PHP 5
create_function ( '$ x, $ y' , 'return $ x + $ y;' )
[12] |
| Python | lambda x , y : x + y
|
| R | function ( x, y ) x + y
|
| Ruby | lambda { | x , y | x + y }
|
| Rust | | x : i32 , y : i32 | x + y
|
| Scheme , Common Lisp | ( lambda ( x y ) ( + x y ))
|
| SML | fn ( x , y ) => x + y
|
| Visual prolog | {( X , Y ) = X + Y }
|
| Go | Z : = func () int {
return X + Y
} ()
|
| Swift | // 1 option
let f : ( Int , Int ) -> Int = { x , y in return x + y }
// Option 2
let f : ( Int , Int ) -> Int = { x , y in x + y }
/ * With abbreviated parameter names * /
// 1 option
let f : ( Int , Int ) -> Int = { return $ 0 + $ 1 }
// Option 2
let f : ( Int , Int ) -> Int = { $ 0 + $ 1 }
|
| Nim | proc ( x , y : int ): int = x * y
|
See also
- Short circuit (programming)
- Callback (programming)
Notes
- ↑ anonymous functions .
- ↑ C ++ 11. Lambda expressions
- ↑ Sutter, Herb Trip Report: ISO C ++ Spring 2013 Meeting . isocpp.org (April 20, 2013). Date of treatment June 14, 2013.
- ↑ Description in the JavaScript Kernel Reference
- ↑ ECMAScript Language Specification Edition 3 79 (March 24, 2000).
- ↑ Arrow functions . Mozilla Developer Network. Date of treatment December 30, 2015.
- ↑ Mathematica Documentation: Function (&) Archived on April 5, 2007.
- ↑ perldoc perlref
- ↑ PHP Digest No. 152 (March 11 - 25, 2019)
- ↑ M. Zandstra, “PHP Objects, Patterns, and Practice”, second edition, Ed. Apress, 2008.
- ↑ PHP Manual
- ↑ PHP Manual
- ↑ Section of the tutorial, “Master Python in 24 hours on your own”. Archived on April 30, 2006.
- ↑ Description in the book “Programming Ruby” Archived on April 11, 2006. (eng.)