Erlang
无线程,轻量级进程:摆脱了共享资源和性能瓶颈
可靠性:Let it crash、hot reload 可以运行多年不宕机
小写是原子,大写开头是变量
1two == two.2% true3Var = 1.
变量不可变,只能被赋值一次
列表与元组
1[1, two, "three"].2{1, two, "three"}.
模式匹配
1Person = {ahabhgk, 19}.2{Name, Age} = Person.34[One, Two | Rest] = [1, 2, 3].5One. % 16Two. % 27Rest. % [3]
位匹配
函数
1% basic.erl2-module(basic). % 模块名3-export([number/1]). % 暴露出的函数,/1 表示有一个参数4-export([fib/1]).56number(one) -> 1;7number(two) -> 2;8number(three) -> 3.910fib(0) -> 1;11fib(1) -> 1;12fib(N) -> fib(N - 1) + fib(N - 2).
1$ erl2> c(basic).3{ok,basic}4> basic:fib(10).589
case、if
1Animal = "dog"2case Animal of3 "cat" -> thundercat;4 "elephant" -> dumbo5 _ -> something_else6end.78X = 09if10 X > 0 -> positive;11 X < 0 -> negative;12 true -> zero13end.
匿名函数
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).34% map(F, [Head | Tail]) -> [F(Head) | map(F, Tail)];5% map(F, []) -> [].67Adder = 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]].67% map 定义8% map(F, L) -> [F(X) || X <- L].910% 可以有多个子句,子句可以是 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]).34loop() ->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$ erl2> c(translate).3> Pid = spawn(fun translate:loop/0). % 通过 spawn 启动进程4# 可以通过其他形式定义,比如不用 Pid 而用名字注册进程5# 还有支持 hot reload 的形式6# 远程进程 spawn(Node, function).7> Pid ! "casa".8house9"casa"10> Pid ! "haa".11I dont understand.12"haa"13# 分布式消息发送给命名资源 node@server ! message
同步消息
1-module(translate_service).2-export([loop/0, translate/2]).34loop() ->5 receive6 {From, "casa"} ->7 From ! "house",8 loop();910 {From, "blanca"} ->11 From ! "white",12 loop();1314 {From, _} ->15 From ! "I do not understand.",16 loop()17 end.1819translate(To, Word) ->20 To ! {self(), Word},21 receive22 Translation -> Translation23 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.erl2-module(roulette).3-export([loop/0]).45loop() ->6 receive7 3 -> io:format("bang.~n"), exit({roulette, die, at, erlang:time()});8 _ -> io:format("click~n"), loop()9 end.
1% doctor.erl2-module(doctor).3-export([loop/0]).45loop() ->6 process_flag(trap_exit, true),7 receive8 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$ erl2> 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.9click10111> revolver ! 3.12bang.13314the shooter <0.88.0> died with reason {roulette,die,at,{21,43,19}}.restarting.15creating and monitoring process.16> revolver ! 4.17click184
feeling
动态
可靠性
轻量级进程