2014-03-31 09:57:19 +00:00
|
|
|
|
---
|
|
|
|
|
contributors:
|
|
|
|
|
- ["Joao Marques", "http://github.com/mrshankly"]
|
2014-03-31 20:19:56 +00:00
|
|
|
|
translators:
|
2014-03-31 11:49:46 +00:00
|
|
|
|
- ["lidashuang", "http://github.com/lidashuang"]
|
2014-03-31 20:19:56 +00:00
|
|
|
|
filename: learnelixir-cn.ex
|
2014-03-31 09:57:19 +00:00
|
|
|
|
---
|
|
|
|
|
|
2014-05-06 13:24:11 +00:00
|
|
|
|
Elixir 是一门构建在Erlang VM 之上的函数式编程语言。Elixir 完全兼容 Erlang,
|
2014-03-31 11:49:46 +00:00
|
|
|
|
另外还提供了更标准的语法,特性。
|
2014-03-31 09:57:19 +00:00
|
|
|
|
|
2014-03-31 11:49:46 +00:00
|
|
|
|
```elixir
|
|
|
|
|
# 这是单行注释, 注释以井号开头
|
2014-03-31 09:57:19 +00:00
|
|
|
|
|
2014-03-31 11:49:46 +00:00
|
|
|
|
# 没有多行注释
|
|
|
|
|
# 但你可以堆叠多个注释。
|
2014-03-31 09:57:19 +00:00
|
|
|
|
|
2014-03-31 11:49:46 +00:00
|
|
|
|
# elixir shell 使用命令 `iex` 进入。
|
|
|
|
|
# 编译模块使用 `elixirc` 命令。
|
2014-03-31 09:57:19 +00:00
|
|
|
|
|
2014-03-31 11:49:46 +00:00
|
|
|
|
# 如果安装正确,这些命令都会在环境变量里
|
2014-03-31 09:57:19 +00:00
|
|
|
|
|
|
|
|
|
## ---------------------------
|
2014-03-31 11:49:46 +00:00
|
|
|
|
## -- 基本类型
|
2014-03-31 09:57:19 +00:00
|
|
|
|
## ---------------------------
|
|
|
|
|
|
2014-03-31 11:49:46 +00:00
|
|
|
|
# 数字
|
|
|
|
|
3 # 整型
|
|
|
|
|
0x1F # 整型
|
|
|
|
|
3.0 # 浮点类型
|
2014-03-31 09:57:19 +00:00
|
|
|
|
|
2014-03-31 11:49:46 +00:00
|
|
|
|
# 原子(Atoms),以 `:`开头
|
2014-03-31 09:57:19 +00:00
|
|
|
|
:hello # atom
|
|
|
|
|
|
2014-03-31 15:25:19 +00:00
|
|
|
|
# 元组(Tuple) 在内存中的存储是连续的
|
2014-03-31 09:57:19 +00:00
|
|
|
|
{1,2,3} # tuple
|
|
|
|
|
|
2014-03-31 11:49:46 +00:00
|
|
|
|
# 使用`elem`函数访问元组(tuple)里的元素:
|
2014-03-31 09:57:19 +00:00
|
|
|
|
elem({1, 2, 3}, 0) #=> 1
|
|
|
|
|
|
2014-03-31 11:49:46 +00:00
|
|
|
|
# 列表(list)
|
2014-03-31 09:57:19 +00:00
|
|
|
|
[1,2,3] # list
|
|
|
|
|
|
2014-03-31 11:49:46 +00:00
|
|
|
|
# 可以用下面的方法访问列表的头尾元素:
|
2014-03-31 09:57:19 +00:00
|
|
|
|
[head | tail] = [1,2,3]
|
|
|
|
|
head #=> 1
|
|
|
|
|
tail #=> [2,3]
|
|
|
|
|
|
2014-03-31 11:49:46 +00:00
|
|
|
|
# 在elixir,就像在Erlang, `=` 表示模式匹配 (pattern matching)
|
|
|
|
|
# 不是赋值。
|
2014-03-31 09:57:19 +00:00
|
|
|
|
#
|
2014-03-31 15:25:19 +00:00
|
|
|
|
# 这表示会用左边的模式(pattern)匹配右侧
|
|
|
|
|
#
|
2014-04-29 07:11:59 +00:00
|
|
|
|
# 上面的例子中访问列表的头部和尾部就是这样工作的。
|
2014-03-31 09:57:19 +00:00
|
|
|
|
|
2014-03-31 11:49:46 +00:00
|
|
|
|
# 当左右两边不匹配时,会返回error, 在这个
|
|
|
|
|
# 例子中,元组大小不一样。
|
2014-03-31 09:57:19 +00:00
|
|
|
|
# {a, b, c} = {1, 2} #=> ** (MatchError) no match of right hand side value: {1,2}
|
|
|
|
|
|
2014-03-31 11:49:46 +00:00
|
|
|
|
# 还有二进制类型 (binaries)
|
2014-03-31 09:57:19 +00:00
|
|
|
|
<<1,2,3>> # binary
|
|
|
|
|
|
2014-03-31 11:49:46 +00:00
|
|
|
|
# 字符串(Strings) 和 字符列表(char lists)
|
2014-03-31 09:57:19 +00:00
|
|
|
|
"hello" # string
|
|
|
|
|
'hello' # char list
|
|
|
|
|
|
2014-03-31 11:49:46 +00:00
|
|
|
|
# 多行字符串
|
2014-03-31 09:57:19 +00:00
|
|
|
|
"""
|
|
|
|
|
I'm a multi-line
|
|
|
|
|
string.
|
|
|
|
|
"""
|
|
|
|
|
#=> "I'm a multi-line\nstring.\n"
|
|
|
|
|
|
2014-03-31 11:49:46 +00:00
|
|
|
|
# 所有的字符串(Strings)以UTF-8编码:
|
2014-03-31 09:57:19 +00:00
|
|
|
|
"héllò" #=> "héllò"
|
|
|
|
|
|
2014-03-31 11:49:46 +00:00
|
|
|
|
# 字符串(Strings)本质就是二进制类型(binaries), 字符列表(char lists)本质是列表(lists)
|
2014-03-31 09:57:19 +00:00
|
|
|
|
<<?a, ?b, ?c>> #=> "abc"
|
|
|
|
|
[?a, ?b, ?c] #=> 'abc'
|
|
|
|
|
|
2014-03-31 11:49:46 +00:00
|
|
|
|
# 在 elixir中,`?a`返回 `a` 的 ASCII 整型值
|
2014-03-31 09:57:19 +00:00
|
|
|
|
?a #=> 97
|
|
|
|
|
|
2014-03-31 11:49:46 +00:00
|
|
|
|
# 合并列表使用 `++`, 对于二进制类型则使用 `<>`
|
2014-03-31 09:57:19 +00:00
|
|
|
|
[1,2,3] ++ [4,5] #=> [1,2,3,4,5]
|
|
|
|
|
'hello ' ++ 'world' #=> 'hello world'
|
|
|
|
|
|
|
|
|
|
<<1,2,3>> <> <<4,5>> #=> <<1,2,3,4,5>>
|
|
|
|
|
"hello " <> "world" #=> "hello world"
|
|
|
|
|
|
|
|
|
|
## ---------------------------
|
2014-03-31 11:49:46 +00:00
|
|
|
|
## -- 操作符(Operators)
|
2014-03-31 09:57:19 +00:00
|
|
|
|
## ---------------------------
|
|
|
|
|
|
2014-03-31 11:49:46 +00:00
|
|
|
|
# 一些数学运算
|
2014-03-31 09:57:19 +00:00
|
|
|
|
1 + 1 #=> 2
|
|
|
|
|
10 - 5 #=> 5
|
|
|
|
|
5 * 2 #=> 10
|
|
|
|
|
10 / 2 #=> 5.0
|
|
|
|
|
|
2014-04-29 07:11:59 +00:00
|
|
|
|
# 在 elixir 中,操作符 `/` 返回值总是浮点数。
|
2014-03-31 09:57:19 +00:00
|
|
|
|
|
2014-03-31 11:49:46 +00:00
|
|
|
|
# 做整数除法使用 `div`
|
2014-03-31 09:57:19 +00:00
|
|
|
|
div(10, 2) #=> 5
|
|
|
|
|
|
2014-03-31 11:49:46 +00:00
|
|
|
|
# 为了得到余数使用 `rem`
|
2014-03-31 09:57:19 +00:00
|
|
|
|
rem(10, 3) #=> 1
|
|
|
|
|
|
2014-03-31 11:49:46 +00:00
|
|
|
|
# 还有 boolean 操作符: `or`, `and` and `not`.
|
|
|
|
|
# 第一个参数必须是boolean 类型
|
2014-03-31 09:57:19 +00:00
|
|
|
|
true and true #=> true
|
|
|
|
|
false or true #=> true
|
|
|
|
|
# 1 and true #=> ** (ArgumentError) argument error
|
|
|
|
|
|
2014-03-31 11:49:46 +00:00
|
|
|
|
# Elixir 也提供了 `||`, `&&` 和 `!` 可以接受任意的类型
|
|
|
|
|
# 除了`false` 和 `nil` 其它都会被当作true.
|
2014-03-31 09:57:19 +00:00
|
|
|
|
1 || true #=> 1
|
|
|
|
|
false && 1 #=> false
|
|
|
|
|
nil && 20 #=> nil
|
|
|
|
|
|
|
|
|
|
!true #=> false
|
|
|
|
|
|
2014-03-31 11:49:46 +00:00
|
|
|
|
# 比较有: `==`, `!=`, `===`, `!==`, `<=`, `>=`, `<` 和 `>`
|
2014-03-31 09:57:19 +00:00
|
|
|
|
1 == 1 #=> true
|
|
|
|
|
1 != 1 #=> false
|
|
|
|
|
1 < 2 #=> true
|
|
|
|
|
|
2014-03-31 11:49:46 +00:00
|
|
|
|
# `===` 和 `!==` 在比较整型和浮点类型时更为严格:
|
2014-03-31 09:57:19 +00:00
|
|
|
|
1 == 1.0 #=> true
|
|
|
|
|
1 === 1.0 #=> false
|
|
|
|
|
|
2014-03-31 11:49:46 +00:00
|
|
|
|
# 我们也可以比较两种不同的类型:
|
2014-03-31 09:57:19 +00:00
|
|
|
|
1 < :hello #=> true
|
|
|
|
|
|
2014-03-31 11:49:46 +00:00
|
|
|
|
# 总的排序顺序定义如下:
|
2014-03-31 09:57:19 +00:00
|
|
|
|
# number < atom < reference < functions < port < pid < tuple < list < bit string
|
|
|
|
|
|
2014-04-29 07:11:59 +00:00
|
|
|
|
# 引用Joe Armstrong :“实际的顺序并不重要,
|
|
|
|
|
# 但是,一个整体排序是否经明确界定是非常重要的。”
|
2014-03-31 09:57:19 +00:00
|
|
|
|
|
|
|
|
|
## ---------------------------
|
2014-03-31 11:49:46 +00:00
|
|
|
|
## -- 控制结构(Control Flow)
|
2014-03-31 09:57:19 +00:00
|
|
|
|
## ---------------------------
|
|
|
|
|
|
2014-03-31 11:49:46 +00:00
|
|
|
|
# `if` 表达式
|
2014-03-31 09:57:19 +00:00
|
|
|
|
if false do
|
|
|
|
|
"This will never be seen"
|
|
|
|
|
else
|
|
|
|
|
"This will"
|
|
|
|
|
end
|
|
|
|
|
|
2014-03-31 11:49:46 +00:00
|
|
|
|
# 还有 `unless`
|
2014-03-31 09:57:19 +00:00
|
|
|
|
unless true do
|
|
|
|
|
"This will never be seen"
|
|
|
|
|
else
|
|
|
|
|
"This will"
|
|
|
|
|
end
|
|
|
|
|
|
2014-03-31 15:25:19 +00:00
|
|
|
|
# 在Elixir中,很多控制结构都依赖于模式匹配
|
2014-03-31 09:57:19 +00:00
|
|
|
|
|
2014-03-31 15:25:19 +00:00
|
|
|
|
# `case` 允许我们把一个值与多种模式进行比较:
|
2014-03-31 09:57:19 +00:00
|
|
|
|
case {:one, :two} do
|
|
|
|
|
{:four, :five} ->
|
|
|
|
|
"This won't match"
|
|
|
|
|
{:one, x} ->
|
|
|
|
|
"This will match and assign `x` to `:two`"
|
|
|
|
|
_ ->
|
|
|
|
|
"This will match any value"
|
|
|
|
|
end
|
|
|
|
|
|
2014-03-31 11:49:46 +00:00
|
|
|
|
# 模式匹配时,如果不需要某个值,通用的做法是把值 匹配到 `_`
|
|
|
|
|
# 例如,我们只需要要列表的头元素:
|
2014-03-31 09:57:19 +00:00
|
|
|
|
[head | _] = [1,2,3]
|
|
|
|
|
head #=> 1
|
|
|
|
|
|
2014-03-31 11:49:46 +00:00
|
|
|
|
# 下面的方式效果一样,但可读性更好
|
2014-03-31 09:57:19 +00:00
|
|
|
|
[head | _tail] = [:a, :b, :c]
|
|
|
|
|
head #=> :a
|
|
|
|
|
|
2014-03-31 15:25:19 +00:00
|
|
|
|
# `cond` 可以检测多种不同的分支
|
|
|
|
|
# 使用 `cond` 代替多个`if` 表达式嵌套
|
2014-03-31 09:57:19 +00:00
|
|
|
|
cond do
|
|
|
|
|
1 + 1 == 3 ->
|
|
|
|
|
"I will never be seen"
|
|
|
|
|
2 * 5 == 12 ->
|
|
|
|
|
"Me neither"
|
|
|
|
|
1 + 2 == 3 ->
|
|
|
|
|
"But I will"
|
|
|
|
|
end
|
|
|
|
|
|
2014-03-31 15:25:19 +00:00
|
|
|
|
# 经常可以看到最后一个条件等于'true',这将总是匹配。
|
2014-03-31 09:57:19 +00:00
|
|
|
|
cond do
|
|
|
|
|
1 + 1 == 3 ->
|
|
|
|
|
"I will never be seen"
|
|
|
|
|
2 * 5 == 12 ->
|
|
|
|
|
"Me neither"
|
|
|
|
|
true ->
|
|
|
|
|
"But I will (this is essentially an else)"
|
|
|
|
|
end
|
|
|
|
|
|
2014-03-31 15:25:19 +00:00
|
|
|
|
# `try/catch` 用于捕获被抛出的值, 它也支持 `after` 子句,
|
|
|
|
|
# 无论是否值被捕获,after 子句都会被调用
|
|
|
|
|
# `try/catch`
|
2014-03-31 09:57:19 +00:00
|
|
|
|
try do
|
|
|
|
|
throw(:hello)
|
|
|
|
|
catch
|
|
|
|
|
message -> "Got #{message}."
|
|
|
|
|
after
|
|
|
|
|
IO.puts("I'm the after clause.")
|
|
|
|
|
end
|
|
|
|
|
#=> I'm the after clause
|
|
|
|
|
# "Got :hello"
|
|
|
|
|
|
|
|
|
|
## ---------------------------
|
2014-03-31 11:49:46 +00:00
|
|
|
|
## -- 模块和函数(Modules and Functions)
|
2014-03-31 09:57:19 +00:00
|
|
|
|
## ---------------------------
|
|
|
|
|
|
2014-03-31 11:49:46 +00:00
|
|
|
|
# 匿名函数 (注意点)
|
2014-03-31 09:57:19 +00:00
|
|
|
|
square = fn(x) -> x * x end
|
|
|
|
|
square.(5) #=> 25
|
|
|
|
|
|
2014-03-31 15:25:19 +00:00
|
|
|
|
|
|
|
|
|
# 也支持接收多个子句和卫士(guards).
|
2014-04-29 07:11:59 +00:00
|
|
|
|
# Guards 可以进行模式匹配
|
|
|
|
|
# Guards 使用 `when` 关键字指明:
|
2014-03-31 09:57:19 +00:00
|
|
|
|
f = fn
|
|
|
|
|
x, y when x > 0 -> x + y
|
|
|
|
|
x, y -> x * y
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
f.(1, 3) #=> 4
|
|
|
|
|
f.(-1, 3) #=> -3
|
|
|
|
|
|
2014-03-31 11:49:46 +00:00
|
|
|
|
# Elixir 提供了很多内建函数
|
|
|
|
|
# 在默认作用域都是可用的
|
2014-03-31 09:57:19 +00:00
|
|
|
|
is_number(10) #=> true
|
|
|
|
|
is_list("hello") #=> false
|
|
|
|
|
elem({1,2,3}, 0) #=> 1
|
|
|
|
|
|
2014-03-31 11:49:46 +00:00
|
|
|
|
# 你可以在一个模块里定义多个函数,定义函数使用 `def`
|
2014-03-31 09:57:19 +00:00
|
|
|
|
defmodule Math do
|
|
|
|
|
def sum(a, b) do
|
|
|
|
|
a + b
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def square(x) do
|
|
|
|
|
x * x
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
Math.sum(1, 2) #=> 3
|
|
|
|
|
Math.square(3) #=> 9
|
|
|
|
|
|
2014-03-31 11:49:46 +00:00
|
|
|
|
# 保存到 `math.ex`,使用 `elixirc` 编译你的 Math 模块
|
|
|
|
|
# 在终端里: elixirc math.ex
|
2014-03-31 09:57:19 +00:00
|
|
|
|
|
2014-03-31 11:49:46 +00:00
|
|
|
|
# 在模块中可以使用`def`定义函数,使用 `defp` 定义私有函数
|
|
|
|
|
# 使用`def` 定义的函数可以被其它模块调用
|
|
|
|
|
# 私有函数只能在本模块内调用
|
2014-03-31 09:57:19 +00:00
|
|
|
|
defmodule PrivateMath do
|
|
|
|
|
def sum(a, b) do
|
|
|
|
|
do_sum(a, b)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
defp do_sum(a, b) do
|
|
|
|
|
a + b
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
PrivateMath.sum(1, 2) #=> 3
|
|
|
|
|
# PrivateMath.do_sum(1, 2) #=> ** (UndefinedFunctionError)
|
|
|
|
|
|
2014-04-29 07:11:59 +00:00
|
|
|
|
|
|
|
|
|
# 函数定义同样支持 guards 和 多重子句:
|
2014-03-31 09:57:19 +00:00
|
|
|
|
defmodule Geometry do
|
|
|
|
|
def area({:rectangle, w, h}) do
|
|
|
|
|
w * h
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def area({:circle, r}) when is_number(r) do
|
|
|
|
|
3.14 * r * r
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
Geometry.area({:rectangle, 2, 3}) #=> 6
|
|
|
|
|
Geometry.area({:circle, 3}) #=> 28.25999999999999801048
|
|
|
|
|
# Geometry.area({:circle, "not_a_number"})
|
|
|
|
|
#=> ** (FunctionClauseError) no function clause matching in Geometry.area/1
|
|
|
|
|
|
2014-03-31 11:49:46 +00:00
|
|
|
|
#由于不变性,递归是Elixir的重要组成部分
|
2014-03-31 09:57:19 +00:00
|
|
|
|
defmodule Recursion do
|
|
|
|
|
def sum_list([head | tail], acc) do
|
|
|
|
|
sum_list(tail, acc + head)
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
def sum_list([], acc) do
|
|
|
|
|
acc
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
Recursion.sum_list([1,2,3], 0) #=> 6
|
|
|
|
|
|
2014-03-31 14:24:09 +00:00
|
|
|
|
# Elixir 模块支持属性,模块内建了一些属性,你也可以自定义属性
|
2014-03-31 09:57:19 +00:00
|
|
|
|
defmodule MyMod do
|
|
|
|
|
@moduledoc """
|
2014-03-31 14:24:09 +00:00
|
|
|
|
内置的属性,模块文档
|
2014-03-31 09:57:19 +00:00
|
|
|
|
"""
|
|
|
|
|
|
2014-03-31 14:24:09 +00:00
|
|
|
|
@my_data 100 # 自定义属性
|
2014-03-31 09:57:19 +00:00
|
|
|
|
IO.inspect(@my_data) #=> 100
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
## ---------------------------
|
2014-03-31 14:12:03 +00:00
|
|
|
|
## -- 记录和异常(Records and Exceptions)
|
2014-03-31 09:57:19 +00:00
|
|
|
|
## ---------------------------
|
|
|
|
|
|
2014-03-31 15:25:19 +00:00
|
|
|
|
# 记录就是把特定值关联到某个名字的结构体
|
2014-03-31 09:57:19 +00:00
|
|
|
|
defrecord Person, name: nil, age: 0, height: 0
|
|
|
|
|
|
|
|
|
|
joe_info = Person.new(name: "Joe", age: 30, height: 180)
|
|
|
|
|
#=> Person[name: "Joe", age: 30, height: 180]
|
|
|
|
|
|
2014-03-31 11:49:46 +00:00
|
|
|
|
# 访问name的值
|
2014-03-31 09:57:19 +00:00
|
|
|
|
joe_info.name #=> "Joe"
|
|
|
|
|
|
2014-03-31 14:12:03 +00:00
|
|
|
|
# 更新age的值
|
2014-03-31 09:57:19 +00:00
|
|
|
|
joe_info = joe_info.age(31) #=> Person[name: "Joe", age: 31, height: 180]
|
|
|
|
|
|
2014-03-31 14:24:09 +00:00
|
|
|
|
# 使用 `try` `rescue` 进行异常处理
|
2014-03-31 09:57:19 +00:00
|
|
|
|
try do
|
|
|
|
|
raise "some error"
|
|
|
|
|
rescue
|
|
|
|
|
RuntimeError -> "rescued a runtime error"
|
|
|
|
|
_error -> "this will rescue any error"
|
|
|
|
|
end
|
|
|
|
|
|
2014-03-31 14:24:09 +00:00
|
|
|
|
# 所有的异常都有一个message
|
2014-03-31 09:57:19 +00:00
|
|
|
|
try do
|
|
|
|
|
raise "some error"
|
|
|
|
|
rescue
|
|
|
|
|
x in [RuntimeError] ->
|
|
|
|
|
x.message
|
|
|
|
|
end
|
|
|
|
|
|
|
|
|
|
## ---------------------------
|
2014-03-31 11:49:46 +00:00
|
|
|
|
## -- 并发(Concurrency)
|
2014-03-31 09:57:19 +00:00
|
|
|
|
## ---------------------------
|
|
|
|
|
|
2014-04-29 07:11:59 +00:00
|
|
|
|
# Elixir 依赖于 actor并发模型。在Elixir编写并发程序的三要素:
|
2014-03-31 14:24:09 +00:00
|
|
|
|
# 创建进程,发送消息,接收消息
|
2014-03-31 09:57:19 +00:00
|
|
|
|
|
2014-03-31 14:12:03 +00:00
|
|
|
|
# 启动一个新的进程使用`spawn`函数,接收一个函数作为参数
|
2014-03-31 14:24:09 +00:00
|
|
|
|
|
2014-03-31 09:57:19 +00:00
|
|
|
|
f = fn -> 2 * 2 end #=> #Function<erl_eval.20.80484245>
|
|
|
|
|
spawn(f) #=> #PID<0.40.0>
|
|
|
|
|
|
2014-03-31 14:24:09 +00:00
|
|
|
|
|
|
|
|
|
# `spawn` 函数返回一个pid(进程标识符),你可以使用pid向进程发送消息。
|
|
|
|
|
# 使用 `<-` 操作符发送消息。
|
|
|
|
|
# 我们需要在进程内接收消息,要用到 `receive` 机制。
|
|
|
|
|
|
2014-03-31 09:57:19 +00:00
|
|
|
|
defmodule Geometry do
|
|
|
|
|
def area_loop do
|
|
|
|
|
receive do
|
|
|
|
|
{:rectangle, w, h} ->
|
|
|
|
|
IO.puts("Area = #{w * h}")
|
|
|
|
|
area_loop()
|
|
|
|
|
{:circle, r} ->
|
|
|
|
|
IO.puts("Area = #{3.14 * r * r}")
|
|
|
|
|
area_loop()
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
end
|
|
|
|
|
|
2014-03-31 14:12:03 +00:00
|
|
|
|
# 编译这个模块,在shell中创建一个进程,并执行 `area_looop` 函数。
|
2014-03-31 09:57:19 +00:00
|
|
|
|
pid = spawn(fn -> Geometry.area_loop() end) #=> #PID<0.40.0>
|
|
|
|
|
|
2014-03-31 14:12:03 +00:00
|
|
|
|
# 发送一个消息给 `pid`, 会在receive语句进行模式匹配
|
2014-03-31 09:57:19 +00:00
|
|
|
|
pid <- {:rectangle, 2, 3}
|
|
|
|
|
#=> Area = 6
|
|
|
|
|
# {:rectangle,2,3}
|
|
|
|
|
|
|
|
|
|
pid <- {:circle, 2}
|
|
|
|
|
#=> Area = 12.56000000000000049738
|
|
|
|
|
# {:circle,2}
|
|
|
|
|
|
2014-03-31 11:49:46 +00:00
|
|
|
|
# shell也是一个进程(process), 你可以使用`self`获取当前 pid
|
2014-03-31 09:57:19 +00:00
|
|
|
|
self() #=> #PID<0.27.0>
|
|
|
|
|
```
|
|
|
|
|
|
2014-03-31 14:12:03 +00:00
|
|
|
|
## 参考文献
|
2014-03-31 09:57:19 +00:00
|
|
|
|
|
|
|
|
|
* [Getting started guide](http://elixir-lang.org/getting_started/1.html) from [elixir webpage](http://elixir-lang.org)
|
|
|
|
|
* [Elixir Documentation](http://elixir-lang.org/docs/master/)
|
|
|
|
|
* ["Learn You Some Erlang for Great Good!"](http://learnyousomeerlang.com/) by Fred Hebert
|
|
|
|
|
* "Programming Erlang: Software for a Concurrent World" by Joe Armstrong
|