您的位置:首页 > Web前端

[Erlang 0040] Hidden Features of Erlang

2012-08-30 10:28 155 查看
Hidden Features of C#这个帖子大家不陌生吧,有人也在stackoverflow上提出了类似的问题:Hidden Features of Erlang,不过运气不佳的是,这个帖子并没有火爆起来而且现在已经关帖了;已经回复的内容其中大部分内容我在之前的博文里面已经覆盖到了,下面逐楼梳理一遍,大家查缺补漏吧;还有一些能够开出来是基础知识,之所以被认为是Hidden Feature(隐藏特性),其实是个人知识盲区.  Erlang Shell函数注释:之前在[Erlang 0031] Erlang Shell中的输出完整数据 提到过使用rp()输出完整数据结果;还是仔细阅读一下完整的手册吧,地址: http://www.erlang.org/doc/man/shell.html   Erlang Inheritance 之前刚刚提到过:[Erlang 0039] Erlang Inheritance   Parameterized Erlang这个也是刚刚提到过[Erlang 0037] Erlang Parameterized Module   user_default.erl you can build your own shell builtins by having a compiled user_default.beam in your path which can be pretty nifty注释:这个我们在[Erlang 0027] Using Record in Erlang Shell  里面用过 Erlang iolistPorts, external or linked-in, accept something called io-lists for sending data to them. An io-list is a binary or a (possibly deep) list of binaries or integers in the range 0..255.This means that rather than concatenating two lists before sending them to a port, one can just send them as two items in a list. So instead of"foo" ++ "bar"one do["foo", "bar"]In this example it is of course of miniscule difference. But the iolist in itself allows for convenient programming when creating output data. io_lib:format/2,3 itself returns an io list for example.The function erlang:list_to_binary/1 accepts io lists, but now we have erlang:iolist_to_binary/1 which convey the intention better. There is also an erlang:iolist_size/1.Best of all, since files and sockets are implemented as ports, you can send iolists to them. No need to flatten or append.注释:这个我们专门分析过iolist [Erlang 0034] Erlang iolist beam_lib:chunkscan get source code from a beam that was compiled with debug on which can be really usefull
{ok,{_,[{abstract_code,{_,AC}}]}} = beam_lib:chunks(Beam,[abstract_code]). io:fwrite("~s~n", [erl_prettypr:format(erl_syntax:form_list(AC))]).
注释:[Erlang 0011] Erlang 杂记Ⅱ 编号14 That match specificationscan be built using ets:fun2ms(...) where the Erlang fun syntax is used and translated into a match specification with a parse transform.
1> ets:fun2ms(fun({Foo, _, Bar}) when Foo > 0 -> {Foo, Bar} end).
[{{'$1','_','$2'},[{'>','$1',0}],[{{'$1','$2'}}]}]
So no fun-value is ever built, the expression gets replaced with the match-spec at compile-time. The fun may only do things a match expression could do.Also, ets:fun2ms is available for usage in the shell, so fun-expressions can be tested easily.注释: [Erlang 0008] Erlang的Match specifications 
.erlang_hostsgives a nice way to share names across machines {packet, Type}  The gen_tcp and ssl sockets have a {packet, Type} socket option to aid in decoding a number of protocols. The function erlang:decode_packet/3 has a good description on what the various Type values can be and what they do.Together with a {active, once} or {active, true} setting, each framed value will be delivered as a single message.Examples: the packet http mode is used heavily for iserve and the packet fcgi mode for ifastcgi. I can imagine that many of the other http servers use packet http as well.注释:这个绝对不是Hidden Feature,: ).erlangcan preload libraries and run commands on a shells startup, you can also do specific commands for specific nodes by doing a case statement on node name.注释:[Erlang 0027] Using Record in Erlang Shell   QLCIt is possible to define your own iterator for QLC to use. For example, a result set from an SQL query could be made into a QLC table, and thus benefit from the features of QLC queries.Besides mnesia tables, dets and ets have the table/1,2 functions to return such a "Query Handle" for them. Code BlockIf you want to execute more than one expression in a list comprehension, you can use a block. For example:
> [begin erlang:display(N), N*10 end || N <- lists:seq(1,3)].123[10,20,30]
 hide an Erlang nodeYou can hide an Erlang node by starting it with:erl -sname foo -hiddenYou can still connect to the node, but it won't appear in the list returned by nodes/0. Hot code loading.  Code is loaded and managed as "module" units, the module is a compilation unit. The system can keep two versions of a module in memory at the same time, and processes can concurrently run code from each.The versions are referred to the "new" and the "old" version. A process will not move into the new version until it makes an external call to its module.注释:[Erlang 0010] Erlang 热更新   最后一条"pajamas:" ++ Color = "pajamas:blue" 这个就真的不算Hidden了吧......... 
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息