2.语言结构及术语 (Structure and Erlang Terminology)
2 Structure and Erlang Terminology
语言结构及术语
Structure:组织/结构 在这里应该是代码结构风格的意思
terminology:术语
Erlang systems are divided into modules. Modules are composed of functions and attributes. Functions are either only visible inside a module or they are exported i.e. they can also be called by other functions in other modules. Attributes begin with “-” and are placed in the beginning of a module.
be divided into:被(划)分为
be composed of: 由…组成
be either only or: 要么是….要么是…(不是x就是y)
Erlang 系统 被划分成各种各样的 模块(module)。这些模块由一些 函数 及 属性 组成。 函数 要么是导出的,要么仅是 模块 内部可见的,好比类里面的私有方法(当然Erlang没有类的概念)。导出(-export([]))的函数不仅在模块内部可见,而且在其他模块也可以被调用到,使用 模块名:函数名(参数…) 的方式。属性以 - 横杠开始,并被放置在模块的 头部 (实践证明,这不是必须的,但却是推荐的)。
The work in a system designed using Erlang is done by processes. A process is a job which can use functions in many modules. Processes communicate with each other by sending messages. Processes receive messages which are sent to them, a process can decide which messages it is prepared to receive. Other messages are queued until the receiving process is prepared to receive them.
在Erlang里的开发工作,被设计为使用 进程(processes) 进行操作(实现)。Erlang的进程可以理解为在一次操作中调用一些不同模块里的函数来实现功能的一个工作过程。进程间的通讯是通过互相发送消息( Pid ! hello_world) 进程收到发送给它的消息后,可以决定如何处理它,处理完毕应该做什么,(通常是通过 case receive Msg of … end 这样的操作)。如果同时有很多消息过来,每次只处理一条,其他消息被放置到消息队列中,直到进程处理完当前消息,并再次调用 receive 才会被处理
A process can supervise the existence of another process by setting up a link to it. When a process terminates, it automatically sends exit signals to the process to which it is linked. The default behavior of a process receiving an exit signal is to terminate and to propagate the signal to its linked processes. A process can change this default behavior by trapping exits, this causes all exit signals sent to a process to be turned into messages.
supervise: 监控
terminate: 结束 终止
propagate: 广播
一个Erlang进程可以通过与另外一个存在的进程通过建立 连接(link) 的方式来监控该进程(这个过程是双向的,发起一个连接,双方互相监控)。当一个进程结束了,它会自动的发送一条消息(信号)给与它连接的进程(可以是多个哦)。默认的,一个进程收到一条 进程结束 的消息后,自己也会结束,同样地将自己结束的消息广播给与自己连接的其他进程。当然,如果你不希望你的进程收到别的进程的退出消息时也跟着挂掉,你可以改变它的这种行为,通过 捕获(trap) 退出消息,这个操作将会使发送到该进程的退出消息变为一个普通的消息。
A pure function is a function that returns the same value given the same arguments regardless of the context of the call of the function. This is what we normally expect from a mathematical function. A function that is not pure is said to have side effects.
regardless: 不管 不顾
context: 上下文
side effects: 副作用
一个纯净的函数是表示当给予相同参数时总是返回相同的值,即不依赖于函数调用所处上下文环境的这样一种函数。这是我们通常所期望的逻辑处理函数。不纯净的函数是指那些有副作用的函数。
Side effects typically occur if a function a) sends a message b) receives a message c) calls exit d) calls any BIF which changes a processes environment or mode of operation (e.g. get/1, put/2, erase/1, process_flag/2 etc).
typically: 通常的、有代表性的
副作用通常出现在: a) 函数内部还发送了消息 b) 接收消息(有阻塞及非阻塞) c) 调用exit 函数 d) 调用任何内建函数(BIF, 通常意味着高效执行的函数) 可以改变进程运行环境或者模式的操作(比如get/1获取进程字典、put/2 往进程字典里塞如数据、erase/1擦除进程字典、process_flag/2 改变进程信号处理方式(这个翻译可能不准确))
POSTED ON 2010年08月9日,