<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:dc="http://purl.org/dc/elements/1.1/" version="2.0"><channel><atom:link rel="hub" href="http://tumblr.superfeedr.com/" xmlns:atom="http://www.w3.org/2005/Atom"/><description>This is where I blog about Erlang, hacks and tech.

Follow me on Twitter: @eproxus</description><title>alind.io</title><generator>Tumblr (3.0; @eproxus)</generator><link>http://alind.io/</link><item><title>All Erlang Modules in the Code Path</title><description>&lt;p&gt;Here&amp;#8217;s a little thing of beauty to get all Erlang modules in your code path, should you ever need it:&lt;/p&gt;

&lt;pre class="brush: erl"&gt;
&lt;code&gt;
[list_to_atom(filename:basename(F, ".beam"))
 || P &amp;lt;- code:get_path(), F &amp;lt;- filelib:wildcard("*.beam", P)].
&lt;/code&gt;
&lt;/pre&gt;</description><link>http://alind.io/post/5664209650</link><guid>http://alind.io/post/5664209650</guid><pubDate>Fri, 20 May 2011 12:52:00 +0200</pubDate></item><item><title>Erlang Alignment in Emacs</title><description>&lt;p&gt;Just realized that the default &lt;code&gt;align&lt;/code&gt; function in Emacs is quite capable. To indent, for example, a list of tuples with data, add the following alignment rule to your &lt;code&gt;.emacs&lt;/code&gt;-file:&lt;/p&gt;

&lt;pre class="brush: lisp"&gt;
(add-hook 'align-load-hook
          (lambda ()
            (add-to-list 'align-rules-list
                         '(erlang-align
                           (regexp . ",\\(\\s-+\\)")
                           (repeat . t)
                           (modes quote (erlang-mode))))))
&lt;/pre&gt;

&lt;p&gt;This will add a new alignment rule that aligns comma separated regions with whitespace. It will align all occurrences over all lines in the region, and only be enabled in the Erlang mode.&lt;/p&gt;

&lt;p&gt;The alignment will transform this:&lt;/p&gt;

&lt;pre class="brush: erl"&gt;
[{a, 19, "test", &amp;lt;&amp;lt;0,0&amp;gt;&amp;gt;},
 {abc, 2, "a", &amp;lt;&amp;lt;12,19,28,11&amp;gt;&amp;gt;}].
&lt;/pre&gt;

&lt;p&gt;Into this:&lt;/p&gt;

&lt;pre class="brush: erl"&gt;
[{a,   19, "test", &amp;lt;&amp;lt;0,0&amp;gt;&amp;gt;},
 {abc, 2,  "a",    &amp;lt;&amp;lt;12,19,28,11&amp;gt;&amp;gt;}].
&lt;/pre&gt;

&lt;p&gt;I set it to Ctrl+E in my &lt;code&gt;.emacs&lt;/code&gt;-file:&lt;/p&gt;

&lt;pre class="brush: lisp"&gt;
(global-set-key (read-kbd-macro "C-E") 'align)
&lt;/pre&gt;

&lt;p&gt;I can also recommend the &lt;code&gt;align-repeat&lt;/code&gt; macro from &lt;a href="http://www.emacswiki.org/emacs/AlignCommands"&gt;Align Commands on Emacs Wiki&lt;/a&gt;. I set it to Ctrl+Shift+E.&lt;/p&gt;</description><link>http://alind.io/post/2794563082</link><guid>http://alind.io/post/2794563082</guid><pubDate>Mon, 17 Jan 2011 14:59:00 +0100</pubDate><category>erlang</category><category>emacs</category></item><item><title>Synchronized, reliable message passing in Erlang</title><description>&lt;p&gt;To do proper, synchronized message passing you need three things: monitoring, timeout and a unique tag in the messages.&lt;/p&gt;

&lt;h3&gt;Basic Case&lt;/h3&gt;

&lt;p&gt;The basic case is sending a blocking request to a server process.&lt;/p&gt;

&lt;pre class="brush: erl"&gt;
request(Pid, Request) -&amp;gt;
    Pid ! {request, Request},
    receive
        {reply, Response} -&amp;gt;
            Response
    end.
&lt;/pre&gt;

&lt;p&gt;This gives us a nice way to send a request to a server and wait for a reply, but it can fail for several reasons:&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;The server process is not alive&lt;/li&gt;
&lt;li&gt;The server process crashes before it sends an answer&lt;/li&gt;
&lt;li&gt;The server process takes to long time to respond&lt;/li&gt;
&lt;li&gt;We receive an old reply to another request&lt;/li&gt;
&lt;/ul&gt;&lt;h3&gt;Monitoring&lt;/h3&gt;

&lt;p&gt;To check if the process is alive or if it crashes during the request is done by monitoring the process. It is also possible to use links, but that requires trapping exits and should not be done dynamically (a process should either trap exits or not when it is started, and keep that behavior for its lifetime).&lt;/p&gt;

&lt;p&gt;A monitor is set up by calling &lt;code&gt;erlang:monitor(process, Pid)&lt;/code&gt;. This will instantly set up a monitor for the process and as soon as the monitored process dies Erlang will send this message to the caller: &lt;code&gt;{'DOWN', MonitorRef, Type, Object, Info}&lt;/code&gt; where&lt;/p&gt;

&lt;ul&gt;&lt;li&gt;&lt;code&gt;MonitorRef&lt;/code&gt; is the return value from the call &lt;code&gt;erlang:monitor/2&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Type&lt;/code&gt; is the atom &lt;code&gt;process&lt;/code&gt;&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Object&lt;/code&gt; is the monitored pid&lt;/li&gt;
&lt;li&gt;&lt;code&gt;Info&lt;/code&gt; is the exit reason for the process or &lt;code&gt;noproc&lt;/code&gt; (if the process didn&amp;#8217;t exist) or &lt;code&gt;noconnection&lt;/code&gt; (for node connections).&lt;/li&gt;
&lt;/ul&gt;&lt;p&gt;The fine thing with &lt;code&gt;erlang:monitor/2&lt;/code&gt; is that it will always send a message to the caller. So if the process is already dead when setting up the monitor, it will still send a down message with the &lt;code&gt;Info&lt;/code&gt; set to &lt;code&gt;noproc&lt;/code&gt;. This allows us to write quite generic code:&lt;/p&gt;

&lt;pre class="brush: erl; highlight: [2,7,8]"&gt;
request(Pid, Request) -&amp;gt;
    MRef = erlang:monitor(process, Pid),
    Pid ! {request, Request},
    receive
        {reply, Response} -&amp;gt;
            Response;
        {'DOWN', MRef, process, Pid, Reason} -&amp;gt;
            {error, Reason}
    end.
&lt;/pre&gt;

&lt;h3&gt;Timeouts&lt;/h3&gt;

&lt;p&gt;Even if the server stays alive, it might take too long to respond. Either it is too busy, or there is a bug making it take to long to process the request or it froze. This can be fixed with a receive timeout:&lt;/p&gt;

&lt;pre class="brush: erl; highlight: [9,10]"&gt;
request(Pid, Request, Timeout) -&amp;gt;
    MRef = erlang:monitor(process, Pid),
    Pid ! {request, Request},
    receive
        {reply, Response} -&amp;gt;
            Response;
        {'DOWN', MRef, process, Pid, Reason} -&amp;gt;
            {error, Reason}
    after Timeout -&amp;gt;
            {error, timeout}
    end.
&lt;/pre&gt;

&lt;h3&gt;Unique Messages&lt;/h3&gt;

&lt;p&gt;The last feature is added to avoid mixing up responses to requests made from the same process. The timeout we added in the last part can actually result in the server replying after the client timed out and stopped caring about the response. This will result in the response ending up in the mail box of the client process, which could be accidentally picked up the next time we wait for a response. To avoid this we create a unique reference using the built in function &lt;code&gt;make_ref/0&lt;/code&gt;:&lt;/p&gt;

&lt;pre class="brush: erl; highlight: [3,4,6]"&gt;
request(Pid, Request, Timeout) -&amp;gt;
    MRef = erlang:monitor(process, Pid),
    Ref = make_ref(),
    Pid ! {request, Ref, Request},
    receive
        {reply, Ref, Response} -&amp;gt;
            Response;
        {'DOWN', MRef, process, Pid, Reason} -&amp;gt;
            {error, Reason}
    after Timeout -&amp;gt;
            {error, timeout}
    end.
&lt;/pre&gt;

&lt;p&gt;This assumes the server responds with the same reference that the request contained. Just remember to flush the mailbox for any old responses lying around, otherwise that will be a memory leak!&lt;/p&gt;</description><link>http://alind.io/post/1309182015</link><guid>http://alind.io/post/1309182015</guid><pubDate>Wed, 13 Oct 2010 13:02:41 +0200</pubDate><category>erlang</category></item><item><title>To avoid the verbose case statements in Erlang, I sometimes use this function:


ifc(true,  True, ...</title><description>&lt;p&gt;To avoid the verbose case statements in Erlang, I sometimes use this function:&lt;/p&gt;

&lt;pre class="brush: erl"&gt;
ifc(true,  True,  _) -&amp;gt; True;
ifc(false, _, False) -&amp;gt; False;
&lt;/pre&gt;

&lt;p&gt;So instead of writing this:&lt;/p&gt;

&lt;pre class="brush: erlang"&gt;
case proplists:is_defined(empty, Options) of
    true  -&amp;gt; [];
    false -&amp;gt; [some_default]
end
&lt;/pre&gt;

&lt;p&gt;one can write this:&lt;/p&gt;

&lt;pre class="brush: erlang"&gt;
ifc(proplists:is_defined(empty, Options), [], [some_default])
&lt;/pre&gt;

&lt;p&gt;The idea was to mimic the simplicity of C&amp;#8217;s (and other languages)&amp;#160;? operator.&lt;/p&gt;</description><link>http://alind.io/post/1216103866</link><guid>http://alind.io/post/1216103866</guid><pubDate>Thu, 30 Sep 2010 18:15:00 +0200</pubDate><category>erlang</category></item></channel></rss>
