Skip to content

AHABHGK

Erlang

无线程,轻量级进程:摆脱了共享资源和性能瓶颈

可靠性:Let it crash、hot reload 可以运行多年不宕机

小写是原子,大写开头是变量

1two == two.
2% true
3Var = 1.

变量不可变,只能被赋值一次

列表与元组

1[1, two, "three"].
2{1, two, "three"}.

模式匹配

1Person = {ahabhgk, 19}.
2{Name, Age} = Person.
3
4[One, Two | Rest] = [1, 2, 3].
5One. % 1
6Two. % 2
7Rest. % [3]

位匹配

函数

1% basic.erl
2-module(basic). % 模块名
3-export([number/1]). % 暴露出的函数,/1 表示有一个参数
4-export([fib/1]).
5
6number(one) -> 1;
7number(two) -> 2;
8number(three) -> 3.
9
10fib(0) -> 1;
11fib(1) -> 1;
12fib(N) -> fib(N - 1) + fib(N - 2).
1$ erl
2> c(basic).
3{ok,basic}
4> basic:fib(10).
589

case、if

1Animal = "dog"
2case Animal of
3 "cat" -> thundercat;
4 "elephant" -> dumbo
5 _ -> something_else
6end.
7
8X = 0
9if
10 X > 0 -> positive;
11 X < 0 -> negative;
12 true -> zero
13end.

匿名函数

1Negate = fun(I) -> -I end.
2Negate(1). % -1

高阶函数

foreach map all any takewith dropwith foldl

1Numbers = [1, 2, 3, 4].
2lists:map(fun(Number) -> Number + 1 end, Numbers).
3
4% map(F, [Head | Tail]) -> [F(Head) | map(F, Tail)];
5% map(F, []) -> [].
6
7Adder = fun(ListItem, SumSoFor) -> ListItem + SumSoFor end.
8lists:foldl(Adder, 0, Numbers).

列表构造

1[1 | [2, 3]]. % [1, 2, 3]

列表解析

1lists:map(Double, Numbers).
2% 等价于
3[Double(X) || X <- Numbers].
4% 等价于
5[X * 2 || X <- [1, 2, 3, 4]].
6
7% map 定义
8% map(F, L) -> [F(X) || X <- L].
9
10% 可以有多个子句,子句可以是 filter 或 generator(Match <- List)
11[{X, Y} || X <- [1, 2, 3, 4], X < 3, Y <- [5, 6]].
12% [{1, 5}, {1, 6}, {2, 5}, {2, 6}].

并发

1-module(translate).
2-export([loop/0]).
3
4loop() ->
5 receive % receive 用来接收其他进程发过来的消息
6 "casa" ->
7 io:format("house~n"),
8 loop();
9 "blanca" ->
10 io:format("white~n"),
11 loop();
12 _ ->
13 io:format("I dont understand.~n"),
14 loop()
15end.
1$ erl
2> c(translate).
3> Pid = spawn(fun translate:loop/0). % 通过 spawn 启动进程
4# 可以通过其他形式定义,比如不用 Pid 而用名字注册进程
5# 还有支持 hot reload 的形式
6# 远程进程 spawn(Node, function).
7> Pid ! "casa".
8house
9"casa"
10> Pid ! "haa".
11I dont understand.
12"haa"
13# 分布式消息发送给命名资源 node@server ! message

同步消息

1-module(translate_service).
2-export([loop/0, translate/2]).
3
4loop() ->
5 receive
6 {From, "casa"} ->
7 From ! "house",
8 loop();
9
10 {From, "blanca"} ->
11 From ! "white",
12 loop();
13
14 {From, _} ->
15 From ! "I do not understand.",
16 loop()
17 end.
18
19translate(To, Word) ->
20 To ! {self(), Word},
21 receive
22 Translation -> Translation
23 end.
1> Translator = spawn(fun translate_service:loop/0).
2> translate_service:translate(Translator, "blanca").
3"white"
4> translate_service:translate(Translator, "casa").
5"house"

链接进程以获得可靠性

1% roulette.erl
2-module(roulette).
3-export([loop/0]).
4
5loop() ->
6 receive
7 3 -> io:format("bang.~n"), exit({roulette, die, at, erlang:time()});
8 _ -> io:format("click~n"), loop()
9 end.
1% doctor.erl
2-module(doctor).
3-export([loop/0]).
4
5loop() ->
6 process_flag(trap_exit, true),
7 receive
8 new ->
9 io:format("creating and monitoring process.~n"),
10 register(revolver, spawn_link(fun roulette:loop/0)),
11 loop();
12
13 {'EXIT', From, Reason} ->
14 io:format("the shooter ~p died with reason ~p.", [From, Reason]),
15 io:format("restarting.~n"),
16 self() ! new,
17 loop()
18 end.
1$ erl
2> c(doctor).
3> c(roulette).
4> Doc = spawn(fun doctor:loop/0).
5<0.86.0>
6> Doc ! new.
7creating and monitoring process.
8> revolver ! 1.
9click
101
11> revolver ! 3.
12bang.
133
14the shooter <0.88.0> died with reason {roulette,die,at,{21,43,19}}.restarting.
15creating and monitoring process.
16> revolver ! 4.
17click
184

feeling

动态

可靠性

轻量级进程