Clever Geek Handbook
📜 ⬆️ ⬇️

Anonymous function

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.

TongueAdd 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 }
Example [2] :
  [] ( int x , int y ) { return x + y ;  }

In C ++ 14, the ability to use lambda functions with auto [3] was added:

  auto lambda = [] ( auto x , auto y ) { return x + y ;};
An anonymous function can capture as separate variables, for example:
  int a ;  auto f = [ a ] () { return a ;}
so are all external variables: by reference [&] or by copy [=] . You can also combine these approaches: for example, capture all variables by reference, and certain parameters by copy. To be able to modify variables captured by reference, you must specify the mutable keyword when declaring a function. C ++ 14 adds the ability to initialize lambda variables in a capture. For example:
  [ 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 }
[4] [5]
  // ES6 + (ES2015 +)
 ( x , y ) => x + y ;
Arrow Functions were added in ECMAScript 6 (also known as ECMAScript 2015) [6] .
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 ]
[7]
MATLAB
  f = @ ( x , y ) x + y
Maxima
  lambda ([ x, y ] , x + y )
Perl
  sub { return $ _ [ 0 ] + $ _ [ 1 ] }
[eight]
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
[13]
R
  function ( x, y ) x + y
Ruby
  lambda { |  x , y |  x + y }
[14]
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

  1. ↑ anonymous functions (unspecified) .
  2. ↑ C ++ 11. Lambda expressions
  3. ↑ Sutter, Herb Trip Report: ISO C ++ Spring 2013 Meeting (Neopr.) . isocpp.org (April 20, 2013). Date of treatment June 14, 2013.
  4. ↑ Description in the JavaScript Kernel Reference
  5. ↑ ECMAScript Language Specification Edition 3 (Neopr.) 79 (March 24, 2000).
  6. ↑ Arrow functions (unspecified) . Mozilla Developer Network. Date of treatment December 30, 2015.
  7. ↑ Mathematica Documentation: Function (&) Archived on April 5, 2007.
  8. ↑ perldoc perlref
  9. ↑ PHP Digest No. 152 (March 11 - 25, 2019)
  10. ↑ M. Zandstra, “PHP Objects, Patterns, and Practice”, second edition, Ed. Apress, 2008.
  11. ↑ PHP Manual
  12. ↑ PHP Manual
  13. ↑ Section of the tutorial, “Master Python in 24 hours on your own”. Archived on April 30, 2006.
  14. ↑ Description in the book “Programming Ruby” Archived on April 11, 2006. (eng.)
Source - https://ru.wikipedia.org/w/index.php?title=Anonymous_function&oldid=101385258


More articles:

  • Gemini-6A
  • Here Lies One Whose Name Was Written in Water
  • Toll-like receptor 10
  • Escalator
  • Korman, Boris Osipovich
  • Perevertkin, Semyon Nikiforovich
  • Elva kvinnor i ett hus
  • Vienna Ball
  • Copa Libertadores 1961
  • Cladonia

All articles

Clever Geek | 2019