The Syntax of Funs
2013-11-14 00:00
113 查看
Funs are written with the syntax:
This creates an anonymous function of N arguments and binds it to the variable F.
If we have already written a function in the same module and wish to pass this function as an argument, we can use the following syntax:
With this form of function reference, the function which is referred to does not need to be exported from the module.
We can also refer to a function defined in a different module with the following syntax:
In this case, the function must be exported from the module in question.
The follow program illustrates the different ways of creating funs:
We can evaluate the fun F with the syntax:
To check whether a term is a fun, use the test is_function/1 in a guard. Example:
Funs are a distinct type. The BIFs erlang:fun_info/1,2 can be used to retrieve information about a fun, and the BIF erlang:fun_to_list/1 returns a textual representation of a fun. The check_process_code/2 BIF returns true if the process contains funs that depend on the old version of a module.
F = fun (Arg1, Arg2, ... ArgN) -> ... end
This creates an anonymous function of N arguments and binds it to the variable F.
If we have already written a function in the same module and wish to pass this function as an argument, we can use the following syntax:
F = fun FunctionName/Arity
With this form of function reference, the function which is referred to does not need to be exported from the module.
We can also refer to a function defined in a different module with the following syntax:
F = {Module, FunctionName}
In this case, the function must be exported from the module in question.
The follow program illustrates the different ways of creating funs:
-module(fun_test). -export([t1/0, t2/0, t3/0, t4/0, double/1]). -import(lists, [map/2]). t1() -> map(fun(X) -> 2 * X end, [1,2,3,4,5]). t2() -> map(fun double/1, [1,2,3,4,5]). t3() -> map({?MODULE, double}, [1,2,3,4,5]). double(X) -> X * 2.
We can evaluate the fun F with the syntax:
F(Arg1, Arg2, ..., Argn)
To check whether a term is a fun, use the test is_function/1 in a guard. Example:
f(F, Args) when is_function(F) -> apply(F, Args); f(N, _) when is_integer(N) -> N.
Funs are a distinct type. The BIFs erlang:fun_info/1,2 can be used to retrieve information about a fun, and the BIF erlang:fun_to_list/1 returns a textual representation of a fun. The check_process_code/2 BIF returns true if the process contains funs that depend on the old version of a module.
相关文章推荐
- The syntax of the string representation of a datetime value is incorrect. SQLSTATE=22007
- Setting an Oracle event:The structure of the trace syntax
- The vim syntax of systemd unit file
- Comparing the syntax of Java 5 and ActionScript 3
- IF EXIST: The syntax of the command is incorrect.
- Review the syntax of using sp_MsForEachDb
- The syntax of C in Backus-Naur form
- The syntax of doctests
- Examples of location paths using the unabbreviated syntax
- In C++, which of the following syntax can be used for instantiating an object named arr of the vecto
- In which of the following does a C++ developer use the placement new syntax to make new allocate an
- The syntax of a uses clause(uses 子句的语法)
- Prefer Compiler Error to Runtime Error - taking advantage of the syntax check flag
- python(2)-------The syntax of python
- The future of R syntax?
- The Details About Syntax of gcc as and ld
- Question 24: Which of the following template declarations provide the correct syntax to write a template class
- Setting an Oracle event:The structure of the trace syntax
- The syntax for stack versus heap allocation of C++, C++/CLI and CSharp
- [Bat] Error: The syntax of the command is incorrect.