erlang

Erlang Recursive end loop

人盡茶涼 提交于 2020-01-05 21:09:25
问题 I just started learning Erlang and since I found out there is no for loop I tried recreating one with recursion: display(Rooms, In) -> Room = array:get(In, Rooms) io:format("~w", [Room]), if In < 59 -> display(Rooms, In + 1); true -> true end. With this code i need to display the content (false or true) of each array in Rooms till the number 59 is reached. However this creates a weird code which displays all of Rooms contents about 60 times (?). When I drop the if statement and only put in

Remove a substring/ string pattern of a string in Erlang

笑着哭i 提交于 2020-01-05 10:02:29
问题 I have an xml string like S = "<B xmns=\"som2\"> <a other='v1' more='v2'/><b some=\"v3/></B>". I want to remove the end tag </B> S2 = "<B xmns=\"som2\"> <a other='v1' more='v2'/><b some=\"v3/>" How can I achieve this? 回答1: If you only want to remove the specific string literal </B> then getting a sublist will do the trick: S = "<B xmns=\"som2\"> <a other='v1' more='v2'/><b some=\"v3\"/></B>", lists:sublist(S, 1, length(S) - 4). %%= "<B xmns=\"som2\"> <a other='v1' more='v2'/><b some=\"v3\"/>"

Split An Erlang List, X, into a List of All X's Sublists

大城市里の小女人 提交于 2020-01-05 05:37:27
问题 lists:sublist/2 and lists:sublist/3 make it simple to extract a single sublist from a list, but is there a BiF or module that returns a list of all of a list's sublists? i.e. lists:awesome_sublist_function([1,2,3,4]) -> [[1], [2], [3], [4], [1,2], [1,3], [1,4], [2,3], [2,4], [3,4], [1,2,3], [1,2,4], [1,3,4], [2,3,4], [1,2,3,4]] Can build my own, but wondered if the problem has been solved before anywhere? 回答1: I assume your test case is forgetting [1,3,4], but it could look something like

Data persistence when worker process dies, how?

本秂侑毒 提交于 2020-01-04 13:48:44
问题 I have worker processes that needs gathered/calculated data as arguments on start up. This is then needed on re-starts as well. Where should I put the initialization code? Inside the supervisors init? Or inside the modules start_link, or init? Are there any best practices in Erlang when it comes to this? 回答1: If the gen_server component has critical state, or state which cannot be re-calculated/re-gathered, I generally avoid keeping the state in gen_server itself. I instead choose to maintain

Data persistence when worker process dies, how?

久未见 提交于 2020-01-04 13:47:33
问题 I have worker processes that needs gathered/calculated data as arguments on start up. This is then needed on re-starts as well. Where should I put the initialization code? Inside the supervisors init? Or inside the modules start_link, or init? Are there any best practices in Erlang when it comes to this? 回答1: If the gen_server component has critical state, or state which cannot be re-calculated/re-gathered, I generally avoid keeping the state in gen_server itself. I instead choose to maintain

Encoding Erlang Maps as JSON with Strings for parsing by Javascript?

丶灬走出姿态 提交于 2020-01-04 09:10:24
问题 I'm trying to take an Erlang map like #{"breakfast" => "leftovers"} and encode as a JSON map. I tried converting a list with jiffy for example (tunnel@127.0.0.1)27> binary_to_list(jiffy:encode(["alpha", "beta"] )). "[[97,108,112,104,97],[98,101,116,97]]" but I am unsure how to convert that to a JSON object. When I try to convert a map I get " invalid_member_key " (tunnel@127.0.0.1)28> jiffy:encode(#{"breakfast" => "egg sandwhich"}). ** exception throw: {error,{invalid_object_member_key,

Erlang OTP编程初体验——gen_server和行为模式

喜夏-厌秋 提交于 2020-01-04 06:46:33
http://blog.sina.com.cn/s/blog_3fe961ae0101k4p6.html 行为模式其实非常类似于面向对象语言中的接口,至少笔者是这么理解的。OTP行为模式将一些反复出现的模式分成了两个部分,通用部分和具体应用相关的实现部分,这一过程其实就类似于面向对象编程中的抽象出接口的过程。本文给出一个OTP中最常见的行为模式的示例:通用服务器,即gen_server。 编写gen_server回调模块大致包括3相步骤: (1) 确定回调模块的名称; (2) 写接口函数(由客户端调用的); (3) 在回调模块中实现gen_server的6个回调函数(由gen_server容器来调用的)。 下面给出一个《Erlang OTP并发编程实战》中的示例代码,代码通过实现gen_server的接口,进而实现一个简单的RPC服务,这个RPC服务可以允许客户端调用服务器端任意模块中导出的任意函数。并且提供了一个get_count的查询接口,用于查询当前服务器已经处理过的请求数量。另外还有start_link()和stop()用于停止服务器进程。 tr_server.erl %%%------------------------------------------------------------------- %%% @author Martin & Eric %%%

erlang(2) gen_server

☆樱花仙子☆ 提交于 2020-01-04 06:45:06
通过前面的 erlang(1) 我们了解到了一个基本的服务器的实现方法。 erlang中为我们提供了这样一个服务器的现成的实现,就是gen_server,不用自己去编写服务器程序了。 所要做的就是以下几个事: 1.确定我们回调模块的名称 2.编写接口函数 3.实现六个回调方法 也就是说我们只需要关注于回调模块的实现就OK了。 1: -module(XXXX) 定义模块名称 2:接口函数,可供调用的函数。 在这里你可以定义所有需要的方法,作为功能调用的入口。 例如常见的start()、stop()等。他们的作用就是调用gen_server,完成指定功能。 3:实现回调函数,一共需要实现六个。 init()/1 ----当我们调用gen_server:start_link 时,服务器会调用此方法 handle_call/3 ----当我们调用gen_server:call 时服务器会调用此方法 handle_cast/2 ----当我们调用gen_server:cast 时服务器会调用此方法 handle_info/2 ----处理外界发送给服务器的原生消息,例如一个通过PID ! message 发送过来的 消息 terminate/2 ----终止服务器时会调用的方法 code_change/2 ----代码热替换,软件升级代码替换时调用的函数 来源: https://www

global:whereis_name() returns different Pid from different terminals

假如想象 提交于 2020-01-04 06:31:46
问题 Could somebody please explain to me why the Pid returned from global:whereis_name() is different when done in different terminals (under OSX, at least). A simple demonstration is below. demo.erl -module(demo). -export([start/0, loop/0, echo/1]). start() -> Pid = spawn(?MODULE, loop, []), yes = global:register_name('demo', Pid). echo(Msg) -> global:send('demo', Msg). loop() -> receive Msg -> io:format("demo: ~w~n", [Msg]), loop() end. Terminal A: erl -sname A -setcookie demo (A@local)1> demo

Does supervisor block calls while restarting children?

百般思念 提交于 2020-01-04 05:40:40
问题 I'm trying to understand what's happening here: I have a supervisor that is cyclically restarting one client without triggering the MaxR, MaxT mechanism. The client just crashes slowly enough never to trigger the rate limitation. There would have been another mechanism that uses supervisor:which_children/1 and delete_child/2, start_child/2 to adapt the set of children to reality (its scanning for USB devices trying to have one supervisor child per device found). This would normally behave