<?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>blog.teemu.im &#187; Erlang</title>
	<atom:link href="http://blog.teemu.im/tags/erlang/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.teemu.im</link>
	<description>i'm teemu and this is my weblog</description>
	<lastBuildDate>Mon, 31 Oct 2011 08:44:53 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
<atom:link rel="hub" href="http://pubsubhubbub.appspot.com"/><atom:link rel="hub" href="http://superfeedr.com/hubbub"/>		<item>
		<title>Playing Around With Openstack&#8217;s Object Storage</title>
		<link>http://blog.teemu.im/2010/10/22/playing-around-with-openstacks-object-storage/</link>
		<comments>http://blog.teemu.im/2010/10/22/playing-around-with-openstacks-object-storage/#comments</comments>
		<pubDate>Fri, 22 Oct 2010 01:51:56 +0000</pubDate>
		<dc:creator>Teemu Harju</dc:creator>
				<category><![CDATA[Erlang]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Openstack]]></category>
		<category><![CDATA[Swift]]></category>

		<guid isPermaLink="false">http://blog.teemu.im/?p=342</guid>
		<description><![CDATA[Couple of weeks ago I found out about the Openstack project and I found it immediately to be very interesting. What I&#8217;ve been playing around with the most is the object storage part of Openstack called Swift. I&#8217;ll show here how you can use Swift with a couple of different libraries. The nice thing about [...]]]></description>
			<content:encoded><![CDATA[<p>Couple of weeks ago I found out about the <a href="http://openstack.org">Openstack project</a> and I found it immediately to be very interesting. What I&#8217;ve been playing around with the most is the <a href="http://www.openstack.org/projects/storage/">object storage</a> part of Openstack called <a href="http://swift.openstack.org/">Swift</a>. I&#8217;ll show here how you can use Swift with a couple of different libraries. The nice thing about Swift is that it is basically the <a href="http://www.rackspacecloud.com/cloud_hosting_products/files">Rackspace Cloudfiles</a> storage, so the same libraries that work with Cloudfiles, should work with Swift as well. Well, they require some small modifications. But, I&#8217;ll show you here two libraries that I know are working already. Of course, you will need a Swift instance running somewhere and instructions on how to setup one you can read the <a href="http://swift.openstack.org/development_saio.html">&#8220;Swift All In One&#8221;</a> document that shows how you can run Swift on a single server.</p>
<p>The first library I&#8217;ll show here is the <a href="http://github.com/rackspace/python-cloudfiles">python-cloudfiles</a>. I recommend using the latest one from Github, since the one that you can get for example from Ubuntu repositories does not support Swift and the one you can get from <a href="http://pypi.python.org/pypi/python-cloudfiles/1.7.0">Python Package Index</a> had a bug that made it not work with Swift.</p>
<p>Here I&#8217;ll show you how you can connect to your local Swift instance using the <code>authurl</code> parameter and how you can create containers and objects using <code>python-cloudfiles</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">from</span> cloudfiles.<span style="color: black;">connection</span> <span style="color: #ff7700;font-weight:bold;">import</span> Connection
&nbsp;
conn = Connection<span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;test:test&quot;</span>, <span style="color: #483d8b;">&quot;test&quot;</span>, authurl=<span style="color: #483d8b;">&quot;http://127.0.0.1:11000/v1.0&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
container = conn.<span style="color: black;">create_container</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;test&quot;</span><span style="color: black;">&#41;</span>
&nbsp;
obj = container.<span style="color: black;">create_object</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;test.txt&quot;</span><span style="color: black;">&#41;</span>
obj.<span style="color: black;">content_type</span> = <span style="color: #483d8b;">&quot;text/plain&quot;</span>
obj.<span style="color: black;">write</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;test&quot;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>Pretty straightforward&#8230; right?</p>
<p>Next, I&#8217;ll show you another library that works with Swift called <a href="http://github.com/ddossot/cferl">cferl</a>. It&#8217;s a Erlang library for Cloudfiles and I made some <a href="http://github.com/ddossot/cferl/commit/38689bebcc0f229b3c75d113aed9532da745667d">simple patches</a> to it to make it work with Swift.</p>
<p>Here&#8217;s how you can do the same things as in previous example using <code>cferl</code>.</p>

<div class="wp_syntax"><div class="code"><pre class="erlang" style="font-family:monospace;">ibrowse:<span style="color: #ff3c00;">start</span><span style="color: #109ab8;">&#40;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span>
<span style="color: #109ab8;">&#123;</span>ok<span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Connection</span><span style="color: #109ab8;">&#125;</span> <span style="color: #014ea4;">=</span> cf<span style="color: #ff4e18;">erl</span>:<span style="color: #ff3c00;">connect</span><span style="color: #109ab8;">&#40;</span><span style="color: #ff7800;">&quot;test:test&quot;</span><span style="color: #6bb810;">,</span> <span style="color: #ff7800;">&quot;test&quot;</span><span style="color: #6bb810;">,</span> <span style="color: #ff7800;">&quot;http://127.0.0.1:11000/v1.0&quot;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span>
&nbsp;
<span style="color: #109ab8;">&#123;</span>ok<span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Container</span><span style="color: #109ab8;">&#125;</span> <span style="color: #014ea4;">=</span> <span style="color: #45b3e6;">Connection</span>:<span style="color: #ff3c00;">create_container</span><span style="color: #109ab8;">&#40;</span><span style="color: #ee3800;">&lt;&lt;</span><span style="color: #ff7800;">&quot;test&quot;</span><span style="color: #ee3800;">&gt;&gt;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span>
&nbsp;
<span style="color: #109ab8;">&#123;</span>ok<span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Object</span><span style="color: #109ab8;">&#125;</span> <span style="color: #014ea4;">=</span> <span style="color: #45b3e6;">Container</span>:<span style="color: #ff3c00;">create_object</span><span style="color: #109ab8;">&#40;</span><span style="color: #ee3800;">&lt;&lt;</span><span style="color: #ff7800;">&quot;test.txt&quot;</span><span style="color: #ee3800;">&gt;&gt;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span>
ok <span style="color: #014ea4;">=</span> <span style="color: #45b3e6;">Obje</span><span style="color: #ff4e18;">ct</span>:<span style="color: #ff3c00;">write_data</span><span style="color: #109ab8;">&#40;</span><span style="color: #ee3800;">&lt;&lt;</span><span style="color: #ff7800;">&quot;test&quot;</span><span style="color: #ee3800;">&gt;&gt;</span><span style="color: #6bb810;">,</span> <span style="color: #ee3800;">&lt;&lt;</span><span style="color: #ff7800;">&quot;text/plain&quot;</span><span style="color: #ee3800;">&gt;&gt;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span></pre></div></div>

<p>Ok, that&#8217;s it. Now you can start playing with Swift and storing petabytes of data in it.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.teemu.im/2010/10/22/playing-around-with-openstacks-object-storage/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Generating random strings in Erlang</title>
		<link>http://blog.teemu.im/2009/11/07/generating-random-strings-in-erlang/</link>
		<comments>http://blog.teemu.im/2009/11/07/generating-random-strings-in-erlang/#comments</comments>
		<pubDate>Sat, 07 Nov 2009 05:44:20 +0000</pubDate>
		<dc:creator>Teemu Harju</dc:creator>
				<category><![CDATA[Erlang]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://blog.teemu.im/?p=287</guid>
		<description><![CDATA[I could not find any decent examples from the web on how to generate a random string with a certain set of characters and length in Erlang. The basic idea for such a method is to take a string of allowed characters and loop N times where the N is the length of the resulting [...]]]></description>
			<content:encoded><![CDATA[<p>I could not find any decent examples from the web on how to generate a random string with a certain set of characters and length in Erlang. The basic idea for such a method is to take a string of allowed characters and loop N times where the N is the length of the resulting string. Then at each loop we take some random character from the string that contains the required set of characters. Sounds relatively simple, right? Next we have to write this in Erlang. This is what I came up with&#8230;</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
</pre></td><td class="code"><pre class="erlang" style="font-family:monospace;"><span style="color: #ff3c00;">get_random_string</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">Length</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">AllowedChars</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span>
    <span style="color: #ff4e18;">lists</span>:<span style="color: #ff3c00;">foldl</span><span style="color: #109ab8;">&#40;</span><span style="color: #186895;">fun</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">_</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Acc</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span>
                        <span style="color: #109ab8;">&#91;</span><span style="color: #ff4e18;">lists</span>:<span style="color: #ff3c00;">nth</span><span style="color: #109ab8;">&#40;</span><span style="color: #ff4e18;">random</span>:<span style="color: #ff3c00;">uniform</span><span style="color: #109ab8;">&#40;</span><span style="color: #fa6fff;">length</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">AllowedChars</span><span style="color: #109ab8;">&#41;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">,</span>
                                   <span style="color: #45b3e6;">AllowedChars</span><span style="color: #109ab8;">&#41;</span><span style="color: #109ab8;">&#93;</span>
                            <span style="color: #014ea4;">++</span> <span style="color: #45b3e6;">Acc</span>
                <span style="color: #186895;">end</span><span style="color: #6bb810;">,</span> <span style="color: #109ab8;">&#91;</span><span style="color: #109ab8;">&#93;</span><span style="color: #6bb810;">,</span> <span style="color: #ff4e18;">lists</span>:<span style="color: #ff3c00;">seq</span><span style="color: #109ab8;">&#40;</span><span style="color: #ff9600;">1</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Length</span><span style="color: #109ab8;">&#41;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span></pre></td></tr></table></div>

<p>Ok, Erlang is not the most readable language in the world and a simple thing such as generating a random string can look pretty tedious. No worries. I&#8217;ll go through the method line by line.</p>
<p>I&#8217;m using the <code>lists:foldl</code> method here. What it does is that it goes through a list (from left to right) and calls a function that has as it&#8217;s parameter a value from that list and the result form the previous iteration. The result of the method is the result of the last call to the function. The list I give as a parameter to <code>lists:foldl</code> is a sequence of numbers from one to the length of the resulting random string. For that I use the <code>lists:seq</code> method. This is how we define how many times we loop.</p>
<p>I&#8217;ll explain the <code>fun()</code> that is the first parameter of <code>lists:foldl</code>. Here is what it looks like separate from the whole code.</p>

<div class="wp_syntax"><div class="code"><pre class="erlang" style="font-family:monospace;"><span style="color: #186895;">fun</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">_</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">Acc</span><span style="color: #109ab8;">&#41;</span> <span style="color: #6bb810;">-&gt;</span>
     <span style="color: #109ab8;">&#91;</span><span style="color: #ff4e18;">lists</span>:<span style="color: #ff3c00;">nth</span><span style="color: #109ab8;">&#40;</span><span style="color: #ff4e18;">random</span>:<span style="color: #ff3c00;">uniform</span><span style="color: #109ab8;">&#40;</span><span style="color: #fa6fff;">length</span><span style="color: #109ab8;">&#40;</span><span style="color: #45b3e6;">AllowedChars</span><span style="color: #109ab8;">&#41;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">,</span> <span style="color: #45b3e6;">AllowedChars</span><span style="color: #109ab8;">&#41;</span><span style="color: #109ab8;">&#93;</span>
          <span style="color: #014ea4;">++</span> <span style="color: #45b3e6;">Acc</span>
<span style="color: #186895;">end</span></pre></div></div>

<p>The first parameter of the function is the value from the given list (<code>[1, 2, 3, 4,..., N]</code>) and we don&#8217;t use it (hence the underscore). The second parameter <code>Acc</code> is called the accumulator that is the result from the previous iteration. To achieve our goal of producing random strings we use <code>lists:nth</code> and <code>random:uniform</code> method calls to pick a random character from the <code>AllowedChars</code> string. Note that the <code>lists:nth</code> returns the integer value of that character so that is why the method call is wrapped in square brackets making the result a string (in Erlang strings are lists of integers). What we do then is that we add the<code> Acc</code> (the result of the previous iteration) to the result and this way build our random string.</p>
<p>There is also a third parameter for the <code>lists:foldl</code> method that you probably have guessed already. Naturally, you also have to give the value of the accumulator for the first iteration, which in this case is empty list <code>[]</code> or empty string since strings in Erlang are actually lists.</p>
<p>Here is an example of the result that the method produces.</p>

<div class="wp_syntax"><div class="code"><pre class="erlang" style="font-family:monospace;">test:<span style="color: #ff3c00;">get_random_string</span><span style="color: #109ab8;">&#40;</span><span style="color: #ff9600;">32</span><span style="color: #6bb810;">,</span> <span style="color: #ff7800;">&quot;qwertyQWERTY1234567890&quot;</span><span style="color: #109ab8;">&#41;</span><span style="color: #6bb810;">.</span>     
<span style="color: #ff7800;">&quot;8qttW01wQET1qRTt1r4tr2T392QY94Re&quot;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.teemu.im/2009/11/07/generating-random-strings-in-erlang/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>Automatic code reloading in Erlang</title>
		<link>http://blog.teemu.im/2009/11/03/automatic-code-reloading-in-erlang/</link>
		<comments>http://blog.teemu.im/2009/11/03/automatic-code-reloading-in-erlang/#comments</comments>
		<pubDate>Tue, 03 Nov 2009 13:24:48 +0000</pubDate>
		<dc:creator>Teemu Harju</dc:creator>
				<category><![CDATA[Erlang]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[PubSubHubbub]]></category>

		<guid isPermaLink="false">http://blog.teemu.im/?p=282</guid>
		<description><![CDATA[I&#8217;ve recently got back to coding Erlang and noticed a neat module that I didn&#8217;t know existed that is probably worth writing a blog entry about. I&#8217;ve started developing a PubSubHubbub hub in Erlang called Hubbabubba and I&#8217;m using the great Mochiweb HTTP library as the HTTP server implementation. I discovered the reloader.erl module that [...]]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve recently got back to coding <a href="http://www.erlang.org">Erlang</a> and noticed a neat module that I didn&#8217;t know existed that is probably worth writing a blog entry about. I&#8217;ve started developing a <a href="http://code.google.com/p/pubsubhubbub/">PubSubHubbub</a> hub in Erlang called <a href="http://github.com/tsharju/hubbabubba">Hubbabubba</a> and I&#8217;m using the great <a href="http://code.google.com/p/mochiweb/">Mochiweb HTTP library</a> as the HTTP server implementation. I discovered the <a href="http://code.google.com/p/mochiweb/source/browse/trunk/src/reloader.erl">reloader.erl module</a> that comes with Mochiweb. It automatically reloads the code when you have the application running and you modify the code (remember to compile as well). This is something that I&#8217;ve found very useful when developing with <a href="http://www.djangoproject.com/">Django</a> or <a href="http://code.google.com/appengine/docs/whatisgoogleappengine.html">AppEngine</a> and I&#8217;m really satisfied that there is a similar solution for Erlang as well.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.teemu.im/2009/11/03/automatic-code-reloading-in-erlang/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

