博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
The Syntax of Funs
阅读量:6437 次
发布时间:2019-06-23

本文共 1521 字,大约阅读时间需要 5 分钟。

hot3.png

Funs are written with the syntax:

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.

转载于:https://my.oschina.net/u/1263964/blog/176347

你可能感兴趣的文章
java杂乱
查看>>
在Linux上安装Python3.6.1
查看>>
[基础]iOS 可视化编程(全系列)
查看>>
我的友情链接
查看>>
LVS之NAT模型配置实验
查看>>
nginx 报错 99: Cannot assign requested address
查看>>
几种流行的AJAX框架:jQuery,Mootools,Dojo,Ext JS的对比
查看>>
Socket-Client通信
查看>>
understanding shader mat4 * vec4 calculation
查看>>
Maven搭建简单的SS项目
查看>>
#我要上首页# 新版博客首页来了,做明星博主还会远吗?
查看>>
PHP缓存技术
查看>>
关于SOCKET资源堆栈
查看>>
笔记 百度搜索
查看>>
Kebernetes 学习总结(9)认证-授权-RBAC
查看>>
控制台 - 网络管理之华为交换机 S系列端口限速
查看>>
天下会 - 搜索实战系列之视频
查看>>
修改windows远程登录端口
查看>>
ccflow表结构与运行机制(二次开发必读)
查看>>
mysql数据库引擎调优
查看>>