<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Avery Blog &#187; Erlang</title>
	<atom:link href="http://averyblog.com/category/erlang/feed/" rel="self" type="application/rss+xml" />
	<link>http://averyblog.com</link>
	<description>This is not the greatest tagline in the world... this is just a tribute.</description>
	<lastBuildDate>Tue, 08 Feb 2011 02:41:15 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.8.4</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>An Erlang Sequence Server</title>
		<link>http://averyblog.com/erlang/an-erlang-sequence-server/</link>
		<comments>http://averyblog.com/erlang/an-erlang-sequence-server/#comments</comments>
		<pubDate>Wed, 15 Apr 2009 13:52:00 +0000</pubDate>
		<dc:creator>javery</dc:creator>
				<category><![CDATA[Erlang]]></category>

		<guid isPermaLink="false">http://averyblog.infozerk.net/?p=774</guid>
		<description><![CDATA[**Thanks to Chandrashekhar who in the comments pointed out I could use mnesia:dirty_update_counter to do pretty much the same thing, I didn&#8217;t know about this function but it looks perfect**
For a project I am working on I need to have a sequence number assigned to each record in an mnesia table. This is fairly simple [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>**Thanks to Chandrashekhar who in the comments pointed out I could use mnesia:dirty_update_counter to do pretty much the same thing, I didn&#8217;t know about this function but it looks perfect**</p>
<p>For a project I am working on I need to have a sequence number assigned to each record in an mnesia table. This is fairly simple in most databases but mnesia doesn&#8217;t provide this option. The reason I need this number isn&#8217;t just to be used as a unique identifier, if that was all I need there are other techniques that would make more sense in erlang like using a combination of the node() id and a timestamp: {node(), now()}.</p>
<p>I need the sequential number so I can perform reporting on the table and keep track of the last record I processed, so I needed the number to be sequential and easy to query on. (I wanted to avoid date parsing)</p>
<p>So I decided to build a very simple little gen_server, and since it is very self-contained, I decided to release it out on github. Hopefully someone else will find it useful and if nothing else its a good example of working with mnesia and writing a gen_server. (at least I hope its a good example, please let me know if there is something I could improve)</p>
<p>Using the server is dead simple:</p>
<pre name="code" class="erlang">
Sequence = sequence_server:get_sequence(impression)
</pre>
<p>When you call get_sequence you pass it either a string or an atom and based on that value the server will return the next Id. If this is the first time you are passing in the atom or string then it will create a new record for that value and return 1.</p>
<p>Here is the meat of the server:</p>
<pre name="code" class="erlang">
F = fun() -&gt;
Return = mnesia:read(sequence, Id, write),
case Return of
[S] -&gt;
Seq = S#sequence.sequence,
New = S#sequence{sequence = Seq + 1},
mnesia:write(New),
Seq;
[] -&gt;
SequenceRecord = #sequence{id = Id, sequence = 2},
mnesia:write(SequenceRecord),
1
end
end,
{atomic, Seq} = mnesia:transaction(F),
</pre>
<p>The first line reads from the Mnesia table and gets a write lock. The first part of the case statement is if a record is found for this atom or string value in that case it increments the value, writes it back to the table, and returns it. The second part of the case statement is if there is no record, in which case it creates a new record and returns the number 1.</p>
<p>You can check out the full source code <a href="http://github.com/averyj/sequence_server/tree/master">here</a>.</p>
<p>-James</p>
]]></content:encoded>
			<wfw:commentRss>http://averyblog.com/erlang/an-erlang-sequence-server/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Erlang: List Comprehensions</title>
		<link>http://averyblog.com/erlang/erlang-list-comprehensions/</link>
		<comments>http://averyblog.com/erlang/erlang-list-comprehensions/#comments</comments>
		<pubDate>Sat, 21 Feb 2009 11:22:00 +0000</pubDate>
		<dc:creator>javery</dc:creator>
				<category><![CDATA[Erlang]]></category>

		<guid isPermaLink="false">http://averyblog.infozerk.net/?p=770</guid>
		<description><![CDATA[One of the most challenging things about learning a new language isn&#8217;t just learning how to do something, but more importantly learning the best way to do something. &#34;Best&#34; in this context is of course very subjective, but for me it not only means correctness but also readability, brevity, and following what is the normal [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>One of the most challenging things about learning a new language isn&#8217;t just learning how to do something, but more importantly learning the best way to do something. &quot;Best&quot; in this context is of course very subjective, but for me it not only means correctness but also readability, brevity, and following what is the normal convention for writing code in that language. Erlang has lots of ways to do things. I am working on a small solution where I am pulling messages back from Amazon SQS and processing them in Erlang. Here is a piece of code I initially wrote to handle looping through the list of messages and processing each of them:</p>
<pre class="erlang" name="code">
process_messages(H|T, SQS) -&gt;
process_message(H, SQS),
process_messages(T, SQS.
process_messages([], SQS) -&gt;
ok.
process_message(Message, SQS) -&gt;
%%process message here.
</pre>
<p>I was showing <a href="http://twitter.com/markimbriaco">Mark</a> this code and it struck him as strange, the process_messages function is just going through every item in the list and passing it to the process_message function, so a perfect case for map. So I rewrote the code using the lists:map function:</p>
<pre class="erlang" name="code">
process_messages(List, SQS) -&gt;
lists:map(fun(X) -&gt; process_message(X, SQS) end, List).
process_message(Message, SQS) -&gt;
%%process message here.
</pre>
<p>This is much nicer and reads better, I could even inline this code if I wanted to but I like the descriptiveness of having a function named process_messages. But, this can be even more descriptive using a list comprehension:</p>
<pre class="erlang" name="code">
process_messages(List, SQS) -&gt;
[process_message(X, SQS) || X &lt;- List].
process_message(Message, SQS) -&gt;
%%process message here.
</pre>
<p>An even simpler line of code and once you get comfortable reading list comprehensions it makes much more sense.</p>
<p>(found a nice Erlang Brush for Syntax Highlighter over <a href="http://stevegilham.blogspot.com/2008/12/syntax-highlight-brush-for-erlang.html">here</a>)</p>
]]></content:encoded>
			<wfw:commentRss>http://averyblog.com/erlang/erlang-list-comprehensions/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Learning Erlang</title>
		<link>http://averyblog.com/erlang/learning-erlang/</link>
		<comments>http://averyblog.com/erlang/learning-erlang/#comments</comments>
		<pubDate>Mon, 16 Feb 2009 18:42:00 +0000</pubDate>
		<dc:creator>javery</dc:creator>
				<category><![CDATA[Erlang]]></category>

		<guid isPermaLink="false">http://averyblog.infozerk.net/?p=769</guid>
		<description><![CDATA[Last year I saw Kevin Smith do a presentation at the local raleigh.rb group on this fascinating little language called Erlang. Erlang is a functional language that is dynamically typed. Its true power is that it makes concurrency, one of the most difficult programming problems, extremely easy. One statement that really says it all to [...]]]></description>
			<content:encoded><![CDATA[<p></p><p>Last year I saw <a href="http://weblog.hypotheticalabs.com/">Kevin Smith</a> do a presentation at the local <a href="http://www.meetup.com/raleighrb/">raleigh.rb</a> group on this fascinating little language called <a href="http://erlang.org/">Erlang</a>. Erlang is a functional language that is dynamically typed. Its true power is that it makes concurrency, one of the most difficult programming problems, extremely easy. One statement that really says it all to me is this:</p>
<p><strong>In Erlang spawning a new process is as easy and cheap as creating an object in an object oriented language.</strong></p>
<p>The presentation peaked my interest and I dabbled with Erlang when I had time last year. I went to a couple of the local <a href="http://www.meetup.com/erloungerdu/">Erlang hack nights</a>, I started reading the <a href="http://www.amazon.com/Programming-Erlang-Software-Concurrent-World/dp/193435600X/ref=pd_bbs_sr_1?ie=UTF8&amp;s=books&amp;qid=1234823899&amp;sr=8-1">Programming Erlang</a> book, and I checked out the <a href="http://www.pragprog.com/screencasts/v-kserl/erlang-in-practice">prag prog screencasts</a> (also by Kevin Smith).</p>
<p>Last week I went to a great 2-day training class on Erlang put on by Kevin down in Carborro at the <a href="http://carrborocoworking.com/">co-working facility</a> there. This class impressed me for a couple of reasons:</p>
<p>1) I feel like I finally understand Erlang and I can actually start a project using it.</p>
<p>2) I normally hate training classes, but this one was actually very effective. It put the focus on writing erlang code in labs, but the labs were just a problem and you had to figure out how to solve it. They weren&#8217;t the classic step by step labs that are just mindless instruction following, you really had to stretch your knowledge of the language and what you had learned to complete these labs. I enjoyed it so much that I am actually considering trying to put together a training course and becoming a trainer is one of the things I never wanted to do.</p>
<p>With all this knowledge I have started an interesting little project with Erlang. I am attempting to re-write the reporting section of Adzerk (the software that runs Ruby Row and The Lounge) using Erlang. I have been meaning to separate the reporting piece from the rest of the application (especially from a database perspective) for sometime and this gives me a great excuse. Erlang will make it easy for me to make my reporting system near time, advertisers and publishers will be able to see impressions and clicks seconds after they happen instead of the day after like how it currently works. (I know you could accomplish this in just about any technology, but Erlang makes it easier to do this and to scale in the ways I want to).</p>
<p>So if you are looking for a new language to pick up this year I would encourage you to try out Erlang. I plan on using it more and more and will be posting interesting tools or tricks I find to this blog.</p>
<p>-James</p>
]]></content:encoded>
			<wfw:commentRss>http://averyblog.com/erlang/learning-erlang/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

