Erlang Trick O’ The Day
Hypothetical Labs - kevin - June 02, 2009I’ve been researching Erlang <-> Javascript integration for a client project and decided to take a look at how CouchDB integrates with Spidermonkey. Along the way I found the couch_os_process:init/1 function:
init([Command, Options, PortOptions]) ->
case code:priv_dir(couch) of
{error, bad_name} ->
% small hack, in dev mode "app" is couchdb. Fixing requires renaming
% src/couch to src/couch. Not really worth the hassle.-Damien
PrivDir = code:priv_dir(couchdb);
PrivDir -> ok
end,
Spawnkiller = filename:join(PrivDir, "couchspawnkillable"),
BaseProc = #os_proc{
command=Command,
port=open_port({spawn, Spawnkiller ++ " " ++ Command}, PortOptions),
writer=fun writejson/2,
reader=fun readjson/1
},
KillCmd = readline(BaseProc),
Pid = self(),
spawn(fun() ->
% this ensure the real os process is killed when this process dies.
erlang:monitor(process, Pid),
receive _ -> ok end,
os:cmd(?b2l(KillCmd))
end),
OsProc =
lists:foldl(fun(Opt, Proc) ->
case Opt of
{writer, Writer} when is_function(Writer) ->
Proc#os_proc{writer=Writer};
{reader, Reader} when is_function(Reader) ->
Proc#os_proc{reader=Reader};
{timeout, TimeOut} when is_integer(TimeOut) ->
Proc#os_proc{timeout=TimeOut}
end
end, BaseProc, Options),
{ok, OsProc}.
The neat bit is the call to list:foldl/3 at the end of the function. It folds over a list of configuration options and pops them into an Erlang record. I would’ve never thought to use a fold this way. Neat!
Categories: Blogs Hypothetical Labs
Comments
Ideally 642-642 you will have familiarity with the complete application development 646-205 life cycle (including testing), experience in GUI, design and effects, iOS database, 650-177 SQL, MySQL expertise, local data storage experience, good understanding of platform C-TADM51-70 performance/optimisation techniques and an understanding of semantic data.
Posted by roter on 28 Nov 2011 at 13:51
Add comment
Erlang on Twitter
» erlang (Andreas Åkre Solberg): RT @SaraJChipps: Node.js is like taking a bubble bath in JavaScript.
» trabajosit (Empleos en IT): argentina Desarrolladores - iOS, Ruby, Erlang: Estamos buscando un líder de desarrollo que quiera hacer una gran… http://t.co/zhTLpGI1
» erlang (Andreas Åkre Solberg): jQuery Scroll Path Plugin http://t.co/DHZ0W36c via @JoelBesada
» martyns (martynas): fighting with load bursts. probably its time to deploy https://t.co/xJxKcRmQ. #erlang
» Louellaoqk (Louella Delaroca): Informationsmanagement in Hochschulen (German Edition): Die Informations- und Kommunikationstechnik (IuK) erlang… http://t.co/95UJGZS3
» fgtrjhyu (アスパラガー): 多分「どうでもいい」だと思うんだ。 Smalltalk→Obj…C erlang→node.jsと同じで。
» zbyszek (Zbyszek Żółkiewski): RT @michalptaszek: Going to give #ejabberd tutorial on @erlangfactory in SF this March :) Anyone?
http://t.co/0bnFtIKf #xmpp #erlang
» jeedee (jeedee): Erlang, y u so fast?
» michalptaszek (Michal Ptaszek): Going to give #ejabberd tutorial on @erlangfactory in SF this March :) Anyone?
http://t.co/0bnFtIKf #xmpp #erlang
» FrancescoC (Francesco Cesarini): Woot! RT @valdo404: Practical Erlang Programming at #QConLondon I want to go there
Statistics
Number of aggregated posts: 10456
Number of comments: 1445
Most recent article: February 06, 2012
Latest comments
» simple smile on Scale means Skills: Very informative article. Pretty sure people would love to go to that place for shopping. Specially to those who are…
» simplesmile on 27 January 2012: Erlang Solutions embarks on an Erlang Embedded KTP: Your article will make the world better. Thanks again and good luck to you in your life. See you next time.simplesmile
» tandblekning easewhite on 08 February 2012: Erlang Express 3-day Course in San Francisco on 8 February: ncomprehensible to me now, but in general, the usefulness and significance is overwhelmingtandblekning easewhite