标签:des blog http io ar os 使用 sp for
学习Erlang的时候在书的留白处随手记录了一些东西,还有一些记录在了demo的注释里面,今天抽时间整理出来了一部分,分享一下.
12> code:lib_dir(kernel).
"/usr/local/lib/erlang/lib/kernel-3.0"
[1 | 2]
gives what we call an ‘improper list‘. Improper lists will work when you pattern match in the [Head|Tail]
manner, but will fail to be used with standard functions of Erlang (even length()
). This is because Erlang expects proper lists. Proper lists end with an empty list as their last cell. When declaring an item like[2]
, the list is automatically formed in a proper manner. As such, [1|[2]]
would work! Improper lists, although syntactically valid, are of very limited use outside of user-defined data structures.在EShell中执行Erlang方法
$> erl -boot start_clean -noshell -eval ‘io:format("hi\n")‘ -eval ‘halt(0)‘
% or
$> erl -boot start_clean -noshell -eval ‘io:format("hi\n"), halt(0)‘
% example:
erl -sname ad_service -eval ‘ok=mnesia:create_schema([node()]).‘ -s init stop
打印浮点型
lists:flatten(io_lib:format("~.*..f", [2, S]));
3> lists:flatten(io_lib:format("~.*..f", [2, 192.2225])).
"192.22"
4> lists:flatten(io_lib:format("~.*..f", [3, 192.2225])).
"192.223"
5> lists:flatten([io_lib:format("~8.2.0B,", [L]) || L <- [1,2,3]]).
"00000001,00000010,00000011,"
6> lists:flatten([io_lib:format("~2.16.0B ", [L]) || L <- [1,2,3]]).
"01 02 03 ".
找出消耗内存最多的进程
lists:reverse(lists:keysort(2,[{P, erlang:process_info(P, heap_size)} || P <- erlang:processes()])).
1
2
3
4
5
6
|
flush() -> receive _ -> flush() after 0 -> ok end. |
找到最消耗内存的ETS表
lists:reverse(lists:keysort(2,[{T, ets:info(T, memory)} || T <- ets:all()])).
record类型作为参数的小技巧
-record(x,{name,zz}).
-record(y,{yy,name}).
-export([test1/0,test2/0]).
-define(create(Type,Name),#Type{name = Name}).
test1() -> ?create(x,"Noel"). % -> {x,"Noel",undefined}
test2() -> ?create(y,"Noel"). % -> {y,undefined,"Noel"}
binary_to_list VS bitstring_to_list
1> A = <<1:2, 23:6>>.
<<"W">>
2> B = <<1:2, 23:5>>.
<<55:7>>
3> binary_to_list(A).
"W"
4> binary_to_list(B).
** exception error: bad argument
in function binary_to_list/1
called as binary_to_list(<<55:7>>)
5> bitstring_to_list(A).
"W"
6> bitstring_to_list(B).
[<<55:7>>]
Erlang执行操作系统命令 os:cmd("uptime").
[3] or "3"
17> [51] =:= "3".
true
18> [3] =:= "3".
false
19>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
|
26> MyTerm = {atom, << "binary" >>, 1}. {atom,<< "binary" >>,1} 27> MyList = [ MyTerm || _ <- lists:seq(1, 100) ]. [{atom,<< "binary" >>,1}, {atom,<< "binary" >>,1}, {atom,<< "binary" >>,1}, {atom,<< "binary" >>,1}, {atom,<< "binary" >>,1}, {atom,<< "binary" >>,1}, {atom,<< "binary" >>,1}, {atom,<< "binary" >>,1}, {atom,<< "binary" >>,1}, {atom,<< "binary" >>,1}, {atom,<< "binary" >>,1}, {atom,<< "binary" >>,1}, {atom,<< "binary" >>,1}, {atom,<< "binary" >>,1}, {atom,<< "binary" >>,1}, {atom,<< "binary" >>,1}, {atom,<< "binary" >>,1}, {atom,<< "binary" >>,1}, {atom,<< "binary" >>,1}, {atom,<< "binary" >>,1}, {atom,<< "binary" >>,1}, {atom,<< "binary" >>,1}, {atom,<< "binary" >>,1}, {atom,<< "binary" >>,1}, {atom,<< "binary" >>,1}, {atom,<< "bina" ...>>,1}, {atom,<<...>>,...}, {atom,...}, {...}|...] 28> erts_debug:size(MyList). 210 29> erts_debug:flat_size(MyList). 1200 30> |
如果仅仅是将一系列的模块打包在一起,并不需要启动application,那么只需要在app文件中移除掉{mod,{Module,Args}}配置节即可.这种Libiary Application典型范例就是stdlib.
看配置文件:
erlang:now常用作随机数的种子,这个并不是太好,建议使用:
4> <<A:32,B:32,C:32>> = crypto:strong_rand_bytes(12) . <<42,136,117,238,28,89,154,241,88,189,70,139>> 5> b(). A = 713586158 B = 475634417 C = 1488799371 ok
启动脚本可以使用escript来写
1
2
3
4
5
|
#!/usr/bin/env escript %% -*- erlang -*- %%! -pa ‘ebin/‘ [Other erl Arguments] main([StringArguments]) -> .... |
erlang启动过程可以通过-init_debug参数查看,erl启动不成功的时候就方便了
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
|
[root@nimbus ~]# erl -init_debug {progress,preloaded} {progress,kernel_load_completed} {progress,modules_loaded} {start,heart} {start,error_logger} {start,application_controller} {progress,init_kernel_started} {apply,{application,load,[{application,stdlib,[{description, "ERTS CXC 138 10" },{vsn, "2.0" },{id,[]},{modules,[array,base64,beam_lib,binary,c,calendar,dets,dets_server,dets_sup,dets_utils,dets_v8,dets_v9,dict,digraph,digraph_utils,edlin,edlin_expand,epp,eval_bits,erl_bits,erl_compile,erl_eval,erl_expand_records,erl_internal,erl_lint,erl_parse,erl_posix_msg,erl_pp,erl_scan,erl_tar,error_logger_file_h,error_logger_tty_h,escript,ets,file_sorter,filelib,filename,gb_trees,gb_sets,gen,gen_event,gen_fsm,gen_server,io,io_lib,io_lib_format,io_lib_fread,io_lib_pretty,lib,lists,log_mf_h,maps,math,ms_transform,orddict,ordsets,otp_internal,pg,pool,proc_lib,proplists,qlc,qlc_pt,queue,random,re,sets,shell,shell_default,slave,sofs, string ,supervisor,supervisor_bridge,sys,timer,unicode,win32reg,zip]},{registered,[timer_server,rsh_starter,take_over_monitor,pool_master,dets]},{applications,[kernel]},{included_applications,[]},{env,[]},{maxT,infinity},{maxP,infinity}]}]}} {progress,applications_loaded} {apply,{application,start_boot,[kernel,permanent]}} Erlang/OTP 17 [erts-6.0] [source] [64-bit] [smp:2:2] [async-threads:10] [hipe] [kernel-poll: false ] {apply,{application,start_boot,[stdlib,permanent]}} {apply,{c,erlangrc,[]}} {progress,started} Eshell V6.0 (abort with ^G) 1> |
erlydtl真的让Erlang Web开发有了幸福感
并发&并行 concurrency and parallelism.
In many places both words refer to the same concept. They are often used as two different ideas in the context of Erlang. For many Erlangers, concurrency refers to the idea of having many actors running independently, but not necessarily all at the same time. Parallelism is having actors running exactly at the same time. I will say that there doesn‘t seem to be any consensus on such definitions around various areas of computer science, but I will use them in this manner in this text. Don‘t be surprised if other sources or people use the same terms to mean different things.
This is to say Erlang had concurrency from the beginning, even when everything was done on a single core processor in the ‘80s. Each Erlang process would have its own slice of time to run, much like desktop applications did before multi-core systems.
Parallelism was still possible back then; all you needed to do was to have a second computer running the code and communicating with the first one. Even then, only two actors could be run in parallel in this setup. Nowadays, multi-core systems allows for parallelism on a single computer (with some industrial chips having many dozens of cores) and Erlang takes full advantage of this possibility.
The distinction between concurrency and parallelism is important to make, because many programmers hold the belief that Erlang was ready for multi-core computers years before it actually was. Erlang was only adapted to true symmetric multiprocessing in the mid 2000s and only got most of the implementation right with the R13B release of the language in 2009. Before that, SMP often had to be disabled to avoid performance losses. To get parallelism on a multicore computer without SMP, you‘d start many instances of the VM instead.
An interesting fact is that because Erlang concurrency is all about isolated processes, it took no conceptual change at the language level to bring true parallelism to the language. All the changes were transparently done in the VM, away from the eyes of the programmers.
Through the years, there were some attempts to build type systems on top of Erlang. One such attempt happened back in 1997, conducted by Simon Marlow, one of the lead developers of the Glasgow Haskell Compiler, and Philip Wadler, who worked on Haskell‘s design and has contributed to the theory behind monads (Read the paper on said type system). Joe Armstrong latercommented on the paper:
One day Phil phoned me up and announced that a) Erlang needed a type system, b) he had written a small prototype of a type system and c) he had a one year’s sabbatical and was going to write a type system for Erlang and “were we interested?” Answer —“Yes.”
Phil Wadler and Simon Marlow worked on a type system for over a year and the results were published in [20]. The results of the project were somewhat disappointing. To start with, only a subset of the language was type-checkable, the major omission being the lack of process types and of type checking inter-process messages.
二进制列表解析
%%The only change in syntax from regular list comprehensions is the <- which became <= and using binaries (<<>>) instead of lists ([]).
1> Pixels = <<213,45,132,64,76,32,76,0,0,234,32,15>>.
<<213,45,132,64,76,32,76,0,0,234,32,15>>
2> RGB = [ {R,G,B} || <<R:8,G:8,B:8>> <= Pixels ].
[{213,45,132},{64,76,32},{76,0,0},{234,32,15}]
3> << <<R:8, G:8, B:8>> || {R,G,B} <- RGB >>.
<<213,45,132,64,76,32,76,0,0,234,32,15>>
4> << <<R:8, G:8, B:8>> || {R,G,B} <- RGB >>.
<<213,45,132,64,76,32,76,0,0,234,32,15>>
5> << <<Bin>> || Bin <- [<<3,7,5,4,7>>] >>.
** exception error: bad argument
6> << <<Bin/binary>> || Bin <- [<<3,7,5,4,7>>] >>.
<<3,7,5,4,7>>
7> << <<(X+1)/integer>> || <<X>> <= <<3,7,5,4,7>> >>.
<<4,8,6,5,8>>
Note: At the time of this writing, binary comprehensions were seldom used and not documented very well. As such, it was decided not to dig more than what is necessary to identify them and understand their basic working. To understand more bit syntax as a whole, read the white paper defining their specification.
To be continued...
标签:des blog http io ar os 使用 sp for
原文地址:http://www.cnblogs.com/fvsfvs123/p/4142481.html