When I learned Erlang, I recorded some items in the white space of the book, and some of them were recorded in the demo notes. Today I 've sorted out some of them and shared them.
Last address: [Erlang 0009] Erlang note
- Erlang is a message-oriented language. The message-oriented language does not share objects. inter-process interaction is completed by sending and receiving messages.
- An assigned variable in Erlang is a pointer pointing to the storage area of the stored value.
- A single value assignment means that the value of the bucket will not be modified.
- If Erlang does not share memory, there will be no locks.
- The order in which function clauses are matched is the order in the file.
- The data generator part of list parsing can also achieve data filtering through pattern matching [x | {A, x} <-[{A, 1}, {B, 12}, {C, 33}, {A, 23}].
- The reverse/1 of the lists module is highly optimized and can be used to rearrange the order of lists elements.
Note:Instead of writing your ownreverse/1
Function, you shocould uselists:reverse/1
. It's been used so much for tail recursive callthat the maintainers and developers of Erlang decided to turn it into a BIF. your lists can now benefit from extremely fast reversal (thanks to functions written in C) which will make the reversal disadvantage a lot less obvious.
- All BIF is in the Erlang module, so don't be surprised that the functions contained in this module are so complicated.
- The binary data composed of character sequences is equivalent to the binary data consisting of each ASCII Code <"King">
==< <$ K, $ I, $ N, $ G>
- The integers used in binary data must be between 0 and 255, because each byte in binary data is 8 bits, 2 #11111111
<A >>=< <255>. <B> = <256>. <C >>=< <257>. the values of a B C are respectively 255 0 1
- Bit syntax end-sign-type-Unit: a data example:
8/unsigned-little-integer
- Apply (M, F, A) is 6 ~ slower than calling the corresponding method M: F (A1, a2 ~ 10 times, try to avoid using
- Check that metadata of a module can be used
Modlue: module_info (). During module compilation, module_info is automatically created.
- Beam_lib: The chunk/2 method can analyze beam metadata without loading code.
- Begin end
Block expressions remind me of the VB syntax, which is rarely used. Where multiple expressions need to be written, they are basically extracted as independent methods.
- F1 = fun (x) when x = 0-> X; (x)
-> X + 100 end. % note that guard can be used here.
- Step_two (n, total) WHEN n =/= 0
-> Step_two (N-1, total + n). % note that guard is also used
- EPP Erlang pre-processor I mentioned before when talking about macros and record
Link: http://www.cnblogs.com/me-sa/archive/2011/07/20/erlang0006.html
- The process dictionary is suitable for storing global read-only configurations.
- Erlang: make_ref
Create a unique identifier, which is commonly used in gen_tcp
- Q () is the alias of init: Stop () Erlang: Halt ()
Both methods can disable the Erlang system.
- The ERL parameter-s can be executed multiple times in sequence.
-The noshell parameter allows the program to run in the background.
- Code: clash () method in code
Find the module with duplicate names in Path
- Press Ctrl + G in shell to enter "shell jcl" job
Control language is commonly used by j c r commands.
- Execute help () in Shell ()
Many practical auxiliary methods
- The execution of the spawn (fun) method will not fail, and a PID will always be returned
- Erlang: system_info/1 can obtain the system information at runtime, such as the number of process atoms.
Memory consumption, etc.
- The 0-Timeout Statement of receive can help us quickly clear the mail queue
- The process that captures the Exit message is called the system process
Process), learning new things is often based on the threshold brought by terms.
- Used exit (PID, kill) to clean up botnets when handling problems online.
- -Sname can be used only when no DNS service is required
- To enable the Erlang node to communicate with different hosts, You need to enable the TCP and UDP4369Port, EMPD (Erlang
Port mapper daemon) will use this port
- To use distributed Erlang, you need to open some ports or specify the port range, and ensure that the firewall has been opened.
Use-kernel inet_dist_listen_min for startup parameters
- File: Consult can directly read the Erlang items in the file.
File: unconsult can write Erlang items to files.
- IO: Format ("123 ~ W ~ N ", [" ABC "]). Output: 123 [97,98, 99] ~ W write data with standard syntax
- IO: Format ("123 ~ P ~ N ", [" ABC "]). Output: 123" ABC "~ P complete print Parameters
- Group_leader () is used to manage the output. For example, the RPC call intercepts the result output from the target machine and outputs it to the current shell.
- A compacting Garbage Collector for Erlang (1997) http://citeseerx.ist.psu.edu/viewdoc/summary? Doi = 10.1.1.34.2421
- % Unified Call interface capture exception execution M f a init_p_do_apply (M, F, A)-> tryapply (M, F, a) catchclass: reason-> exit_p (class, reason) end.
- % Search for process PID by Registration Name
Name_to_pid (name)->
Case whereis (name)
Undefined->
Case Global: safe_whereis_name (name)
Undefined->
Exit (could_not_find_registerd_name );
PID->
PID
End;
PID->
PID
End.
- Erlang: get_stacktrace ().Normally, the execution stack trace isn't stored in the part of the exception that you can see; it's stored internally. you can inspect the stack trace of the latest thrown exception of the current process by calling the built-in function Erlang: get_stacktrace ().
- % Check whether the process is alive is_process_alive (PID) When is_pid (PID)-> RPC: Call (node (PID), Erlang, is_process_alive, [pid]).
- Erlang: system_info (min_heap_size ).
{Min_heap_size, 233}
Erlang: system_info (min_bin_vheap_size ).
{Min_bin_vheap_size, 46368}
Erlang: system_info (fullsweep_after ).
{Fullsweep_after, 65535}
- The Erl-detached parameter is used to remove the dependency on the terminal.
- Erang performance tuning from ejabberd project http://www.ejabberd.im/tuning
Note:Tail recursion as seen here is not making the memory grow because when the virtual machine sees a function calling itself in a tail position (the last expression to be evaluated in a function ), it eliminates the current stack frame. this is called tail-call optimisation (TCO) And it is a special case of a more general optimisation namedLast call Optimisation(LCO ).
Lco is done whenever the last expression to be evaluated in a function body is another function call. when that happens, as with TCO, the Erlang VM avoids storing the stack frame. as such tail recursion is also possible between multiple functions. as an example, the chain of functionsa() -> b(). b() -> c(). c() -> a().
Will wait tively create an infinite loop that won't go out of memory as LCO avoids overflowing the stack. This principle, combined with our use of accumulators is what makes tail recursion useful.
Note:The correct ordering of each element in a comparison is the following:
number < atom < reference < fun < port < pid < tuple < list < bit string
You don't know all these types of things yet, but you will get to know them through the book. Just remember that this is why you can compare anything with anything! To quote Joe Armstrong, one of the creators of Erlang: "the actual order is not important-but that a total ordering is well defined is important ."