erlang

erlang mnesia - illegal record info

不问归期 提交于 2019-12-22 07:05:01
问题 I am trying to have a function that ensures the table I need is already created and if not to create it. Here's the sample: ensure_table_exists(Table, MnesiaTables, Nodes) -> case lists:member(Table, MnesiaTables) of true -> throw({error, db_might_have_already_been_created}); false -> mnesia:create_table(Table, [{disc_copies, Nodes}, {attributes, record_info(fields, Table)}]), ok end. The issue is that when compiling I get the error: illegal record info . It might have to do that record_info

Showing full expected and value information when ?_assertEqual fails

╄→尐↘猪︶ㄣ 提交于 2019-12-22 06:45:06
问题 I'm coding a unit test where a (rather lengthy) binary is generated, and I want to assert that the generated binary equals the one I expect to be generated. I'm running eunit through " rebar eunit ". Thing is, when this assertion fails, the output is abreviated with " ... ", and I want to see the complete output so I can spot where the difference is. I'm now using " ?debugFmt() " as a temporary solution, but I'd like to know if there's an alternative to it (a config option or argument

Tracing the action of consuming messages from mailbox in Erlang

守給你的承諾、 提交于 2019-12-22 06:39:41
问题 I went through the documentation of the trace/3 BIF in Erlang. However, one observation I have made is that it cannot be used for tracing the consuming of messages from the mailbox. The flag 'receive' only traces when messages are added to a process's mailbox. Is there any way one can trace events such as reading from the mailbox using the receive construct? If not, is there any reason why this isn't possible? It seems very strange that one can trace most kind of events in a program and the

Tracing the action of consuming messages from mailbox in Erlang

不问归期 提交于 2019-12-22 06:39:08
问题 I went through the documentation of the trace/3 BIF in Erlang. However, one observation I have made is that it cannot be used for tracing the consuming of messages from the mailbox. The flag 'receive' only traces when messages are added to a process's mailbox. Is there any way one can trace events such as reading from the mailbox using the receive construct? If not, is there any reason why this isn't possible? It seems very strange that one can trace most kind of events in a program and the

Concatenating BitStrings (Not Binaries) in Erlang

笑着哭i 提交于 2019-12-22 06:36:23
问题 How do you concatenate bitstrings. I mean bitstrings because I do not know the number of bytes to be a multiple of 8. A = <<3:2>> B = <<1:1>> C = <<15:4>> Solution should A|B|C should be <<127:7>> Thanks 回答1: Construct the binary using /bitstring and all the previous values. Here's an example, running in the erlang shell: 1> A = <<3:2>>. <<3:2>> 2> B = <<1:1>>. <<1:1>> 3> C = <<15:4>>. <<15:4>> 4> D = <<A/bitstring, B/bitstring, C/bitstring>>. <<127:7>> 来源: https://stackoverflow.com/questions

ERLANG wait() and blocking

拈花ヽ惹草 提交于 2019-12-22 05:40:13
问题 Does the following function block on its running core? A great answer will detail the internal working of erlang and/or the cpu. wait(Sec) -> receive after (1000 * Sec) -> ok end. 回答1: The process which executes that code will block, the scheduler which runs that process currently will not block. The code you posted is equal to a yield, but with a timeout. The Erlang VM scheduler for that core will continue to execute other processes until that timeout fires and that process will be scheduled

What does “new” do when called on an erlang module do?

我们两清 提交于 2019-12-22 05:36:51
问题 I've seen module_name:new used in Erlang code, but there is no reference to a "new" function in the module_name module. What does "new" do? 回答1: It is for "parametrized module": see here and there for more details. 回答2: 'new'/2 = fun (_cor1,_cor0) -> apply 'instance'/2 (_cor1, _cor0) 'instance'/2 = fun (_cor1,_cor0) -> {'p',_cor1,_cor0} %%GOTCHA!!!!! http://www.cnblogs.com/me-sa/archive/2012/02/16/Erlang0037.html 来源: https://stackoverflow.com/questions/2283480/what-does-new-do-when-called-on

Flatten a list of nested lists in Erlang

北城以北 提交于 2019-12-22 05:33:39
问题 I'm working on the exercises in Erlang Programming . The question is Write a function that, given a list of nested lists, will return a flat list. Example: flatten([[1,[2,[3],[]]], [[[4]]], [5,6]]) ⇒ [1,2,3,4,5,6]. Hint: use concatenate to solve flatten . And here is my concatenate function %% concatenate([[1,2,3], [], [4, five]]) ⇒ [1,2,3,4,five]. concatenate([X|Xs]) -> concat(X, Xs, []). concat([X|Xs], T, L) -> concat(Xs, T, [X|L]); concat([], [X|Xs], L) -> concat(X, Xs, L); concat([], [],

Erlang: priority receive

喜你入骨 提交于 2019-12-22 05:12:10
问题 Priority receive in Erlang can easily be implemented as follows: prio() -> receive {priority, X} -> X after 0 -> receive X -> X end end. I am reading a paper called Priority Messaging made Easy in which they describe the following problem: The main problem with the [code] example [above], is that we do not take into consideration that when evaluation is resumed from the inner blocking receive we may have more than one message in the mailbox. In a worst case scenario, all but the first, of

Stop Erlang Daemon

爷,独闯天下 提交于 2019-12-22 05:09:14
问题 Besides running $ killall -9 beam.smp , how can I kill an Erlang node programmatically when I know its -sname ? If I don't want the heartbeat monitor to restart the process, how can I ensure that whatever answer is given for the above question will also kill the heartbeat? Is there a decent guide to deploying Erlang as a daemon? 回答1: kill and killall with -9 is almost always wrong . You can quite easily ask the remote node to exit using: rpc:call(RemoteNode, init, stop, []). I don't know