<?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>Ståle's weblog &#187; Programming</title>
	<atom:link href="http://blog.staale.org/category/programming/feed" rel="self" type="application/rss+xml" />
	<link>http://blog.staale.org</link>
	<description>Just another WordPress weblog</description>
	<lastBuildDate>Mon, 07 Sep 2009 13:35:35 +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>Another pointless challenge solved in Scala</title>
		<link>http://blog.staale.org/2009/07/another-pointless-challenge-solved-in-scala.html</link>
		<comments>http://blog.staale.org/2009/07/another-pointless-challenge-solved-in-scala.html#comments</comments>
		<pubDate>Mon, 27 Jul 2009 12:06:47 +0000</pubDate>
		<dc:creator>Staale</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Scala]]></category>
		<category><![CDATA[TheDailyWTF]]></category>

		<guid isPermaLink="false">http://blog.staale.org/?p=124</guid>
		<description><![CDATA[I am an avid reader of The Daily WTF, and recently there was a small challenge posted on the site. It deals with something called Russian multiplication. The explanation on the site is much better to understand than something I could reproduce myself, so I urge you to read the full text there. Basically, in [...]]]></description>
			<content:encoded><![CDATA[<p>I am an avid reader of <a title="The Daily WTF" href="http://thedailywtf.com">The Daily WTF</a>, and recently there was a small <a href="http://thedailywtf.com/Articles/Programming-Praxis-Russian-Peasant-Multiplication.aspx">challenge</a> posted on the site. It deals with something called Russian multiplication. The explanation on the site is much better to understand than something I could reproduce myself, so I urge you to read the full text there. Basically, in order to multiply 2 numbers, you set them in 2 columns. You then add a row with half and double of the first and second number in the previous row. Keep doing that until you have reached 1. Here is a simple table example for 47*131:</p>
<table border="0">
<tbody>
<tr>
<td>47</td>
<td>131</td>
</tr>
<tr>
<td>23</td>
<td>262</td>
</tr>
<tr>
<td>11</td>
<td>524</td>
</tr>
<tr>
<td>5</td>
<td>1048</td>
</tr>
<tr>
<td>2</td>
<td>2096</td>
</tr>
<tr>
<td>1</td>
<td>4192</td>
</tr>
</tbody>
</table>
<p>You then add all the numbers in the second column, when the number in the first column is odd, 131 + 262 + 524 + 1048 + 4192 = 6157.</p>
<p>Now to create a small Scala program that can solve this. First, I need a method that just produces the table above. This is rather simple to create:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">def</span> sumParts<span style="color: #F78811;">&#40;</span>a<span style="color: #000080;">:</span>Int, b<span style="color: #000080;">:</span>Int<span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> List<span style="color: #F78811;">&#91;</span><span style="color: #F78811;">&#40;</span>Int,Int<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span> 
    <span style="color: #0000ff; font-weight: bold;">if</span> <span style="color: #F78811;">&#40;</span>a<span style="color: #000080;">==</span><span style="color: #F78811;">0</span><span style="color: #F78811;">&#41;</span> List<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> <span style="color: #0000ff; font-weight: bold;">else</span> <span style="color: #F78811;">&#40;</span>a,b<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">::</span> sumParts<span style="color: #F78811;">&#40;</span>a/<span style="color: #F78811;">2</span>, b<span style="color: #000080;">*</span><span style="color: #F78811;">2</span><span style="color: #F78811;">&#41;</span></pre></td></tr></table></div>

<p>Notice the list type here is (Int, Int), which indicates it&#8217;s a list of tuples. If you have used scripting languages, you might be aware of tuples already. Tuples in Scala allows us to define a list like type with fixed size, and fixed type for each value. This is a tuple of 2 Ints, and the actual type off the method is a List of tuples, each with 2 Ints. If we send 47,131 as arguments to this method, we will get List((47,131), (23,262), (11,524), (5,1048), (2,2096), (1,4192)) in return. We can now iterate over this list to create a presentable output:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">val</span> numbers <span style="color: #000080;">=</span> sumParts<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">47</span>, <span style="color: #F78811;">131</span><span style="color: #F78811;">&#41;</span>
<span style="color: #0000ff; font-weight: bold;">for</span> <span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#40;</span>a,b<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">&lt;</span>- numbers<span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
    println<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;% 8d % 8d %5s&quot;</span> format<span style="color: #F78811;">&#40;</span>a, b, <span style="color: #0000ff; font-weight: bold;">if</span> <span style="color: #F78811;">&#40;</span>a<span style="color: #000080;">%</span>2<span style="color: #000080;">==</span><span style="color: #F78811;">1</span><span style="color: #F78811;">&#41;</span> <span style="color: #6666FF;">&quot;*&quot;</span> <span style="color: #0000ff; font-weight: bold;">else</span> <span style="color: #6666FF;">&quot;&quot;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
<span style="color: #F78811;">&#125;</span></pre></td></tr></table></div>

<p>Which produces this output:</p>
<pre>
      47      131     *
      23      262     *
      11      524     *
       5     1048     *
       2     2096
       1     4192     *
</pre>
<p>To format the output nicely, I use the format operator on the string. I have used the &#8220;%&#8221; operator to format stuff extensivly when printing in python, so I was happy to find the same functionality easily available in Scala. It would be nice if they also added a &#8220;%&#8221; overloaded method for RichString in Scala for this. I also use an if to print out a &#8220;*&#8221; next to each number that should be included in the addition. Next we need to filter out the numbers we actually need for the addition, and add them together.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">val</span> addNums <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">for</span> <span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#40;</span>a,b<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">&lt;</span>- numbers<span style="color: #000080;">;</span> <span style="color: #0000ff; font-weight: bold;">if</span> <span style="color: #F78811;">&#40;</span>a<span style="color: #000080;">%</span>2<span style="color: #000080;">==</span><span style="color: #F78811;">1</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span> <span style="color: #0000ff; font-weight: bold;">yield</span> b
<span style="color: #0000ff; font-weight: bold;">val</span> sum <span style="color: #000080;">=</span> <span style="color: #F78811;">&#40;</span><span style="color: #F78811;">0</span> /<span style="color: #000080;">:</span> addNums<span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#40;</span><span style="color: #000080;">_</span>+<span style="color: #000080;">_</span><span style="color: #F78811;">&#41;</span>
println<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;Numbers: %s, ansver: %d&quot;</span> format<span style="color: #F78811;">&#40;</span>addNums mkString<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot; + &quot;</span><span style="color: #F78811;">&#41;</span>, sum<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span></pre></td></tr></table></div>

<p>To get addNums, we use a for loop, and Scalas powerfull pattern matching. The for syntax is (item <- iterable). Instead of accessing each entry as a tuple, we use the (a,b) pattern to indicate the shape to Scala. The shape of the List ([(Int, Int)]) matches this, and each value in the tuple will be unpacked into a and b. When then filter on values where a is odd, and yield b. We will then have a list of only those numbers that had a corresponding odd number in our original table. Using mkString we can create a presentable String version off those numbers, and using the fold operator we add the numbers together.</p>
<p>It's also possible to create a smaller method for just calculating the sum from the list of parts returned by sumParts:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">def</span> calc<span style="color: #F78811;">&#40;</span>a<span style="color: #000080;">:</span> Int, b<span style="color: #000080;">:</span> Int<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#40;</span><span style="color: #F78811;">0</span> /<span style="color: #000080;">:</span> <span style="color: #F78811;">&#40;</span>sumParts<span style="color: #F78811;">&#40;</span>a,b<span style="color: #F78811;">&#41;</span> filter<span style="color: #F78811;">&#40;</span><span style="color: #000080;">_</span>.<span style="color: #000080;">_</span>1<span style="color: #000080;">%</span>2<span style="color: #000080;">==</span><span style="color: #F78811;">1</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#40;</span><span style="color: #000080;">_</span>+<span style="color: #000080;">_</span>.<span style="color: #000080;">_</span>2<span style="color: #F78811;">&#41;</span></pre></td></tr></table></div>

<p>Here I fold on the tuples, so I need to use _2 to access the 2nd value off the tuple. </p>
<p>Finally, here is the program in full, accepting 2 numbers on the command line. No error checking and verification is present in this version:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">object</span> RussianMultiplication <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">def</span> sumParts<span style="color: #F78811;">&#40;</span>a<span style="color: #000080;">:</span>Int, b<span style="color: #000080;">:</span>Int<span style="color: #F78811;">&#41;</span><span style="color: #000080;">:</span> List<span style="color: #F78811;">&#91;</span><span style="color: #F78811;">&#40;</span>Int,Int<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#93;</span> <span style="color: #000080;">=</span>
        <span style="color: #0000ff; font-weight: bold;">if</span> <span style="color: #F78811;">&#40;</span>a<span style="color: #000080;">==</span><span style="color: #F78811;">0</span><span style="color: #F78811;">&#41;</span> List<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> <span style="color: #0000ff; font-weight: bold;">else</span> <span style="color: #F78811;">&#40;</span>a,b<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">::</span> sumParts<span style="color: #F78811;">&#40;</span>a/<span style="color: #F78811;">2</span>, b<span style="color: #000080;">*</span><span style="color: #F78811;">2</span><span style="color: #F78811;">&#41;</span>
&nbsp;
   <span style="color: #0000ff; font-weight: bold;">def</span> main<span style="color: #F78811;">&#40;</span>args<span style="color: #000080;">:</span> Array<span style="color: #F78811;">&#91;</span>String<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
        <span style="color: #0000ff; font-weight: bold;">val</span> numbers <span style="color: #000080;">=</span> sumParts<span style="color: #F78811;">&#40;</span>args<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">0</span><span style="color: #F78811;">&#41;</span> toInt,args<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">1</span><span style="color: #F78811;">&#41;</span> toInt<span style="color: #F78811;">&#41;</span>
        <span style="color: #0000ff; font-weight: bold;">for</span> <span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#40;</span>a,b<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">&lt;</span>- numbers<span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
            println<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;% 8d % 8d %5s&quot;</span> format<span style="color: #F78811;">&#40;</span>a, b, <span style="color: #0000ff; font-weight: bold;">if</span> <span style="color: #F78811;">&#40;</span>a<span style="color: #000080;">%</span>2<span style="color: #000080;">==</span><span style="color: #F78811;">1</span><span style="color: #F78811;">&#41;</span> <span style="color: #6666FF;">&quot;*&quot;</span> <span style="color: #0000ff; font-weight: bold;">else</span> <span style="color: #6666FF;">&quot;&quot;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
        <span style="color: #F78811;">&#125;</span>
        <span style="color: #0000ff; font-weight: bold;">val</span> addNums <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">for</span> <span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#40;</span>a,b<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">&lt;</span>- numbers<span style="color: #000080;">;</span> <span style="color: #0000ff; font-weight: bold;">if</span> <span style="color: #F78811;">&#40;</span>a<span style="color: #000080;">%</span>2<span style="color: #000080;">==</span><span style="color: #F78811;">1</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span> <span style="color: #0000ff; font-weight: bold;">yield</span> b
        <span style="color: #0000ff; font-weight: bold;">val</span> sum <span style="color: #000080;">=</span> <span style="color: #F78811;">&#40;</span><span style="color: #F78811;">0</span> /<span style="color: #000080;">:</span> addNums<span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#40;</span><span style="color: #000080;">_</span>+<span style="color: #000080;">_</span><span style="color: #F78811;">&#41;</span>
        println<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;Numbers: %s, ansver: %d&quot;</span> format<span style="color: #F78811;">&#40;</span>addNums mkString<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot; + &quot;</span><span style="color: #F78811;">&#41;</span>, sum<span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span>
    <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></td></tr></table></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.staale.org/2009/07/another-pointless-challenge-solved-in-scala.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>A GWT request que</title>
		<link>http://blog.staale.org/2009/07/a-gwt-request-que.html</link>
		<comments>http://blog.staale.org/2009/07/a-gwt-request-que.html#comments</comments>
		<pubDate>Fri, 17 Jul 2009 11:02:18 +0000</pubDate>
		<dc:creator>Staale</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Django]]></category>
		<category><![CDATA[GWT]]></category>
		<category><![CDATA[Java]]></category>

		<guid isPermaLink="false">http://blog.staale.org/?p=112</guid>
		<description><![CDATA[I have gotten the opportunity to work a bit with GWT likely, which is a technology I really like. And I am happy that I am now able to try it out more, and learn how to use it. One off the things I needed to work, was to request data from a different server, [...]]]></description>
			<content:encoded><![CDATA[<p>I have gotten the opportunity to work a bit with GWT likely, which is a technology I really like. And I am happy that I am now able to try it out more, and learn how to use it. One off the things I needed to work, was to request data from a different server, as I integrate with Python Django on the back end. I could create a forwarding servlet to handle the communication to deal with the same origin policy off browsers, but I have instead opted for using a Script tag approach that I found from the <a href="http://code.google.com/p/google-web-toolkit-doc-1-5/wiki/FAQ_JSONFeedsFromOtherDomain">GWT faq</a> and <a href="http://giantflyingsaucer.com/blog/?p=126">elsewhere</a>.</p>
<p>However, I had one job that resulted in over 20 additional request jobs. So I wanted a system that throttled this, and only kept a limited amount of requests live at the same time. So I needed a solution a bit beyond what I had in the examples.</p>
<h3>Circumventing the SOP when fetching JSON data</h3>
<p>The way you circumvent the SOP in browsers, is to use script tags. You add a script tag to the document with the url for the JSON service, and you also add a parameter for which callback method the service should use. The service then creates the json data, and wraps it in &#8220;callback(&lt;json-data&gt;)&#8221;, so when the page loads, that method will get invoked, giving you the data. This does of course require the server you are communicating with to support this method of data fetching.</p>
<h3>Server side Django views</h3>
<p>To ease the creation off the JSON views in Python, I created a decorator that, when applied to a method, would wrap it such that it only needed to return something that could be json encoded. Then encode and send the result. The actual code for this relies on en extension off the DecoratorBase I posted about <a href="http://blog.staale.org/2009/03/python-decorators.html">here</a>, so I will not post it here. Maybe I can update this in a seperate post.</p>
<h3>Easy request building</h3>
<p>To make it easier to code the requests, I used the builder pattern to create JsonRequestJob objects. The JsonRequrestJob objects are rather simple:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">class</span> JsonRequestJob<span style="color: #339933;">&lt;</span>T <span style="color: #000000; font-weight: bold;">extends</span> JavaScriptObject<span style="color: #339933;">&gt;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> url<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> params<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> AsyncDataCallback<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> callback<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> JsonRequestJob<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> url, <span style="color: #003399;">String</span> params, AsyncDataCallback<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> callback<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">url</span> <span style="color: #339933;">=</span> url<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">params</span> <span style="color: #339933;">=</span> params<span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">callback</span> <span style="color: #339933;">=</span> callback<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>And here is the builder:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">class</span> JsonRequestBuilder <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> url<span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">final</span> StringBuilder params <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> StringBuilder<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> JsonRequestBuilder<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> url<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">url</span> <span style="color: #339933;">=</span> url<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> JsonRequestBuilder param<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> key, <span style="color: #003399;">String</span> value<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        params.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'&amp;'</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>key<span style="color: #009900;">&#41;</span>.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'='</span><span style="color: #009900;">&#41;</span>.<span style="color: #006633;">append</span><span style="color: #009900;">&#40;</span>value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">this</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> exec<span style="color: #009900;">&#40;</span>AsyncDataCallback<span style="color: #339933;">&lt;?</span> <span style="color: #000000; font-weight: bold;">extends</span> JavaScriptObject<span style="color: #339933;">&gt;</span> callback<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        jobs.<span style="color: #006633;">add</span><span style="color: #009900;">&#40;</span><span style="color: #000000; font-weight: bold;">new</span> JsonRequestJob<span style="color: #009900;">&#40;</span>url, params.<span style="color: #006633;">toString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>, callback<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        dispatchJob<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>To actually create a builder, I have a DataFetcher class (which contains the above 2 classes), and it has a get method for creating a builder. The DataFetcher also need configuration in the form off a base url. This is set up by including a script tag in the root element:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;script language=&quot;javascript&quot;&gt;
&nbsp;
var dataUrlBase = &quot;http://localhost:8000/etc&quot;
&nbsp;
&lt;script&gt;</pre></td></tr></table></div>

<p>And here is the get method. Keep in mind that the function argument here refers to which view/function I am calling on the Python Django side off things:</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="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">native</span> <span style="color: #003399;">String</span> getDataUrlBase<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">/*-{
    return $wnd[&quot;dataUrlBase&quot;]
}-*/</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> JsonRequestBuilder get<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> function<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #000000; font-weight: bold;">new</span> JsonRequestBuilder<span style="color: #009900;">&#40;</span>getDataUrlBase<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">+</span>function<span style="color: #339933;">+</span><span style="color: #0000ff;">&quot;.json?callback=&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Finally, the actual DataFetcher needs to have a que for adding and dequing jobs, as well as an implementation that actually creates and inserts the script tag, sets up the callback method to call when data is loaded, as well as ensuring the max number of requests is kept at a minimum.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> DataFetcher <span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> Queue<span style="color: #339933;">&lt;</span>JsonRequestJob<span style="color: #339933;">&gt;</span> jobs <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> LinkedList<span style="color: #339933;">&lt;</span>JsonRequestJob<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #000066; font-weight: bold;">int</span> MAX_JOB_COUNT <span style="color: #339933;">=</span> <span style="color: #cc66cc;">3</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">int</span> jobCount <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">int</span> requestCount <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000066; font-weight: bold;">void</span> dispatchJob<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>jobCount <span style="color: #339933;">&gt;=</span> MAX_JOB_COUNT <span style="color: #339933;">||</span> jobs.<span style="color: #006633;">isEmpty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">return</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        jobCount<span style="color: #339933;">++;</span>
        <span style="color: #000000; font-weight: bold;">final</span> JsonRequestJob job <span style="color: #339933;">=</span> jobs.<span style="color: #006633;">poll</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        makeJSONRequest<span style="color: #009900;">&#40;</span>job.<span style="color: #006633;">url</span>, job.<span style="color: #006633;">params</span>, job.<span style="color: #006633;">callback</span>, requestCount<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        requestCount <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>requestCount <span style="color: #339933;">+</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">%</span> <span style="color: #cc66cc;">5000</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">private</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #000000; font-weight: bold;">native</span> <span style="color: #339933;">&lt;</span>T <span style="color: #000000; font-weight: bold;">extends</span> JavaScriptObject<span style="color: #339933;">&gt;</span> <span style="color: #000066; font-weight: bold;">void</span> makeJSONRequest<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> url, <span style="color: #003399;">String</span> params, AsyncDataCallback<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> handler, <span style="color: #000066; font-weight: bold;">int</span> requestId<span style="color: #009900;">&#41;</span> <span style="color: #666666; font-style: italic;">/*-{
    var callback = &quot;callback&quot; + requestId;
&nbsp;
    var script = document.createElement(&quot;script&quot;);
    script.setAttribute(&quot;src&quot;, url+callback+params);
    script.setAttribute(&quot;type&quot;, &quot;text/javascript&quot;);
&nbsp;
    $wnd[callback] = function(jsonObj) {
        $wnd[callback + &quot;done&quot;] = true;
        @DataFetcher::dispatchJSON(Lcom/google/gwt/core/client/JavaScriptObject;LAsyncDataCallback;)(jsonObj, handler);
    }
&nbsp;
    setTimeout(function() {
        if (!$wnd[callback + &quot;done&quot;]) {
            @DataFetcher::dispatchJSON(Lcom/google/gwt/core/client/JavaScriptObject;LAsyncDataCallback;)(null, handler);
        }
&nbsp;
        // cleanup
        $wnd.document.getElementsByTagName(&quot;head&quot;)[0].removeChild(script);
        delete $wnd[callback];
        delete $wnd[callback + &quot;done&quot;];
    }, 1000);
&nbsp;
    $wnd.document.getElementsByTagName(&quot;head&quot;)[0].appendChild(script);
&nbsp;
    }-*/</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">static</span> <span style="color: #339933;">&lt;</span>T <span style="color: #000000; font-weight: bold;">extends</span> JavaScriptObject<span style="color: #339933;">&gt;</span> <span style="color: #000066; font-weight: bold;">void</span> dispatchJSON<span style="color: #009900;">&#40;</span>JavaScriptObject jsonObj, AsyncDataCallback<span style="color: #339933;">&lt;</span>T<span style="color: #339933;">&gt;</span> handler<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        jobCount<span style="color: #339933;">--;</span>
        dispatchJob<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        handler.<span style="color: #006633;">handleData</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>T<span style="color: #009900;">&#41;</span> jsonObj<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Thankfully, JavaScript client side is singlethreaded, so we don&#8217;t have to worry about race conditions or synchronizations. If we needed to do that, this could could obviously break, as it has race conditions in relation to the increment/decrement and checking off the jobCount variable. Also notice that there is a timeout added that checks that the page is loaded within 1 second. If it doesn&#8217;t, null is sent to the callback, and the actual callback method is removed from the page.</p>
<p>The code is not entirely complete. The AsyncDataCallback interface is missing (but it&#8217;s tiny and easy to implement). </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.staale.org/2009/07/a-gwt-request-que.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Using traits to configure Berkeley JDP</title>
		<link>http://blog.staale.org/2009/03/using-traits-to-configure-berkely-jdp.html</link>
		<comments>http://blog.staale.org/2009/03/using-traits-to-configure-berkely-jdp.html#comments</comments>
		<pubDate>Fri, 27 Mar 2009 16:13:27 +0000</pubDate>
		<dc:creator>Staale</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Scala]]></category>
		<category><![CDATA[BerkeleyDB]]></category>
		<category><![CDATA[JDP]]></category>

		<guid isPermaLink="false">http://blog.staale.org/?p=70</guid>
		<description><![CDATA[I have been using BerkeleyDBs JDP on and off a bit, just experementing with it to see how it works. BerkeleyDB is basically a B-Tree with transaction support. You store key/value pairs, both being byte arrays. JDP &#8211; Java Direct Persistence &#8211; is a layer that is built on top off this using annotations. It [...]]]></description>
			<content:encoded><![CDATA[<p>I have been using BerkeleyDBs JDP on and off a bit, just experementing with it to see how it works. BerkeleyDB is basically a B-Tree with transaction support. You store key/value pairs, both being byte arrays. JDP &#8211; Java Direct Persistence &#8211; is a layer that is built on top off this using annotations. It provides you with JPA like architecture for storing objects. Except there is no underlying SQL, just pure key lookups. This means you can&#8217;t write complex queries to select data, you will have to use code instead. But imho. this also means the complexity off what you are doing becomes more apparent.</p>
<p>I have fiddled around a bit with this, and I wanted to create a clean way off configuring environments. When you are using BerkeleyDB, you need to set up an Environment first:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">Environment</span><span style="color: #009900;">&#40;</span><span style="color: #003399;">File</span> envHome, EnvironmentConfig configuration<span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>The EnvironmentConfig in return is set up using a setters:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="java" style="font-family:monospace;">EnvironmentConfig envConfig <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> EnvironmentConfig<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
envConfig.<span style="color: #006633;">setTransactional</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
envConfig.<span style="color: #006633;">setAllowCreate</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
envConfig.<span style="color: #006633;">setCacheSize</span><span style="color: #009900;">&#40;</span><span style="color: #cc66cc;">1000000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>However, for Scala I wanted to use chaining to set up the environment config. The base implementation does not return this in the setters, so you can&#8217;t chain the statements. My first idea was to use implicit to convert the EnvironmentConfig to a richer version, that simply returned itself, so you could chain it.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">class</span> RichEnvConfig<span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">val</span> config<span style="color: #000080;">:</span> EnvironmentConfig<span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">def</span> transactional<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>config.<span style="color: #000000;">setTransactional</span><span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">true</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">;</span> <span style="color: #0000ff; font-weight: bold;">this</span><span style="color: #F78811;">&#125;</span>
    <span style="color: #0000ff; font-weight: bold;">def</span> allowCreate<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>config.<span style="color: #000000;">setAllowCreate</span><span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">true</span><span style="color: #F78811;">&#41;</span><span style="color: #000080;">;</span> <span style="color: #0000ff; font-weight: bold;">this</span><span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></td></tr></table></div>

<p>However I didn&#8217;t get it to work properly. I also thought about using operators to chain together traits. But then it occured to me that using traits themselves might make things clearer. So I created the following class:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">object</span> EnvConfig <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">implicit</span> det unwrap<span style="color: #F78811;">&#40;</span>envCfg<span style="color: #000080;">:</span> EnvConfig<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> envCfg.<span style="color: #000000;">config</span>
<span style="color: #F78811;">&#125;</span>
<span style="color: #0000ff; font-weight: bold;">class</span> EnvConfig <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> config <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> EnvironmentConfig<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>
    <span style="color: #0000ff; font-weight: bold;">trait</span> Transactional <span style="color: #F78811;">&#123;</span> config.<span style="color: #000000;">setTransactional</span><span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">true</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#125;</span>
    <span style="color: #0000ff; font-weight: bold;">trait</span> AllowCreate <span style="color: #F78811;">&#123;</span> config.<span style="color: #000000;">setTransactional</span><span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">true</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></td></tr></table></div>

<p>Now I could more easily create EnvironmentConfigs and use them in code:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">val</span> config <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> EnvConfig <span style="color: #0000ff; font-weight: bold;">with</span> Transactional <span style="color: #0000ff; font-weight: bold;">with</span> AllowCreate
<span style="color: #0000ff; font-weight: bold;">val</span> env <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> Environment<span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">new</span> File<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;.&quot;</span><span style="color: #F78811;">&#41;</span>, config<span style="color: #F78811;">&#41;</span></pre></td></tr></table></div>

<p>I do however want to use the loan pattern with the Environment, as the Environment requires you to call close on it after use. So I thought instead off having a class for EnvironmentConfig, I could use have one for Environment itself, with an apply method. The Environment wouldn&#8217;t be created until it was required in apply, and using traits you could configure the Environment. Pretty happy with myself with the final solution for the Environment part:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">class</span> Env<span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">val</span> path<span style="color: #000080;">:</span>File<span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">trait</span> Transactional <span style="color: #0000ff; font-weight: bold;">extends</span> Env <span style="color: #F78811;">&#123;</span> config.<span style="color: #000000;">setTransactional</span><span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">true</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#125;</span>
    <span style="color: #0000ff; font-weight: bold;">trait</span> AllowCreate <span style="color: #0000ff; font-weight: bold;">extends</span> Env <span style="color: #F78811;">&#123;</span> config.<span style="color: #000000;">setAllowCreate</span><span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">true</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#125;</span>
&nbsp;
    <span style="color: #0000ff; font-weight: bold;">val</span> config <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> EnvironmentConfig<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span>
    <span style="color: #0000ff; font-weight: bold;">def</span> apply<span style="color: #F78811;">&#91;</span>T<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#40;</span>f<span style="color: #000080;">:</span><span style="color: #F78811;">&#40;</span>Environment<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=&gt;</span> T<span style="color: #F78811;">&#41;</span> <span style="color: #000080;">=</span> <span style="color: #F78811;">&#123;</span>
        <span style="color: #0000ff; font-weight: bold;">val</span> env <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> Environment<span style="color: #F78811;">&#40;</span>path, config<span style="color: #F78811;">&#41;</span>
        <span style="color: #0000ff; font-weight: bold;">try</span> <span style="color: #F78811;">&#123;</span> f<span style="color: #F78811;">&#40;</span>env<span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#125;</span> <span style="color: #0000ff; font-weight: bold;">finally</span> <span style="color: #F78811;">&#123;</span> env.<span style="color: #000000;">close</span><span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#125;</span>
&nbsp;
    <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span>
&nbsp;
<span style="color: #0000ff; font-weight: bold;">object</span> EnvTest <span style="color: #0000ff; font-weight: bold;">extends</span> Application <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> env <span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">new</span> Env<span style="color: #F78811;">&#40;</span><span style="color: #0000ff; font-weight: bold;">new</span> File<span style="color: #F78811;">&#40;</span><span style="color: #6666FF;">&quot;.&quot;</span><span style="color: #F78811;">&#41;</span><span style="color: #F78811;">&#41;</span> <span style="color: #0000ff; font-weight: bold;">with</span> Transactional <span style="color: #0000ff; font-weight: bold;">with</span> AllowCreate
    env <span style="color: #F78811;">&#123;</span> e <span style="color: #000080;">=&gt;</span>
        println<span style="color: #F78811;">&#40;</span>e<span style="color: #F78811;">&#41;</span>
    <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></td></tr></table></div>

<p>Some handy links to BerkeleyDB Java Edition:</p>
<ul>
<li><a href="http://www.oracle.com/database/berkeley-db/je/index.html">BerkeleyDB Java Edition</a></li>
<li><a href="http://www.oracle.com/technology/documentation/berkeley-db/je/GettingStartedGuide/index.html">Getting started guide</a></li>
<li><a href="http://www.oracle.com/technology/documentation/berkeley-db/je/java/index.html">BerkeleyDB JavaDoc</a></li>
</ul>
<div id="e_clipper" style="border: 0pt none; margin: 10px; padding: 0pt; position: absolute; right: 0px; z-index: 100000; top: 0px;"></div>
<div id="evernote_clip_form" style="display: none;">
<form action="http://www.evernote.com/clip.action" accept-charset="UTF-8" enctype="application/x-www-form-urlencoded" method="post">
<input name="url" type="text" />
<input name="format" type="text" /><textarea name="body"></textarea></p>
<input name="title" type="text" />
<input name="quicknote" type="text" /></form>
</div>
]]></content:encoded>
			<wfw:commentRss>http://blog.staale.org/2009/03/using-traits-to-configure-berkely-jdp.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Python decorators</title>
		<link>http://blog.staale.org/2009/03/python-decorators.html</link>
		<comments>http://blog.staale.org/2009/03/python-decorators.html#comments</comments>
		<pubDate>Fri, 20 Mar 2009 15:08:15 +0000</pubDate>
		<dc:creator>Staale</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Python]]></category>
		<category><![CDATA[Decorators]]></category>

		<guid isPermaLink="false">http://blog.staale.org/?p=48</guid>
		<description><![CDATA[How to create class decorators in Python that are relatively transparent as decorators for the outside world.]]></description>
			<content:encoded><![CDATA[<p>I have a project at work where in Python, where I use decorators to wrap functionality for views. I hit on a snag when I used 2 decorators on 1 function. I like using class decorators, I read some recommendations regarding that, and it fits better with my Java upbringing <img src='http://blog.staale.org/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> . The 2 decorators I needed to apply to the same function looked like this (__call__ method omitted for brevity):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> AutoTemplate<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, func<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">func</span> = func
        pathElems = func.__module__.<span style="color: black;">split</span><span style="color: black;">&#40;</span><span style="color: #483d8b;">&quot;.&quot;</span><span style="color: black;">&#41;</span><span style="color: black;">&#91;</span>:-<span style="color: #ff4500;">1</span><span style="color: black;">&#93;</span>+<span style="color: black;">&#91;</span><span style="color: #483d8b;">&quot;%s.html&quot;</span><span style="color: #66cc66;">%</span>func.__name__<span style="color: black;">&#93;</span>
        <span style="color: #008000;">self</span>.<span style="color: black;">template</span> = <span style="color: #dc143c;">os</span>.<span style="color: black;">path</span>.<span style="color: black;">join</span><span style="color: black;">&#40;</span><span style="color: #66cc66;">*</span> pathElems<span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> AccessRequired<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, func<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">func</span> = func</pre></td></tr></table></div>

<p>In addition, I used the functions actual name in the urls.py file to generate the url tuple:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">def</span> createUrl<span style="color: black;">&#40;</span>method<span style="color: black;">&#41;</span>:
    name = method.<span style="color: black;">func</span>.<span style="color: black;">func_name</span>
    <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: black;">&#40;</span>r<span style="color: #483d8b;">'^%s.html$'</span><span style="color: #66cc66;">%</span>name, method, <span style="color: black;">&#123;</span><span style="color: black;">&#125;</span>, name<span style="color: black;">&#41;</span></pre></td></tr></table></div>

<p>So the finished product after decorators where applied needed to work for that. Because the decorators resulted in a class rather than a function being assigned to the the view, this didn&#8217;t work properly.</p>
<p>With a little help from <a href="http://stackoverflow.com/questions/666216/decorator-classes-in-python">Stackoverflow</a> however, I came upon a good solution, that makes it really transparent that a function is actually decorated:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
</pre></td><td class="code"><pre class="python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> DecoratorBase<span style="color: black;">&#40;</span><span style="color: #008000;">object</span><span style="color: black;">&#41;</span>:
    <span style="color: #ff7700;font-weight:bold;">def</span> <span style="color: #0000cd;">__init__</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, func<span style="color: black;">&#41;</span>:
        <span style="color: #008000;">self</span>.<span style="color: black;">func</span> = func
        <span style="color: #ff7700;font-weight:bold;">for</span> n <span style="color: #ff7700;font-weight:bold;">in</span> <span style="color: #008000;">set</span><span style="color: black;">&#40;</span><span style="color: #008000;">dir</span><span style="color: black;">&#40;</span>func<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span> - <span style="color: #008000;">set</span><span style="color: black;">&#40;</span><span style="color: #008000;">dir</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>:
            <span style="color: #008000;">setattr</span><span style="color: black;">&#40;</span><span style="color: #008000;">self</span>, n, <span style="color: #008000;">getattr</span><span style="color: black;">&#40;</span>func, n<span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">class</span> AutoTemplate<span style="color: black;">&#40;</span>DecoratorBase<span style="color: black;">&#41;</span>: <span style="color: #ff7700;font-weight:bold;">pass</span>
<span style="color: #ff7700;font-weight:bold;">class</span> AccessRequired<span style="color: black;">&#40;</span>DecoratorBase<span style="color: black;">&#41;</span>: <span style="color: #ff7700;font-weight:bold;">pass</span></pre></td></tr></table></div>

<p>This does not work for decorators requiring parameters, I will see if I can find a solution for that if I need it.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.staale.org/2009/03/python-decorators.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Euler problem 1 in Scala</title>
		<link>http://blog.staale.org/2009/03/euler-problem-1-in-scala.html</link>
		<comments>http://blog.staale.org/2009/03/euler-problem-1-in-scala.html#comments</comments>
		<pubDate>Wed, 18 Mar 2009 06:20:08 +0000</pubDate>
		<dc:creator>Staale</dc:creator>
				<category><![CDATA[Programming]]></category>
		<category><![CDATA[Scala]]></category>
		<category><![CDATA[Euler]]></category>

		<guid isPermaLink="false">http://blog.staale.org/?p=28</guid>
		<description><![CDATA[The first problem in project Euler is trivial:


If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.
Find the sum of all the multiples of 3 or 5 below 1000.

This can be solved quite easily mathematically. [...]]]></description>
			<content:encoded><![CDATA[<p>The first problem in project Euler is trivial:</p>
<blockquote>
<div class="problem_content">
<p>If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23.</p>
<p>Find the sum of all the multiples of 3 or 5 below 1000.</p></div>
</blockquote>
<p>This can be solved quite easily mathematically. By using the formulas for calculating the sum of all numbers up to a given number. However, since I am working on learning Scala, I think it&#8217;s a nice problem to start the language at.</p>
<p>Let&#8217;s first try and solve it in a traditional Java way in scala</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">object</span> Euler001 <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">def</span> main<span style="color: #F78811;">&#40;</span>args<span style="color: #000080;">:</span>Array<span style="color: #F78811;">&#91;</span>String<span style="color: #F78811;">&#93;</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
        <span style="color: #0000ff; font-weight: bold;">var</span> i<span style="color: #000080;">=</span><span style="color: #F78811;">1</span>
        <span style="color: #0000ff; font-weight: bold;">var</span> sum<span style="color: #000080;">=</span><span style="color: #F78811;">0</span>
        <span style="color: #0000ff; font-weight: bold;">while</span> <span style="color: #F78811;">&#40;</span>i<span style="color: #000080;">&lt;</span><span style="color: #F78811;">1000</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
            <span style="color: #0000ff; font-weight: bold;">if</span> <span style="color: #F78811;">&#40;</span>i<span style="color: #000080;">%</span>3 <span style="color: #000080;">==</span> <span style="color: #F78811;">0</span> || i<span style="color: #000080;">%</span>5 <span style="color: #000080;">==</span><span style="color: #F78811;">0</span><span style="color: #F78811;">&#41;</span> sum +<span style="color: #000080;">=</span> i
            i +<span style="color: #000080;">=</span> <span style="color: #F78811;">1</span>
        <span style="color: #F78811;">&#125;</span>
        println<span style="color: #F78811;">&#40;</span>sum<span style="color: #F78811;">&#41;</span>
    <span style="color: #F78811;">&#125;</span>
<span style="color: #F78811;">&#125;</span></pre></td></tr></table></div>

<p>Now, anyone that has done functional programming will cringe at this. There are several problems with this program. First, it&#8217;s too verbose, we can express the same in less code. Additionally we are using var instead of val. In Scala, we prefix variables with var or val. For Java comparison, val means the variable is final and not reassignable, var means it is. We don&#8217;t need to say the types here, as that&#8217;s inferred.</p>
<p>But, I don&#8217;t want to drone on too much about language details, you can learn those other places for Scala. I will first make an example that is more compact, and introduces things that are different from Java.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">object</span> Euler001 <span style="color: #0000ff; font-weight: bold;">extends</span> Application <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">var</span> sum <span style="color: #000080;">=</span> <span style="color: #F78811;">0</span>
    <span style="color: #0000ff; font-weight: bold;">for</span> <span style="color: #F78811;">&#40;</span>i <span style="color: #000080;">&lt;</span>- <span style="color: #F78811;">1</span> to <span style="color: #F78811;">999</span><span style="color: #F78811;">&#41;</span> <span style="color: #F78811;">&#123;</span>
        <span style="color: #0000ff; font-weight: bold;">if</span> <span style="color: #F78811;">&#40;</span>i<span style="color: #000080;">%</span>3 <span style="color: #000080;">==</span> <span style="color: #F78811;">0</span> || i<span style="color: #000080;">%</span>5 <span style="color: #000080;">==</span><span style="color: #F78811;">0</span><span style="color: #F78811;">&#41;</span> sum +<span style="color: #000080;">=</span> i    
    <span style="color: #F78811;">&#125;</span>
    println<span style="color: #F78811;">&#40;</span>sum<span style="color: #F78811;">&#41;</span>
<span style="color: #F78811;">&#125;</span></pre></td></tr></table></div>

<p>Now we still have a var variable here, but the iteration part looks much nicer. The thing to note here is the 1 to 999 construct. This actually is a 1.to(999) call. Numbers are true objects in Scala, with methods on them. However, the dots can be replaced with spaces for a more natural language. In addition, methods that take just 1 argument, we don&#8217;t have to include (), so this makes for a nicer to read syntax. We also changed the object to extend Application, which means we don&#8217;t need to declare the main method.</p>
<p>I have also not really explained how object works in Scala. They are like static methods in Java classes, but you can extend them. So you can add methods from another class in your static context by simply extending a class.</p>
<p>Still, we can do better. Functional languages focus on immutable values, and extracting information from expressions. The reality is that every expression in Scala results in a value, the value from the last statement in the expression.<br />
So we could actually write:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;">    sum +<span style="color: #000080;">=</span> <span style="color: #0000ff; font-weight: bold;">if</span> <span style="color: #F78811;">&#40;</span>i<span style="color: #000080;">%</span>3 <span style="color: #000080;">==</span> <span style="color: #F78811;">0</span> || i<span style="color: #000080;">%</span>5 <span style="color: #000080;">==</span><span style="color: #F78811;">0</span><span style="color: #F78811;">&#41;</span> i <span style="color: #0000ff; font-weight: bold;">else</span> <span style="color: #F78811;">0</span></pre></td></tr></table></div>

<p>But functional languages are more about immutability, so we should get rid of the last var, sum. Instead we should construct expressions that results in final parts off the process:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
</pre></td><td class="code"><pre class="scala" style="font-family:monospace;"><span style="color: #0000ff; font-weight: bold;">object</span> Euler001 <span style="color: #0000ff; font-weight: bold;">extends</span> Application <span style="color: #F78811;">&#123;</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> numbers <span style="color: #000080;">=</span> <span style="color: #F78811;">1</span> to <span style="color: #F78811;">1000</span> filter<span style="color: #F78811;">&#40;</span><span style="color: #F78811;">&#40;</span>x<span style="color: #F78811;">&#41;</span><span style="color: #000080;">=&gt;</span>x<span style="color: #000080;">%</span>3 <span style="color: #000080;">==</span> <span style="color: #F78811;">0</span> || x<span style="color: #000080;">%</span>5<span style="color: #000080;">==</span><span style="color: #F78811;">0</span><span style="color: #F78811;">&#41;</span>
    <span style="color: #0000ff; font-weight: bold;">val</span> sum <span style="color: #000080;">=</span> numbers reduceLeft<span style="color: #F78811;">&#40;</span><span style="color: #000080;">_</span>+<span style="color: #000080;">_</span><span style="color: #F78811;">&#41;</span>
    println<span style="color: #F78811;">&#40;</span>sum<span style="color: #F78811;">&#41;</span>
<span style="color: #F78811;">&#125;</span></pre></td></tr></table></div>

<p>Here I introduce even more unfamiliar constructs for Java programmers. 1 to 999 produces a sequence of the numbers 1 to 999, the filter method applies a method to this sequence. The expression (X)=> means that we are creating a function that takes x. The code following could be a block in {}, but since it&#8217;s a simple statement we just include it directly. This method will actually get inferred by the compiler to have type (x:Int):Boolean &#8211; because it&#8217;s a method taking and Int (Scala wrapper for the integer primitive) and returns a boolean. The Int type is inferred from the type of the sequence, Int. And the return type is inferred from the result off our expression. The filter method also requires the return type to be boolean, so anything else would have been a compiler error.</p>
<p>The next piece of the magic is the reduceLeft on numbers. This method, basing on the previous line, should have looked something like (a,b)=>a+b. But in Scala there is a shorthand for such expressions, called partial functions. The underscore serves as a placeholder, and the first underscore will take the value off the first method parameter, and the second will take the second parameter and so on. The reduceLeft method takes a 2 argument functions, and returns another value off the same type. This is applied to all elements in the sequence, and the final reduced value is returned, which is the sum.</p>
<p>I might have gotten some terminology and concepts wrong in the text, but all the above code works in Scala. I hope to do more posts on Scala in the future, possibly solving some harder Euler problems. The above problem can of course also be solved by simple math:<br />
<img src="http://blog.staale.org/wp-content/plugins/wpmathpub/phpmathpublisher/img/math_982_973f69594446e7ddc4f32ed6e2d57b49.png" style="vertical-align:-18px; display: inline-block ;" alt="3*(333*334/2)+5*(199*200/2)-15*(66*67/2)" title="3*(333*334/2)+5*(199*200/2)-15*(66*67/2)"/><br />
(Add every 3rd number to 999 to every 5th number to 999 and subtract every 15th number, as we count those twice otherwise)</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.staale.org/2009/03/euler-problem-1-in-scala.html/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
