<?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>Ikke&#039;s blog &#187; Development</title>
	<atom:link href="http://eikke.com/category/development/feed/" rel="self" type="application/rss+xml" />
	<link>http://eikke.com</link>
	<description>&#039;cause this is what I do</description>
	<lastBuildDate>Sun, 13 Feb 2011 14:58:55 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3</generator>
		<item>
		<title>On Functional Programming Languages</title>
		<link>http://eikke.com/on-functional-programming-languages/</link>
		<comments>http://eikke.com/on-functional-programming-languages/#comments</comments>
		<pubDate>Wed, 07 Apr 2010 21:50:38 +0000</pubDate>
		<dc:creator>Nicolas</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[fp]]></category>
		<category><![CDATA[functional programming]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://eikke.com/?p=173</guid>
		<description><![CDATA[As a programming-language adept I&#8217;ve been studying the ideas, concepts and theory of functional programming (FP) and FP-related languages for about 2 years now, still learning new things everyday. Recently a &#8216;FP User Group&#8217; was started by some people at Ghent University, called GhentFPG, and the first meeting took place last thursday, with great interest [...]]]></description>
			<content:encoded><![CDATA[<p>As a programming-language adept I&#8217;ve been studying the ideas, concepts and theory of <a href="http://en.wikipedia.org/wiki/Functional_programming">functional programming</a> (FP) and FP-related languages for about 2 years now, still learning new things everyday.</p>
<p>Recently a &#8216;FP User Group&#8217; was started by some people at <a href="http://www.ugent.be">Ghent University</a>, called GhentFPG, and the first meeting took place last thursday, with great interest from students, university employees as well as people working in the industry. You can find some more info in the <a href="http://groups.google.com/group/ghent-fpg">GhentFPG Google Group</a> or in the <a href="http://www.haskell.org/haskellwiki/Ghent_Functional_Programming_Group">wiki</a> (where you can also find the slides of the presentations given during the first meeting).</p>
<p>Some days ago someone new to FP posted a <a href="http://groups.google.com/group/ghent-fpg/browse_thread/thread/81cee5d8c9a77266">message on the mailing list</a>, asking which language he should study, among other things.</p>
<p>Since I think my reply might be of general interest (also outside GhentFPG), I decided to post a copy on this blog as well (note I did add some extra markup). Comments welcome!</p>
<blockquote><p>
Based on my experience (which is biased, obviously):</p>
<ul>
<li>
<p>Functional Programming is not only a language-related thing. FP<br />
languages do enforce you to apply functional paradigms, but you can<br />
easily follow these paradigms in lots of other (more mainstream?)<br />
languages as well: it is easier to learn people a paradigm using a<br />
language they already know, rather than telling them FP is really cool<br />
and useful and interesting, but requires them to learn a new<br />
language/toolchain/&#8230; first.</p>
<p>Not talking about <a href="http://www.java.net">Java</a> or <a href="http://en.wikipedia.org/wiki/C%2B%2B">C++</a> or something similar here, rather <a href="http://www.python.org">Python</a><br />
and <a href="http://ruby-lang.org/">Ruby</a>.</p>
<li>
<p>If you&#8217;re into Java/<a href="http://msdn.microsoft.com/en-us/vcsharp/aa336809.aspx">C#</a>/&#8230;, <a href="http://www.scala-lang.org/"><em>Scala</em></a> is a really good introduction to FP:<br />
it allows you to write OOP code just like you do already, but also<br />
provides you lots of FP-related features, and pushes you gently into the<br />
FP approach. The book &#8220;Programming in Scala&#8221; by Odersky et al. (the main<br />
author of Scala) is IMO a really good intro to both Scala as well as the<br />
FP concepts it provides, not only showing them but also explaining<br />
gently why they&#8217;re useful, and why they&#8217;re &#8216;better&#8217; than the approaches<br />
you&#8217;re taking already.</p>
<p>The Scala type system is rather interesting as well.</p>
<p>It&#8217;s the gentle path, so you want <img src='http://eikke.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' />  Learning Scala before reading<br />
&#8216;<a href="http://www.realworldhaskell.org/blog/">Real World Haskell</a>&#8216; certainly helped me a lot to understand the latter.</p>
<li>
<p><a href="http://haskell.org/"><em>Haskell</em></a> is an incredibly interesting language because of the concepts<br />
it adopted and types it provides, but it does require an immediate mind<br />
switch when coming from a non-OOP world (I once spent about 2 hours to<br />
explain a Java-guy how classes and instances in Haskell relate to<br />
classes and instances in Java, it wasn&#8217;t obvious). &#8220;Real World Haskell&#8221;<br />
is certainly worth a read (and if you read &#8220;Programming in Scala&#8221; as<br />
well, you&#8217;ll notice lots of similarities).</p>
<p>I for one can read Haskell code pretty easily and learned lots of<br />
CS/math things thanks to learning it, but I&#8217;m (still) unable to write<br />
non-trivial code (I need some good project to get my hands dirty I<br />
guess).</p>
<li>
<p><a href="http://www.erlang.org"><em>Erlang</em></a> is really interesting from a (very specific) feature<br />
perspective: high-availability, distributed computing, the actor system<br />
and the OTP library on top of it,&#8230;</p>
<p>It&#8217;s a rather &#8216;old&#8217; language, but I kind of like it. Some people do<br />
complain about the syntax, but once you figured out &#8216;,&#8217;, &#8216;;&#8217; and &#8216;.&#8217; are<br />
used almost the same as they are in &#8216;human&#8217; written language, everything<br />
becomes obvious  <img src='http://eikke.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>Do note though Erlang is not a normal general-purpose language. You can<br />
code +- everything you want using it, but it&#8217;s really targeted to<br />
distributed/high-available/network applications. You most likely won&#8217;t<br />
use it to solve mathematical problems or write a game. It&#8217;s really good<br />
at what it&#8217;s built for though.</p>
<p>One final note: please don&#8217;t ever make the mistake I made. If you know<br />
Erlang, and take a look at Scala (which also has an actor library in the<br />
standard distribution, as well as the more advanced <a href="http://akkasource.org">Akka</a>-library), don&#8217;t<br />
judge Scala as being a competitor for Erlang, they&#8217;re both completely<br />
different languages targeting different applications. &#8216;Scala&#8217; is not<br />
about &#8216;scalability&#8217; as Erlang is (it&#8217;s a &#8220;Scalable Language&#8221;).</p>
<li>
<p><a href="http://research.microsoft.com/en-us/um/cambridge/projects/fsharp/"><em>F#</em></a> (and most likely <a href="http://caml.inria.fr/ocaml/"><em>OCaml</em></a> as well, although I never used it though) is<br />
certainly worth a look as well. I only read 3/4th of a book on it, but<br />
it looks really promising and interesting.</p>
<li>
<p>There&#8217;s obviously all sorts of <a href="http://en.wikipedia.org/wiki/Lisp_(programming_language)"><em>Lisp</em></a> dialects. I have no opinion on<br />
them, never looked into any Lisp closely enough. I only wrote some<br />
<a href="http://clojure.org"><em>Clojure</em></a> (a Lisp-dialect for the JVM) code one day, but need to learn<br />
more about the Lisp-way of programming. Clojure seems to be interesting<br />
because of the deep integration of <a href="http://en.wikipedia.org/wiki/Software_transactional_memory">Software Transactional Memory</a> (STM)<br />
in the language (yet another approach to concurrency <img src='http://eikke.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> ).</p>
</ul>
<p>As for the IDE question: <a href="http://www.vim.org">Vim</a> and a decent terminal are all you need,<br />
luckily none of the above languages require you to learn how to use a<br />
toolchain which enforces you (or some magic IDE) to write 500 lines of<br />
XML-based build &#8216;programs&#8217; or other insanities.</p>
<p>My advice: pick some language, learn it, but make sure you don&#8217;t only<br />
learn the language, but especially the concepts (type system,<br />
higher-order stuff, list manipulation,&#8230;). Then pick some other<br />
language and learn it as well (which will be easier since you got the<br />
concepts already) and so on.</p>
<p>And read tons of papers available on the internet in between <img src='http://eikke.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' />  Even if<br />
you don&#8217;t understand a paper completely, you&#8217;ll pick up some things<br />
already, and re-reading it 2 weeks later helps a lot <img src='http://eikke.com/wp-includes/images/smilies/icon_biggrin.gif' alt=':-D' class='wp-smiley' /> </p>
<p>Just my .02,</p>
<p>Nicolas</p>
</blockquote>
]]></content:encoded>
			<wfw:commentRss>http://eikke.com/on-functional-programming-languages/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Book review: Python Testing: Beginner&#8217;s Guide</title>
		<link>http://eikke.com/book-review-python-testing-beginners-guide/</link>
		<comments>http://eikke.com/book-review-python-testing-beginners-guide/#comments</comments>
		<pubDate>Tue, 23 Feb 2010 21:19:20 +0000</pubDate>
		<dc:creator>Nicolas</dc:creator>
				<category><![CDATA[Development]]></category>

		<guid isPermaLink="false">http://eikke.com/?p=156</guid>
		<description><![CDATA[Recently a new book on software testing in Python projects was published by Packt Publishing, &#8220;Python Testing: Beginner&#8217;s Guide&#8221;. I&#8217;ll read the book the coming weeks (it didn&#8217;t arrive yet) and publish a review later on. If you&#8217;re into Python software development, you might want to take a look at the book webpage, or download [...]]]></description>
			<content:encoded><![CDATA[<p>Recently a new book on software testing in Python projects was published by Packt Publishing, &#8220;Python Testing: Beginner&#8217;s Guide&#8221;. I&#8217;ll read the book the coming weeks (it didn&#8217;t arrive yet) and publish a review later on.</p>
<p>If you&#8217;re into Python software development, you might want to take a look at the book <a title="Python Testing: Beginner's Guide" href="http://www.packtpub.com/python-testing-beginners-guide/book?utm_source=eikke.com&amp;utm_medium=bookrev&amp;utm_content=blog&amp;utm_campaign=mdb_002562">webpage</a>, or download a free <a title="When Doctest isn't Enough: Unittest to the Rescue" href="http://www.packtpub.com/files/8846-python-testing-beginners-guide-sample-chapter-5-when-doctest-isnt-enough-unittest-to-the-rescue.pdf">chapter</a> (PDF) about the standard Python <em>unittest</em> package for now.</p>
<p>Since I&#8217;m not a big fan of extensive/exaggerated unit testing and rather believe in functional/component tests, I&#8217;m looking forward to read the book and learn some new things. Looking forward to the web application testing chapter, among others.</p>
<p><em><strong>Disclaimer:</strong> I was invited by Packt Publishing to review this book, and they provide a free copy.</em></p>
]]></content:encoded>
			<wfw:commentRss>http://eikke.com/book-review-python-testing-beginners-guide/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Scala tail recursion and decompiler adventures</title>
		<link>http://eikke.com/scala-tail-recursion-decompiler/</link>
		<comments>http://eikke.com/scala-tail-recursion-decompiler/#comments</comments>
		<pubDate>Wed, 12 Aug 2009 22:31:03 +0000</pubDate>
		<dc:creator>Nicolas</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[functional programming]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[scala]]></category>

		<guid isPermaLink="false">http://eikke.com/?p=129</guid>
		<description><![CDATA[I've been into Scala lately. More about it will follow later, but there's something I found out which I really like.

Last couple of days I've been writing some very basic Scala snippets, containing constructs which would be non-trivial or 'unusual' to write in Java, compile it to a class file, and then use a Java decompiler to figure out how the Scala compiler maps those constructs on the JVM.

There's one thing which took my immediate attention: looks like (basic) tail-recursive functions are optimized into while-loops!]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve been into <a href="http://www.scala-lang.org">Scala</a> lately. More about it will follow later, but there&#8217;s something I found out which I really like.</p>
<p>Last couple of days I wrote some very basic Scala snippets, containing constructs which would be non-trivial or &#8216;unusual&#8217; to write in <a href="http://java.sun.com">Java</a>, compile it to a class file, and then use a <a href="http://java.decompiler.free.fr">Java decompiler</a> to figure out how the Scala compiler maps those constructs to JVM bytecodes.</p>
<p>There&#8217;s one thing which took my attention: looks like (basic) tail-recursive functions are optimized into while-loops! This only happens if the last call of a function is a call to itself (the most basic form of tail recursion), but it&#8217;s an interesting feature anyway&#8230; No more need to put socket accept handling in an infinite while loop <img src='http://eikke.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /><br />
<span id="more-129"></span><br />
A little demo. First, here&#8217;s a Scala object which implements a very basic &#8216;reduce&#8217; function:</p>
<pre>
object Reducer {
  def reduce[T, V](fun: (V, T) => V, values: List[T], initial: V): V = {
    if(values isEmpty)
      return initial
    val next = fun(initial, values head)
    return reduce(fun, values tail, next)
  }

  def main(args: Array[String]): Unit = {
    val values = List(1, 2, 3, 4)
    val sum = reduce[Int, Int]((x, y) => x + y, values, 0)
    println("Result: " + sum)
  }
}
</pre>
<p>We can compile and run this, and it&#8217;ll output the expected result &#8217;10&#8242;:</p>
<pre>MacBook:reduce nicolas $ scalac Reducer.scala
MacBook:reduce nicolas $ scala Reducer
Result: 10</pre>
<p>Now we can open the generated class files in JD. There are a couple of them (it&#8217;s interesting to take a look at all of them and figure out what they represent exactly), but in this case we need &#8216;Reducer$.class&#8217;, which contains the implementations of our public functions, including &#8216;reduce&#8217;.</p>
<p>Here&#8217;s the Java version of the &#8216;reduce&#8217; function:</p>
<pre>public &lt;T, V&gt; V reduce(Function2&lt;V, T, V&gt; fun, List&lt;T&gt; values, V initial)
{
  while (true)
  {
    if (values.isEmpty())
      return initial;
    Object next = fun.apply(initial, values.head());
    initial = next;
    values = values.tail();
  }
}
</pre>
<p>&#8216;Function2&#8242; is a built-in Scala type which represents a function taking 2 parameters. As you can see, this code does exactly the same as our Scala version and is most likely the way we&#8217;d write the code manually as well (the only thing I don&#8217;t get is why &#8216;next&#8217; is an Object and not a &#8216;V&#8217;, I might figure that out later), but without forcing us to write the imperative code, whilst still producing bytecodes which will most likely show the best performance on the JVM (which currently has no tail recursion optimization support (although <a href="http://openjdk.java.net/projects/mlvm/subprojects.html#TailCalls">that might change one day</a>)).</p>
<p>I like it <img src='http://eikke.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>[update]<br />
For reference, here&#8217;s a slightly more Scala-ish implementation of <em>reduce</em>, showing the same time performance characteristics during some basic profiling. I was not able to get JD nor jad to generate any usable decompiled code though:</p>
<pre>
def reduce[T, V](fun: (V, T) => V, values: List[T], initial: V): V = {
    values match {
        case List() => initial;
        case head :: tail => reduce(fun, tail, fun(initial, head))
    }
}
</pre>
<p>It uses Scala&#8217;s &#8220;List&#8221; pattern matching functionality.</p>
]]></content:encoded>
			<wfw:commentRss>http://eikke.com/scala-tail-recursion-decompiler/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Importing a Git tree into a Subversion repository</title>
		<link>http://eikke.com/importing-a-git-tree-into-a-subversion-repository/</link>
		<comments>http://eikke.com/importing-a-git-tree-into-a-subversion-repository/#comments</comments>
		<pubDate>Sat, 25 Jul 2009 18:07:00 +0000</pubDate>
		<dc:creator>Nicolas</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[git]]></category>
		<category><![CDATA[subversion]]></category>
		<category><![CDATA[svn]]></category>

		<guid isPermaLink="false">http://eikke.com/?p=125</guid>
		<description><![CDATA[Recently I worked on some new project, and as always I created a local <a href="http://git-scm.com">Git</a> repository as a start. After working on it several days, creating lots of commits, I had to publish it into the central <a href="http://subversion.tigris.org">Subversion</a> repository (which is one of the <a href="http://en.wikipedia.org/wiki/Version_control_system">VCS</a>s we got). I could have done this by creating a new folder in SVN and add the latest version of all files of the project to it, but that way all history would be gone, which I didn't like.

Git has a feature to work with SVN repositories, <em>git-svn</em>, but that's intended to check out existing code from SVN and work on it, not publishing an existing Git tree into a Subversion repository.

A first rather naive approach didn't work out (as somewhat expected), but then I figured out how to achieve this anyway.]]></description>
			<content:encoded><![CDATA[<p>Recently I worked on some new project, and as always I created a local <a href="http://git-scm.com">Git</a> repository as a start. After working on it several days, creating lots of commits, I had to publish it into the central <a href="http://subversion.tigris.org">Subversion</a> repository (which is one of the <a href="http://en.wikipedia.org/wiki/Version_control_system">VCS</a>s we got). I could have done this by creating a new folder in SVN and add the latest version of all files of the project to it, but that way all history would be gone, which I didn&#8217;t like.</p>
<p>Git has a feature to work with SVN repositories, <em>git-svn</em>, but that&#8217;s intended to check out existing code from SVN and work on it, not publishing an existing Git tree into a Subversion repository.</p>
<p>A first rather naive approach didn&#8217;t work out (as somewhat expected), but then I figured out how to achieve this anyway.<br />
<span id="more-125"></span></p>
<p>As a test, let&#8217;s first create an empty SVN repository and a Git repository with some commits:</p>
<pre>$ svnadmin create repo
$ svn co file:///Users/nicolas/Temp/git_to_svn/repo svn_repo
Checked out revision 0.
$ cd svn_repo
$ svn mkdir trunk tags branches
A         trunk
A         tags
A         branches
$ svn commit -m "Create repository structure"
Adding         branches
Adding         tags
Adding         trunk

Committed revision 1.
$ cd ..

$ mkdir project; cd project
$ git init
Initialized empty Git repository in /Users/nicolas/Temp/git_to_svn/project/.git/
$ echo "foo" > test.txt; git add test.txt; git commit -m "Initial version"
master (root-commit) 88464cf] Initial version
 1 files changed, 1 insertions(+), 0 deletions(-)
 create mode 100644 test.txt
$ echo "bar" > test.txt; git commit test.txt -m "Second version"
master cb62866] Second version
 1 files changed, 1 insertions(+), 1 deletions(-)
</pre>
<p>We now can set up <em>git-svn</em>:</p>
<pre>$ git svn init -s file:///Users/nicolas/Temp/git_to_svn/repo/
$ git svn fetch
r1 = 741ab63aea786882eafd38dc74369e651f554c9c (trunk)
</pre>
<p>Depending on the layout of your SVN project, you might need to drop the <em>-s</em> parameter and add <em>-t</em>, <em>-T</em> or <em>-b</em> flags, see the <a href="http://www.kernel.org/pub/software/scm/git/docs/git-svn.html"><em>git-svn</em> manpage</a>.</p>
<p>A little naive we could try to push everything to the SVN repository now:</p>
<pre>
$ git svn dcommit
Unable to determine upstream SVN information from HEAD history.
Perhaps the repository is empty. at /opt/local/libexec/git-core/git-svn line 439.
</pre>
<p>This fails since the git svn command can&#8217;t figure out which commits to push: there&#8217;s no link between our original Git repository and the Subversion heads.</p>
<p>To fix this, we can use a Git <em>graft</em> to link them. We&#8217;ll tell Git the commit which created the SVN folder in which we want to store the project is the parent commit of the first commit in our Git repository:</p>
<pre>
$ git show-ref trunk
741ab63aea786882eafd38dc74369e651f554c9c refs/remotes/trunk
$ git log --pretty=oneline master | tail -n1
88464cfdf549a82b30ee7c52e53e2b310f0d9ec4 Initial version
$ echo "88464cfdf549a82b30ee7c52e53e2b310f0d9ec4 741ab63aea786882eafd38dc74369e651f554c9c" >> .git/info/grafts
</pre>
<p>If now you execute <em>git log</em>, you&#8217;ll see the &#8220;Create repository structure&#8221; SVN commit is displayed after our &#8220;Initial version&#8221; commit.</p>
<p>Pushing to SVN now works fine:</p>
<pre>
$ git svn dcommit
Committing to file:///Users/nicolas/Temp/git_to_svn/repo/trunk ...
	A	test.txt
Committed r2
	A	test.txt
r2 = 8c72757dd3a7d550ed8ef393bb74c0350d22dbac (trunk)
No changes between current HEAD and refs/remotes/trunk
Resetting to the latest refs/remotes/trunk
test.txt: locally modified
	M	test.txt
Committed r3
	M	test.txt
r3 = ca0fc06d477bcd4dd5c6f6d2ae6d94356b510280 (trunk)
No changes between current HEAD and refs/remotes/trunk
Resetting to the latest refs/remotes/trunk
</pre>
<p>All set <img src='http://eikke.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://eikke.com/importing-a-git-tree-into-a-subversion-repository/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>Re: Python recursion performance test</title>
		<link>http://eikke.com/re-python-recursion-performance-test/</link>
		<comments>http://eikke.com/re-python-recursion-performance-test/#comments</comments>
		<pubDate>Thu, 16 Jul 2009 01:00:57 +0000</pubDate>
		<dc:creator>Nicolas</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://eikke.com/?p=113</guid>
		<description><![CDATA[(This is a reply on a post by Ahmed Soliman on recursion performance in (C)Python, and CPython function call overhead in general. I started to write this as a comment on his post, but it turned out much longer, so sending it over here in the end.) Hey, As discussed before, this is not a [...]]]></description>
			<content:encoded><![CDATA[<p>(This is a reply on a <a href="http://www.ahmedsoliman.com/2009/07/15/python-recursion-performance-test/" title="Ahmed Soliman: Python recursion performance test">post</a> by <a href="http://www.ahmedsoliman.com" title="Ahmed Soliman">Ahmed Soliman</a> on recursion performance in (C)Python, and CPython function call overhead in general. I started to write this as a comment on his post, but it turned out much longer, so sending it over here in the end.)</p>
<p>Hey,</p>
<p>As discussed before, this is not a fair comparison, since the non-recursive version is much &#8216;smarter&#8217; than the recursive one: it calculates values and will never recalculates them, whilst the recursive version calculates everything over and over again.</p>
<p>Adding some simple memoization helps a lot. First, my testing code:<br />
<span id="more-113"></span><br />
<script src="http://gist.github.com/148082.js"></script></p>
<p>Here are the benchmarks on my MacBook Pro Intel Core2Duo 2.33GHz with 3GB RAM (running quite a lot of applications). Do note the &#8216;dumb&#8217; version calculates <em>fib(35)</em>, whilst the slightly optimized versions, which still use recursion but much less recursive calls (as they should) or your second version calculate <em>fib(150)</em>.</p>
<p>Using MacOS X 10.5.6 stock CPython 2.5.1:</p>
<pre>
MacBook:Projects nicolas $ python -V
Python 2.5.1

MacBook:Projects nicolas $ python fib.py 35 150
fib(35) = 9227465
Calculation took 12.8542108536 seconds

Calculating the amount of recursive calls to calculate fib(35)
Calculating fib(35) = 9227465 took 29860703 calls

fib2(150) = 9969216677189303386214405760200
Calculation took 0.00020694732666 seconds

memoize_dict(fib)(150) = 9969216677189303386214405760200
Calculation took 0.00141310691833 seconds

memoize_constant_list(151, fib)(150) = 9969216677189303386214405760200
Calculation took 0.000310182571411 seconds
</pre>
<p>Overall it looks like <em>fib2</em> and <em>memoize_constant_list</em> perform fairly similar, I guess function call overhead and <em>list.append</em> have a similar influence on performance in this case.</p>
<p>Using Jython 2.5.0 from the binary distribution on the Java HotSpot 64bit Server VM as shipped for OS X 10.5.6:</p>
<pre>
MacBook:Projects nicolas $ ./Jython/jython2.5.0/jython -V
Jython 2.5.0

MacBook:Projects nicolas $ ./Jython/jython2.5.0/jython fib.py 35 150
fib(35) = 9227465
Calculation took 12.5539999008 seconds

Calculating the amount of recursive calls to calculate fib(35)
Calculating fib(35) = 9227465 took 29860703 calls

fib2(150) = 9969216677189303386214405760200
Calculation took 0.0519998073578 seconds

memoize_dict(fib)(150) = 9969216677189303386214405760200
Calculation took 0.00399994850159 seconds

memoize_constant_list(151, fib)(150) = 9969216677189303386214405760200
Calculation took 0.00300002098083 seconds
</pre>
<p>The &#8216;dumb&#8217; <em>fib</em> implementation performs similar in both CPython and Jython. Jython performs significantly less good on the other implementations though, but maybe todays <a href="http://journal.thobe.org/2009/07/improving-performance-in-jython.html">news</a> could help here, not sure how much locking on <em>dict</em> and <em>list</em> access Jython introduces.</p>
<p>Finally, using <a href="http://code.google.com/p/unladen-swallow/">Unladen Swallow</a> 2009Q2, self-compiled from SVN on the same system, using standard settings:</p>
<pre>
MacBook:Projects nicolas $ ./unladen-swallow/unladen-2009Q2-inst/bin/python -V
Python 2.6.1

MacBook:Projects nicolas $ ./unladen-swallow/unladen-2009Q2-inst/bin/python fib.py 35 150
fib(35) = 9227465
Calculation took 12.2675719261 seconds

Calculating the amount of recursive calls to calculate fib(35)
Calculating fib(35) = 9227465 took 29860703 calls

fib2(150) = 9969216677189303386214405760200
Calculation took 0.000118970870972 seconds

memoize_dict(fib)(150) = 9969216677189303386214405760200
Calculation took 0.000972986221313 seconds

memoize_constant_list(151, fib)(150) = 9969216677189303386214405760200
Calculation took 0.00036096572876 seconds
</pre>
<p>which is similar to, slighly better or slightly worse than the CPython run, and when enforcing JIT (which introduces a significant startup time, which is not measured here):</p>
<pre>
MacBook:Projects nicolas $ ./unladen-swallow/unladen-2009Q2-inst/bin/python -j always fib.py 35 150
fib(35) = 9227465
Calculation took 14.6129109859 seconds

Calculating the amount of recursive calls to calculate fib(35)
Calculating fib(35) = 9227465 took 29860703 calls

fib2(150) = 9969216677189303386214405760200
Calculation took 0.0432291030884 seconds

memoize_dict(fib)(150) = 9969216677189303386214405760200
Calculation took 0.0363459587097 seconds

memoize_constant_list(151, fib)(150) = 9969216677189303386214405760200
Calculation took 0.0335609912872 seconds
</pre>
<p>which, to my surprise, performs pretty worse than the default settings.</p>
<p>Overall: your first implementation performs tons and tons of function calls, whilst the second one, which resembles <em>memoize_list_fib</em> in my code (which is recursive), performs significantly less function calls and in the end <em>memoize_list_fib</em> performs almost as good as your second version (it performs +- the same number of function calls as the number of times you&#8217;re going through your loop).</p>
<p>So whilst I do agree function calls in Python are reasonably slow compared to plain C function calls (which is just a jmp, no frame handling etc. etc. required), your comparison between your recursive and non-recursive implementation is completely unfair, and even if calculating <em>fib(35)</em> takes several seconds, consider you&#8217;re doing a pretty impressive 29860703 function calls to perform the calculation.</p>
<p>Time to get some sleep.</p>
]]></content:encoded>
			<wfw:commentRss>http://eikke.com/re-python-recursion-performance-test/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>First Clojure experiments</title>
		<link>http://eikke.com/first-clojure-experiments/</link>
		<comments>http://eikke.com/first-clojure-experiments/#comments</comments>
		<pubDate>Sun, 12 Jul 2009 00:28:25 +0000</pubDate>
		<dc:creator>Nicolas</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[clojure]]></category>
		<category><![CDATA[jvm]]></category>

		<guid isPermaLink="false">http://eikke.com/?p=109</guid>
		<description><![CDATA[Some weeks ago I attended JavaOne (a pretty neat conference, even for non-Java-heads like me) and got in touch with several non-Java languages running on the JVM (nothing really new next to Project Fortress, but I never got into most for real). Since I wanted to learn some language not resembling any other I already [...]]]></description>
			<content:encoded><![CDATA[<p>Some weeks ago I attended <a href="http://java.sun.com/javaone/" title="JavaOne">JavaOne</a> (a pretty neat conference, even for non-Java-heads like me) and got in touch with several non-Java languages running on the <a href="http://en.wikipedia.org/wiki/Java_Virtual_Machine" title="Wikipedia - Java Virtual Machine"><abbr title="Java Virtual Machine">JVM</abbr></a> (nothing really new next to <a href="http://projectfortress.sun.com/Projects/Community" title="Project Fortress">Project Fortress</a>, but I never got into most for real).</p>
<p>Since I wanted to learn some language not resembling any other I already know (even a little), I decided some hours ago to start digging into Clojure, which is a <a href="http://en.wikipedia.org/wiki/LISP" title="Wikipedia - LISP"><abbr title="LISt Processing">LISP</abbr></a> dialect running on the JVM using <a href="http://en.wikipedia.org/wiki/Software_transactional_memory" title="Wikipedia - Software transactional memory"><abbr title="Software Transactional Memory">STM</abbr></a> (Software Transactional Memory) and created with concurrency in mind. Check the <a href="http://clojure.org/" title="Clojure">website</a> for more information.<br />
<span id="more-109"></span></p>
<p>After some hacking I got a first &#8216;application&#8217; running. Since recently there&#8217;s been some little meme at work regarding echo servers, I decided to write a very basic line-oriented echo server in Clojure.</p>
<p>The result is a server using one thread per connection which just sends back lines to a connected client as-is. Nothing fancy, but might be a useful start for developing basic network applications using Clojure.</p>
<p>Enjoy!</p>
<p><script src="http://gist.github.com/145449.js"></script></p>
]]></content:encoded>
			<wfw:commentRss>http://eikke.com/first-clojure-experiments/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Python value swap</title>
		<link>http://eikke.com/python-value-swap/</link>
		<comments>http://eikke.com/python-value-swap/#comments</comments>
		<pubDate>Wed, 22 Apr 2009 18:44:55 +0000</pubDate>
		<dc:creator>Nicolas</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://eikke.com/?p=103</guid>
		<description><![CDATA[Been looking (again) at XMPP recently. While browsing through existing source code and samples in several languages, there&#8217;s one pattern which comes back quite frequently in &#8216;echobot&#8217; demos: when a message comes in, the to and from attributes are swapped, and the message is sent. The most common approach is something like (pseudocode): temp = [...]]]></description>
			<content:encoded><![CDATA[<p>Been looking (again) at XMPP recently. While browsing through existing source code and samples in several languages, there&#8217;s one pattern which comes back quite frequently in &#8216;echobot&#8217; demos: when a message comes in, the to and from attributes are swapped, and the message is sent.</p>
<p>The most common approach is something like (pseudocode):<br />
<code>
<pre>temp = from
from = to
to = temp</pre>
<p></code></p>
<p>In Python there&#8217;s an easier approach though which seems to be unknown to several developers. It uses the multi-assignment/expansion syntax:<br />
<code>
<pre>from, to = to, from</pre>
<p></code></p>
<p>Basically, the tuple on the right (to, from) is constructed, then expanded to locals &#8216;from&#8217; and &#8216;to&#8217;.</p>
<p>Just a hint <img src='http://eikke.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' />  It&#8217;s a pretty elegant line of code IMHO.</p>
]]></content:encoded>
			<wfw:commentRss>http://eikke.com/python-value-swap/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Erlang, Python and Twisted mashup using TwOTP</title>
		<link>http://eikke.com/erlang-python-and-twisted-mashup-using-twotp/</link>
		<comments>http://eikke.com/erlang-python-and-twisted-mashup-using-twotp/#comments</comments>
		<pubDate>Sun, 19 Apr 2009 19:03:30 +0000</pubDate>
		<dc:creator>Nicolas</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Technology]]></category>
		<category><![CDATA[erlang]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[twisted]]></category>
		<category><![CDATA[twotp]]></category>

		<guid isPermaLink="false">http://eikke.com/?p=90</guid>
		<description><![CDATA[Recently, I&#8217;ve been toying around with Erlang again. After creating some simple apps I wanted to integrate some Erlang code inside a Python application (since that&#8217;s still my favorite day-to-day language, it&#8217;s used at work and I&#8217;m sort-of convinced Erlang would be a good choice for several of the applications we need to develop, integrated [...]]]></description>
			<content:encoded><![CDATA[<p>Recently, I&#8217;ve been toying around with <a href="http://erlang.org">Erlang</a> again. After creating some simple apps I wanted to integrate some Erlang code inside a <a href="http://www.python.org">Python</a> application (since that&#8217;s still my favorite day-to-day language, it&#8217;s used at work and I&#8217;m sort-of convinced Erlang would be a good choice for several of the applications we need to develop, integrated with our existing Python code). The most obvious solution would be to use an Erlang port, but this is IMHO rather cumbersome: it requires a developer to define a messaging format, parsing code for incoming messages, etc. There&#8217;s a <a href="http://www.kazmier.net/computer/port-howto/">tutorial</a> available if you want to take this route.</p>
<p>A more elegant solution is creating a node using Python, similar to <a href="http://erlang.org/doc/apps/jinterface/index.html">JInterface</a> and equivalents. Luckily there&#8217;s an existing project working on a library to create Erlang nodes using Python and <a href="http://www.twistedmatrix.com">Twisted</a>: <a href="https://code.launchpad.net/twotp">TwOTP</a>.</p>
<p>One downside: it&#8217;s rather underdocumented&#8230; So here&#8217;s a very quick demo how to call functions on an Erlang node from within a Twisted application.</p>
<p>First of all we&#8217;ll create 2 Erlang functions: one which returns a simple &#8220;Hello&#8221; message, one which uses an extra process to return &#8216;pong&#8217; messages on calls to &#8216;ping&#8217;, and counts those.</p>
<p>The code:</p>
<p><code>
<pre>-module(demo).
-export([hello/1, ping/0, start/0]).

hello(Name) ->
    Message = "Hello, " ++ Name,
    io:format(Message ++ "~n", []),
    Message.

ping_loop(N) ->
    receive
        {get_id, From} ->
            From ! {pong, N},
            ping_loop(N + 1)
    end.

ping() ->
    pingsrv ! {get_id, self()},
    receive
        {pong, N} -&gt; ok
    end,
    {pong, N}.

start() ->
    Pid = spawn_link(fun() -> ping_loop(1) end),
    register(pingsrv, Pid).
</pre>
<p></code></p>
<p>This should be straight-forward if you&#8217;re familiar with Erlang (which I assume).</p>
<p>The Python code is not that hard to get either: it follows the basic Twisted pattern. First one should create a connection to EPMD, the Erlang Port Mapper Daemon (used to find other nodes), then a connection to the server node should be created, and finally functions can be called (calls happen the same way as Erlang&#8217;s <a href="http://erlang.org/doc/man/rpc.html">RPC module</a>).</p>
<p>Here&#8217;s the code. I&#8217;d advise to read it bottom-to-top:</p>
<p><code>
<pre>import sys

from twisted.internet import reactor
import twotp

def error(e):
    '''A generic error handler'''
    print 'Error:'
    print e
    reactor.stop()

def do_pingpong(proto):
    def handle_pong(result):
        # Parse the result
        # 'ping' returns a tuple of an atom ('pong') and an integer (the pong
        # id)
        # In TwOTP, an Atom object has a 'text' attribute, which is the string
        # form of the atom
        text, id_ = result[0].text, result[1]
        print 'Got ping result: %s %d' % (text, id_)
        # Recurse
        reactor.callLater(1, do_pingpong, proto)

    # Call the 'ping' function of the 'demo' module
    d = proto.factory.callRemote(proto, 'demo', 'ping')
    # Add an RPC call handler
    d.addCallback(handle_pong)
    # And our generic error handler
    d.addErrback(error)

def call_hello(proto, name):
    def handle_hello(result):
        print 'Got hello result:', result
        # Erlang strings are lists of numbers
        # The default encoding is Latin1, this might need to be changed if your
        # Erlang node uses another encoding
        text = ''.join(chr(c) for c in result).decode('latin1')
        print 'String form:', text
        # Start pingpong loop
        do_pingpong(proto)

    # Call the 'hello' function of the 'demo' module, and pass in argument
    # 'name'
    d = proto.factory.callRemote(proto, 'demo', 'hello', name)
    # Add a callback for this function call
    d.addCallback(handle_hello)
    # And our generic error handler
    d.addErrback(error)

def launch(epmd, remote, name):
    '''Entry point of our demo application'''
    # Connect to a node. This returns a deferred
    d = epmd.connectToNode(remote)
    # Add a callback, called when the connection to the node is established
    d.addCallback(call_hello, name)
    # And add our generic error handler
    d.addErrback(error)

def main():
    remote = sys.argv[1]
    name = sys.argv[2]
    # Read out the Erlang cookie value
    cookie = twotp.readCookie()
    # Create a name for this node
    this_node = twotp.buildNodeName('demo_client')
    # Connect to EPMD
    epmd = twotp.OneShotPortMapperFactory(this_node, cookie)
    # Call our entry point function when the Twisted reactor is started
    reactor.callWhenRunning(launch, epmd, remote, name)
    # Start the reactor
    reactor.run()

if __name__ == '__main__':
    main()</pre>
<p></code></p>
<p>Finally, to run it, you should first start a server node, and run the &#8216;pingsrv&#8217; process:</p>
<p><code>
<pre>MacBook:pyping nicolas$ erl -sname test@localhost
Erlang (BEAM) emulator version 5.6.5 [source] [async-threads:0] [hipe] [kernel-poll:false]

Eshell V5.6.5  (abort with ^G)
(test@localhost)1> c(demo).
{ok,demo}
(test@localhost)2> demo:start().
true</pre>
<p></code></p>
<p>Notice we started <em>erl</em> providing <em>test@localhost</em> as short node name.</p>
<p>Now we can launch our client:</p>
<p><code>
<pre>(pythonenv)MacBook:pyping nicolas$ python hello.py 'test' Nicolas
Got hello result: [72, 101, 108, 108, 111, 44, 32, 78, 105, 99, 111, 108, 97, 115]
String form: Hello, Nicolas
Got ping result: pong 1
Got ping result: pong 2
Got ping result: pong 3</pre>
<p></code></p>
<p>&#8216;test&#8217; is the shortname of the server node.</p>
<p>You can stop the ping loop using <em>CTRL-C</em>. If you restart the client afterwards, you can see the ping IDs were retained:</p>
<p><code>
<pre>(pythonenv)MacBook:pyping nicolas$ python hello.py 'test' Nicolas
Got hello result: [72, 101, 108, 108, 111, 44, 32, 78, 105, 99, 111, 108, 97, 115]
String form: Hello, Nicolas
Got ping result: pong 4
Got ping result: pong 5</pre>
<p></code></p>
<p>That&#8217;s about it. Using TwOTP you can also develop a node which exposes functions, which can be called from an Erlang node using <em>rpc:call/4</em>. Check the documentation provided with TwOTP for a basic example of this feature.</p>
<p>Combining Erlang applications as distributed, fault tolerant core infrastructure and Python/Twisted applications for &#8216;everyday coding&#8217; can be an interesting match in several setups, an TwOTP provides all required functionalities to integrate the 2 platforms easily.</p>
]]></content:encoded>
			<wfw:commentRss>http://eikke.com/erlang-python-and-twisted-mashup-using-twotp/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Got to love the open source communities&#8230;</title>
		<link>http://eikke.com/got-to-love-the-open-source-communities/</link>
		<comments>http://eikke.com/got-to-love-the-open-source-communities/#comments</comments>
		<pubDate>Sun, 02 Nov 2008 14:16:39 +0000</pubDate>
		<dc:creator>Nicolas</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[open source]]></category>

		<guid isPermaLink="false">http://eikke.com/got-to-love-the-open-source-communities/</guid>
		<description><![CDATA[Hanging around in #coreboot@irc.freenode.net recently, this just passed by: 15:28  &#60;eikke&#62; is it possible in linux to access partitions in an losetup&#8217;ed raw image? 15:29  &#60;carldani&#62;  eikke: yes, I wrote a kernel patch for that 15:29  &#60;eikke&#62; ah, patch :p i did it using &#8211;offset now, but thats kinda hackish 15:29  &#60;carldani&#62;  eikke: the easiest [...]]]></description>
			<content:encoded><![CDATA[<p>Hanging around in #coreboot@irc.freenode.net recently, this just passed by:</p>
<p>15:28  &lt;eikke&gt; is it possible in linux to access partitions in an losetup&#8217;ed raw image?<br />
15:29  &lt;carldani&gt;  eikke: yes, I wrote a kernel patch for that<br />
15:29  &lt;eikke&gt; ah, patch :p i did it using &#8211;offset now, but thats kinda hackish<br />
15:29  &lt;carldani&gt;  eikke: the easiest way is to use kpartx<br />
15:29  &lt;carldani&gt;  eikke: that was impossible back then<br />
15:30  &lt;eikke&gt; i see<br />
15:30  &lt;carldani&gt;  eikke: kpartx -a /dev/loop0<br />
15:30  &lt;carldani&gt;  eikke: that will give you /dev/mapper/loop0p1 /dev/mapper/loop0p5 etc<br />
15:30  &lt;eikke&gt; cool<br />
15:30  &lt;carldani&gt;  eikke: are you the eikke responsible for ivman?<br />
15:31  &lt;eikke&gt; a long time ago, yes<br />
15:31  &lt;eikke&gt; dont start the flames please :p<br />
15:31  &lt;carldani&gt;  eikke: thanks! I&#8217;m still using it on my laptop</p>
<p>A helps out B, B helps out A, A writes something useful for B, B wrote something used by A, etc. Got to love the open source movement.</p>
]]></content:encoded>
			<wfw:commentRss>http://eikke.com/got-to-love-the-open-source-communities/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Python gotcha</title>
		<link>http://eikke.com/python-gotcha/</link>
		<comments>http://eikke.com/python-gotcha/#comments</comments>
		<pubDate>Fri, 26 Sep 2008 19:35:37 +0000</pubDate>
		<dc:creator>Nicolas</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://eikke.com/python-gotcha/</guid>
		<description><![CDATA[Don&#8217;t ever do this unless it&#8217;s really what you want: import os def some_func(fd):   f = os.fdopen(fd, 'w')   f.write('abc') fd = get_some_fd() some_func(fd) some_other_func(fd) Here&#8217;s what goes wrong: when some_func comes to an end, f (which is a file-like objects) goes out of scope, is destructed, which causes fd to be closed. I [...]]]></description>
			<content:encoded><![CDATA[<p>Don&#8217;t ever do this unless it&#8217;s really what you want:</p>
<pre>import os

def some_func(fd):
    f = os.fdopen(fd, 'w')
    f.write('abc')

fd = get_some_fd()
some_func(fd)
some_other_func(fd)</pre>
<p>Here&#8217;s what goes wrong: when some_func comes to an end, f (which is a file-like objects) goes out of scope, is destructed, which causes fd to be closed. I think this is pretty weird behavior (an object closing an fd it didn&#8217;t open itself), but well.</p>
<p>Here&#8217;s a better version, for reference:</p>
<pre>def some_func(fd):
    f = os.fdopen(os.dup(fd), 'w')
    #Use f here</pre>
<p>Try this on fd 0/1/2 in an (I)Python shell <img src='http://eikke.com/wp-includes/images/smilies/icon_wink.gif' alt=';-)' class='wp-smiley' /> </p>
]]></content:encoded>
			<wfw:commentRss>http://eikke.com/python-gotcha/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
	</channel>
</rss>

