1. Anonymous fun function
A. Anonymous functions and variable bindings
What we generally see is assigning an anonymous function to a variable: the equivalent of this variable is the function name of the future.
This variable is then used as the function name. For example:
Double=fun (X)->2*x end. Double (2). The result is 1.
B. Anonymous fun function as function parameter
L=[1,2,3,4].list:map (Fun (X)->2*x end,l).
In short, to be anonymous function, you must bind to the variable, or as a parameter is passed to other functions, and also must not forget that the function body of the anonymous function has an end as its end.
2. The fun function of function aliases
A. This module applies
If you want to apply a function that is already defined inside this module, pass it directly to another function as a parameter, in the following form
Make (Name), register (Name,spawn ()->my_handler (fun NO_OP/1)). No_op (_)->void.my_handler (fun)-&G T Receive {event,any}-> catch fun (any) end.
The fun no_op/1 of the My_handler () function parameter is quite the function alias of the No_op (_) function. When assigned to a variable, you can call this function with the same alias as you would call any normal function.
Note: The local alias fun function is similar to the anonymous happy function in implementation, and they all depend on the current version of the module.
B. Other module applications
1-module (Test). 2 3-export ([TEST/1]). 4 5 test (x) when x > 2-true; 6 7 Test (X), false.
If applied in the shell:
Note: When called, they are not dependent on a specific version. Instead, it always points to the latest version of the reference function.
Summary of data types for fun-functions