<?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>Thought Palace &#187; Languages</title>
	<atom:link href="http://jens.mooseyard.com/category/languages/feed/" rel="self" type="application/rss+xml" />
	<link>http://jens.mooseyard.com</link>
	<description>Little boxes made of words, by Jens Alfke</description>
	<lastBuildDate>Thu, 19 Jan 2012 18:29:28 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>py2rb: A Python-to-Ruby Porting Assistant</title>
		<link>http://jens.mooseyard.com/2010/05/py2rb-a-python-to-ruby-porting-assistant/</link>
		<comments>http://jens.mooseyard.com/2010/05/py2rb-a-python-to-ruby-porting-assistant/#comments</comments>
		<pubDate>Sun, 02 May 2010 04:26:12 +0000</pubDate>
		<dc:creator>jens</dc:creator>
				<category><![CDATA[Computers]]></category>
		<category><![CDATA[Languages]]></category>

		<guid isPermaLink="false">http://jens.mooseyard.com/?p=398</guid>
		<description><![CDATA[I've never figured out whether I prefer Python or Ruby, so I've written things in both languages. Sometimes I start in one and decide I'd rather use the other. Unfortunately, converting code is painful, even though both have fairly similar syntax. For instance, converting to Ruby means inserting zillions of "end" statements!

Having a need to do this recently, I lazily looked around for a script that would do the grunt-work of Python-to-Ruby translation. I couldn't find one, so I ended up writing one myself. And I've uploaded it for the benefit of others who might have the same need, and who might even improve it.]]></description>
			<content:encoded><![CDATA[	<p>I&#8217;ve never figured out whether I prefer Python or Ruby, so I&#8217;ve written things in both languages. Sometimes I start in one, then change my mind and decide I&#8217;d rather use the other. Unfortunately, changing over is painful, even though both have fairly similar syntax. For instance, converting to Ruby means inserting zillions of &#8220;end&#8221; statements!</p>

	<p>Having a need to do this recently, I lazily looked around for a script that would do the grunt-work of Python-to-Ruby translation. I couldn&#8217;t find one, so I ended up writing one myself. And I&#8217;ve uploaded it for the benefit of others who might have the same need, and who might even improve it.</p>

	<p>So here it is: <a href="http://bitbucket.org/snej/py2rb/src/tip/py2.rb" title=""><strong>py2.rb</strong></a>. Use it wisely. In particular, pay attention to the caveat found in the file&#8217;s header comments:</p>

	<blockquote>This script just does the obvious, easy transformations, giving you more time to work on the harder ones :) It is <span class="caps">NOT</span> a real parser, just a bunch of kludgy regex operations, so it can&#8217;t do anything fancy. It may get some things wrong, and won&#8217;t even attempt some other things that it&#8217;s very likely to get wrong. The output will definitely have to be hand edited by someone familiar with both languages, before it can be expected to compile as Ruby, much less run correctly. The goal is simply to require <em>less</em> hand editing, and less mechanical replacing, than you would have had to do without this script.</blockquote>

	<p>It&#8217;s in the public domain. &#8220;Do what thou wilt&#8221; shall be the whole of the law.</p>

	<p><strong>Note:</strong> Please don&#8217;t comment with Python advocacy or, worse, anti-Ruby flames. That&#8217;s not the point. I like Python fine; I just prefer to remain promiscuous. And I think it&#8217;s ultimately in everyone&#8217;s interest for ideas to be able to flow from one form of expression to another.</p>
 ]]></content:encoded>
			<wfw:commentRss>http://jens.mooseyard.com/2010/05/py2rb-a-python-to-ruby-porting-assistant/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Lazyweb: Is there a Ruby template engine like Genshi or Kid?</title>
		<link>http://jens.mooseyard.com/2008/12/lazyweb-is-there-a-ruby-template-engine-like-genshi-or-kid/</link>
		<comments>http://jens.mooseyard.com/2008/12/lazyweb-is-there-a-ruby-template-engine-like-genshi-or-kid/#comments</comments>
		<pubDate>Sat, 20 Dec 2008 18:44:10 +0000</pubDate>
		<dc:creator>jens</dc:creator>
				<category><![CDATA[Computers]]></category>
		<category><![CDATA[Languages]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://mooseyard.com/Jens/2008/12/lazyweb-is-there-a-ruby-template-engine-like-genshi-or-kid/</guid>
		<description><![CDATA[Ruby has a wide variety of HTML/XML templating engines, but none of the ones I&#8217;ve found work the way I&#8217;d like. It&#8217;s quite possible I&#8217;ve overlooked some, though. My current gold standard for templaters is the Python library Genshi (which was inspired by Kid)&#8212;what I like about these is that your templates are valid XML: [...]]]></description>
			<content:encoded><![CDATA[	<p>Ruby has a wide variety of <span class="caps">HTML</span>/XML templating engines, but none of the ones I&#8217;ve found work the way I&#8217;d like. It&#8217;s quite possible I&#8217;ve overlooked some, though.</p>

	<p>My current gold standard for templaters is the Python library <a href="http://genshi.edgewall.org/" title="">Genshi</a> (which was inspired by <a href="http://kid-templating.org/" title="">Kid</a>)&#8212;what I like about these is that <em>your templates are valid <span class="caps">XML</span>:</em> the parameters and control structures are expressed as special <span class="caps">XML</span> attributes and tags. This makes it easy to edit your templates in syntax-checking <span class="caps">XML</span> editors, and guarantees that your app serves valid <span class="caps">XML</span> or <span class="caps">HTML</span>.</p>

	<p>But as I said, none of the Ruby template engines I&#8217;ve seen work this way. They&#8217;re all either generic macro systems that intermix markup and Ruby (like <span class="caps">ERB</span>), or they&#8217;re ruby APIs that output markup (like Builder, <span class="caps">HAML</span> and Markaby). I don&#8217;t really like either approach.</p>

	<p>If you&#8217;ve got a good suggestion, I&#8217;ve got a gold star ready to stick on your forehead!</p>
 ]]></content:encoded>
			<wfw:commentRss>http://jens.mooseyard.com/2008/12/lazyweb-is-there-a-ruby-template-engine-like-genshi-or-kid/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>James Bennett: “Let&#8217;s talk about Python 3.0”</title>
		<link>http://jens.mooseyard.com/2008/12/james-bennett-%e2%80%9clets-talk-about-python-30%e2%80%9d/</link>
		<comments>http://jens.mooseyard.com/2008/12/james-bennett-%e2%80%9clets-talk-about-python-30%e2%80%9d/#comments</comments>
		<pubDate>Sat, 06 Dec 2008 19:51:20 +0000</pubDate>
		<dc:creator>jens</dc:creator>
				<category><![CDATA[Languages]]></category>

		<guid isPermaLink="false">http://mooseyard.com/Jens/2008/12/james-bennett-%e2%80%9clets-talk-about-python-30%e2%80%9d/</guid>
		<description><![CDATA[James Bennett has posted a very informative defense of Python 3, in reaction to my more negative article. I learned things from it, as from many of the comments on my post. I even agree with most of what he says, but it hasn&#8217;t really changed my opinion, because it seems like we&#8217;re not talking [...]]]></description>
			<content:encoded><![CDATA[	<p>James Bennett has posted <a href="http://www.b-list.org/weblog/2008/dec/05/python-3000/" title="">a very informative defense of Python 3</a>, in reaction to <a href="http://mooseyard.com/Jens/2008/12/python-30-whats-the-point/" title="">my more negative article</a>.</p>

	<p>I learned things from it, as from many of the comments on my post. I even agree with most of what he says, but it hasn&#8217;t really changed my opinion, because it seems like we&#8217;re not talking about the same things.</p>

	<p>From the standpoint of <em>writing Python code</em>, Python 3 sounds great. It&#8217;s so nice not to have to deal with crufty APIs that don&#8217;t make sense anymore, or half-baked mechanisms like the original string class.</p>

	<p>But from the standpoint of <em>using Python libraries</em>, I still think this is terrible. Any time I want to use a library I&#8217;ll have to check whether it works with the version of Python I&#8217;m on. I may find that a library I use, and need updates to, will no longer support Python 2.x going forward. And the porting and branch-maintenance process will definitely take time out of library developers&#8217; schedules, slowing down other work.</p>

	<blockquote>(Since James had several great quotes in his article, here&#8217;s one making my point: In Elbonia, cars have always driven on the left side of the road. Finally, the government decides that it&#8217;s time to harmonize this with the country&#8217;s neighbors, and announces that Elbonia will change to the more standard right-side driving. But they&#8217;re going to phase it in: next Monday, trucks will switch to driving on the right, and the Monday after that, the cars will switch. <em>Rim-shot!</em> The weakness of this analogy is that the sensible choice for Elbonia&#8212;making everyone switch at once&#8212;doesn&#8217;t exist for something as distributed and anarchic as a programming-language community.)</blockquote>

	<p>For me, the second issue predominates. I&#8217;m more of a library-user than a hardcore Python coder. In fact, on balance, I like the core Python language <em>less</em> than Ruby. What won me over were the libraries: there are a lot of really well-designed, well-documented, highly modular libraries out there that I&#8217;ve really enjoyed using. (<a href="http://genshi.edgewall.org/" title="">Genshi</a>, to name just one, is the best templating engine I&#8217;ve ever used.)</p>

	<p>So my perspective of the Python 3 release is that it heralds a difficult period, where library development and maintenance may slow, and grazing the Python ecosystem will become more difficult. Hopefully this won&#8217;t last too long. But the worst-case scenario is that this forking process may discourage enough software developers to reduce the popularity of Python in the long term. That&#8217;s bad not because popularity is important in itself, but because a vibrant community is important for maintaining forward progress, and because popularity brings useful things like books and tools.</p>
 ]]></content:encoded>
			<wfw:commentRss>http://jens.mooseyard.com/2008/12/james-bennett-%e2%80%9clets-talk-about-python-30%e2%80%9d/feed/</wfw:commentRss>
		<slash:comments>14</slash:comments>
		</item>
		<item>
		<title>Python 3.0: What&#8217;s The Point?</title>
		<link>http://jens.mooseyard.com/2008/12/python-30-whats-the-point/</link>
		<comments>http://jens.mooseyard.com/2008/12/python-30-whats-the-point/#comments</comments>
		<pubDate>Thu, 04 Dec 2008 18:10:34 +0000</pubDate>
		<dc:creator>jens</dc:creator>
				<category><![CDATA[Languages]]></category>

		<guid isPermaLink="false">http://mooseyard.com/Jens/2008/12/python-30-whats-the-point/</guid>
		<description><![CDATA[Python 3.0 is finally out. I like Python (though it and Ruby are always competing for my affections), and I&#8217;m always a sucker for new features in any language, but I&#8217;m having trouble getting excited about this. Despite the longtime code-name &#8220;Python 3000&#8221;, it doesn&#8217;t seem very futuristic; and it introduces a lot of compatibility [...]]]></description>
			<content:encoded><![CDATA[	<p><a href="http://www.python.org/download/releases/3.0/" title="">Python 3.0 is finally out</a>. I like Python (though it and Ruby are always competing for my affections), and I&#8217;m always a sucker for new features in any language, but I&#8217;m having trouble getting excited about this. Despite the longtime code-name &#8220;Python 3000&#8221;, it doesn&#8217;t seem very futuristic; and it introduces a lot of compatibility problems. In fact, after reading the docs, I can&#8217;t come up with any good reasons to install or use the new version. Am I missing something?</p>

	<p>Looking at the official <a href="http://docs.python.org/dev/3.0/whatsnew/3.0.html" title="">What&#8217;s New page</a>, my takeaway is that the benefits are:</p>

	<ul>
		<li>A bunch of minor language features cleaned up</li>
		<li>Better (at least less confusing) Unicode support</li>
		<li>Some nice but minor new syntactic features</li>
	</ul>

	<p>while the drawbacks are:</p>

	<ul>
		<li>Won&#8217;t run any pre-existing Python software. Just one minor example: the xrange() function no longer exists; instead, range() behaves the way xrange did. I&#8217;m pretty sure I&#8217;ve written something like &#8220;for i in xrange(a,b)&#8221; in every significant Python program I&#8217;ve ever written, causing all of them to break in 3.0 until I update them. Which is trivial; but there are dozens and dozens of changes like this. And many of them won&#8217;t cause easy-to-diagnose exceptions like the removal of xrange; some just cause subtle changes in behavior that will likely require debugging if they&#8217;re overlooked. (There is apparently an automated code converter, but of course it won&#8217;t fix everything.)</li>
	</ul>

	<ul>
		<li>As a side-effect of the above: All existing Python books are now out-of-date. (Sure, much of each book&#8217;s coverage is still accurate, but the reader who&#8217;s not a Python 3 expert already won&#8217;t know which parts are OK and which are now wrong.)</li>
	</ul>

	<ul>
		<li>Programs written for Python 3.0 probably won&#8217;t run in any earlier version of the interpreter, since most of the changes aren&#8217;t backward-compatible.</li>
	</ul>

	<ul>
		<li><em>Slower</em> performance: &#8220;Python 3.0 runs the pystone benchmark around 10% slower than Python 2.5.&#8221;</li>
	</ul>

	<p><em>Something seems wrong here!</em> the benefits are minor, the drawbacks are huge. Where&#8217;s the incentive to use it?</p>

	<p>Compare this to the Ruby 1.9 story: A number of language improvements; they&#8217;re mostly backward-compatible, although with enough exceptions to break lots of large apps like Rails). But oh yeah: <i><a href="http://antoniocangiano.com/2007/12/03/the-great-ruby-shootout/" title="">it&#8217;s three times as fast</a>. </i> That&#8217;s a very big deal, considering the poor performance of today&#8217;s Ruby (and Python). Once the 1.9 tech appears in a stable release, I am definitely upgrading.</p>

	<p>Or compare to an operating system upgrade. How would you respond to a pitch like: &#8220;The &#8216;Meerkat&#8217; release is going to be incredible! We&#8217;ve made a number of improvements to the system libraries. Of course, all existing apps will break, so you&#8217;ll have to get upgrades from the developers (which won&#8217;t run on earlier OS&#8217;s, by the way.) And afterwards you&#8217;ll notice everything running 10% slower! But we&#8217;re sure you&#8217;re going to love the enhancements to iterators and Unicode support&#8230;&#8221;</p>

	<p>So what did I miss? I don&#8217;t mean this as an attack; I&#8217;m just genuinely confused&#8230;</p>

	<p><strong>Update:</strong> See also <a href="http://mooseyard.com/Jens/2008/12/james-bennett-&#8220;lets-talk-about-python-30&#8221;">my further thoughts</a> in response to James Bennett&#8217;s article.</p>
 ]]></content:encoded>
			<wfw:commentRss>http://jens.mooseyard.com/2008/12/python-30-whats-the-point/feed/</wfw:commentRss>
		<slash:comments>90</slash:comments>
		</item>
		<item>
		<title>Blocks/Closures For C!</title>
		<link>http://jens.mooseyard.com/2008/08/blocksclosures-for-c/</link>
		<comments>http://jens.mooseyard.com/2008/08/blocksclosures-for-c/#comments</comments>
		<pubDate>Sat, 30 Aug 2008 14:59:33 +0000</pubDate>
		<dc:creator>jens</dc:creator>
				<category><![CDATA[Computers]]></category>
		<category><![CDATA[Languages]]></category>

		<guid isPermaLink="false">http://mooseyard.com/Jens/2008/08/blocksclosures-for-c/</guid>
		<description><![CDATA[Chris Lattner, of Apple&#8217;s compiler team, writes : Until there is more real documentation, this is a basic idea of Blocks: it is closures for C. It lets you pass around units of computation that can be executed later. For example: void call_a_block(void (^blockptr)(int)) { blockptr(4); } void test() { int X = ... call_a_block(^(int [...]]]></description>
			<content:encoded><![CDATA[	<p><a href="http://lists.cs.uiuc.edu/pipermail/cfe-dev/2008-August/002670.html" title="">Chris Lattner, of Apple&#8217;s compiler team, writes</a> :</p>

	<p><blockquote>Until there is more real documentation, this is a basic idea of Blocks: it is <a href="http://en.wikipedia.org/wiki/Closure_(computer_science)">closures</a> for C.  It lets you pass around units of computation that can be executed later.  For example:</p>

<pre>void call_a_block(void (^blockptr)(int)) {
blockptr(4);
}

void test() {
int X = ...
call_a_block(^(int y){ print(X+y); });  // references stack var
snapshot

call_a_block(^(int y){ print(y*y); });
}</pre>

	<p>In this example, when the first block is formed, it snapshots the value of X into the block and builds a small structure on the stack. Passing the block pointer down to call_a_block passes a pointer to this stack object.  Invoking a block (with function call syntax) loads the relevant info out of the struct and calls it.  call_a_block can obviously be passed different blocks as long as they have the same type.</blockquote></p>

	<p>This is very exciting: it&#8217;s the kind of new abstraction the C family has needed for years. As you know if you&#8217;ve worked in Ruby or Python or Smalltalk or any functional language, the ability to declare an anonymous function inline, and pass it as a parameter to another function, opens the door to creating new and useful control structures. Blocks are to control structures as <tt>struct</tt> is to data structures.</p>

	<p>For example, one of the things that blew my mind about Smalltalk-80 when I first learned it was how the basic conditional and loop operations were not hardwired into the language; instead, they were just methods of built-in classes. For example:</p>

<pre>root := y&gt;=0 ifTrue: [y sqrt] ifFalse: [-1]</pre>

	<p>(Apologies if I&#8217;ve messed up the syntax; it&#8217;s been a long time.) Yes, this is an if-then-else expression. But literally, it&#8217;s the message <tt>ifTrue:ifFalse:</tt> being sent to the (boolean) result of the expression <tt>y>0</tt>, with its parameters being two blocks. And what happens at runtime is that the class <tt>True</tt> implements <tt>ifTrue:ifFalse:</tt> by evaluating the <em>first</em> block parameter and returning its value, while the class <tt>False</tt> implements it by evaluating the <em>second</em> block.</p>

	<p>This is, not surprisingly, a bit too expensive a way to implement the ubiquitous if-then-else, so in practice the compiler optimizes this expression into hardwired bytecodes instead. But it demonstrates that you can define comparable control structures yourself for your own purposes &#8230; such as for <span class="caps">URL</span> routing in a web application, which many Ruby and Python frameworks allow to be customized with blocks.</p>

	<p>I think blocks will also make it possible to implement better concurrency patterns like Actors in a clean way. You may recall that I was investigating these, but after a few days I gave up because the necessary stack munging confused the Obj-C runtime too much. Once blocks are available (and hopefully integrated into Objective-C and Foundation) I&#8217;ll have to give it another try.</p>
 ]]></content:encoded>
			<wfw:commentRss>http://jens.mooseyard.com/2008/08/blocksclosures-for-c/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>JavaScript 2.0, aka ECMAScript 4</title>
		<link>http://jens.mooseyard.com/2008/06/javascript-20-aka-ecmascript-4/</link>
		<comments>http://jens.mooseyard.com/2008/06/javascript-20-aka-ecmascript-4/#comments</comments>
		<pubDate>Wed, 18 Jun 2008 01:09:30 +0000</pubDate>
		<dc:creator>jens</dc:creator>
				<category><![CDATA[Computers]]></category>
		<category><![CDATA[Languages]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://mooseyard.com/Jens/2008/06/javascript-20-aka-ecmascript-4/</guid>
		<description><![CDATA[I just discovered that there is an ECMAScript 4&#8212;better known as JavaScript 2.0&#8212;in development. As a shameless language slut, I immediately dove into the language overview, and it&#8217;s pretty neat. This turns JavaScript into a much more useable language, that I could see doing serious development in. I&#8217;m well aware that today&#8217;s JS isn&#8217;t a [...]]]></description>
			<content:encoded><![CDATA[	<p>I just discovered that there is an <a href="http://www.ecmascript.org/" title=""><span class="caps">ECMA</span>Script 4</a>&#8212;better known as JavaScript 2.0&#8212;in development. As a shameless language slut, I immediately dove into the <a href="http://www.ecmascript.org/es4/spec/overview.pdf" title="">language overview</a>, and it&#8217;s pretty neat.</p>

	<p>This turns JavaScript into a much more useable language, that I could see doing serious development in. I&#8217;m well aware that today&#8217;s JS isn&#8217;t a &#8220;toy&#8221;, that it&#8217;s got an interesting prototype-based object model under the hood; but I&#8217;ve found the language so loose as to be difficult to use. This new version adds a full class model, much better support for iteration, a form of generators/coroutines, and more.</p>

	<p>Intriguingly, it also offers type-checking, which has been absent from most mainstream dynamic languages. I know it&#8217;s trendy to use &#8220;duck typing&#8221;, but I find that languages with no compile-time type-checking frustrate me because I make so damn many mistakes that aren&#8217;t caught until runtime; which means things I could have fixed in two minutes in C++ or Objective-C take half an hour to fix as I run the program over, and over, and over, discovering and fixing one problem at a time.</p>

	<p>Given the concurrent <a href="http://webkit.org/blog/189/announcing-squirrelfish/" title="">developments in Apple&#8217;s JavaScript interpreter</a>, I&#8217;m getting pretty excited for JavaScript. I hope that in addition to making it blindingly fast, Apple will also add <span class="caps">ECMA</span>Script 4 support, and make it easy to access the language outside of a WebView.</p>
 ]]></content:encoded>
			<wfw:commentRss>http://jens.mooseyard.com/2008/06/javascript-20-aka-ecmascript-4/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Coroutines in Objective-C</title>
		<link>http://jens.mooseyard.com/2008/04/coroutines-in-objective-c/</link>
		<comments>http://jens.mooseyard.com/2008/04/coroutines-in-objective-c/#comments</comments>
		<pubDate>Wed, 30 Apr 2008 22:03:14 +0000</pubDate>
		<dc:creator>jens</dc:creator>
				<category><![CDATA[Computers]]></category>
		<category><![CDATA[Languages]]></category>

		<guid isPermaLink="false">http://mooseyard.com/Jens/2008/04/coroutines-in-objective-c/</guid>
		<description><![CDATA[I&#8217;ve started using NSOperation in a few places in Cloudy, which means I&#8217;m backsliding into using threads and locking and so forth. It definitely makes writing network code easier than Cocoa&#8217;s asynchronous API, but I really don&#8217;t want to get into a morass of threads. What I&#8217;d really like to use are Actors. In a [...]]]></description>
			<content:encoded><![CDATA[	<p>I&#8217;ve started using NSOperation in a few places in Cloudy, which means I&#8217;m backsliding into using threads and locking and so forth. It definitely makes writing network code easier than Cocoa&#8217;s asynchronous <span class="caps">API</span>, but I really don&#8217;t want to get into a morass of threads.</p>

	<p>What I&#8217;d really like to use are <a href="http://revactor.org/philosophy" title=""><strong>Actors</strong></a>. In a nutshell, an Actor is an object that has its own [cooperative] thread and message queue. Actors interact by message-passing instead of shared state. The idea is to eliminate the need for standard synchronization primitives like semaphors and locks, and get rid of the race conditions and deadlocks that plague multi-threaded programs.</p>

	<p>The page I linked to above is from <a href="http://revactor.org/" title="">Revactor</a>, a new Actor implementation for Ruby 1.9; Actors are also built into languages like Erlang and Io.</p>

	<p>The only hard part about implementing Actors in Obj-C appears to be the underlying dependency on <a href="http://en.wikipedia.org/wiki/Coroutine" title=""><strong>coroutines</strong></a>. Steve Dekorte [author of Io] has a <a href="http://www.dekorte.com/projects/opensource/libCoroutine/" title="">C coroutine implementation</a> ; when I discovered that last year, I then found an <a href="http://www.dpompa.com/?p=83" title="">Objective-C wrapper for it</a>, but that relies on a HigherOrderMessaging library which is incompatible with <span class="caps">OS X 10</span>.5 and hasn&#8217;t yet been updated.</p>

	<h3>So I&#8217;ve gone <span class="caps">DIY</span>.</h3>

	<p>I started with Steve Dekorte&#8217;s coroutine library, but unfortunately it doesn&#8217;t build on 10.5 &#8230; and when I got it to build, I then ran into a bug in the system header <ucontext.h> &#8230; and by the time I&#8217;d worked around <em>that</em>, I knew the code well enough that I pretty much rewrote it. Doing this made it much shorter and clearer, since Steve&#8217;s code is cross-platform and supports three different implementations, while all I care about is <span class="caps">OS X 10</span>.5. I got it down to 150 lines of C.</p>

	<p>Then I wrote a pretty simple Objective-C wrapper around it, under 200 lines. (<a href="http://mooseyard.com/hg/hgwebdir.cgi/Actors/file/tip/Coroutines/MYCoroutine.h" title="">Here&#8217;s the header</a>.)</p>

	<h3>Come &#8216;n&#8217; get it&#8230;</h3>

	<p>The work in progress is <a href="http://mooseyard.com/hg/hgwebdir.cgi/Actors/" title="">in my Mercurial repository</a> if you want to check it out. It&#8217;s grandly titled &#8220;Actors&#8221;, but all that&#8217;s in it so far is the coroutine implementation.</p>

	<p>Needless to say, this is currently <strong>for amusement purposes only</strong>; do not use it in real code. In particular, I strongly suspect that there are going to be problems getting Objective-C exceptions and autorelease pools to work correctly in coroutines. I&#8217;ll try to explore that next.</p>
 ]]></content:encoded>
			<wfw:commentRss>http://jens.mooseyard.com/2008/04/coroutines-in-objective-c/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>MacRuby</title>
		<link>http://jens.mooseyard.com/2008/03/macruby/</link>
		<comments>http://jens.mooseyard.com/2008/03/macruby/#comments</comments>
		<pubDate>Sat, 01 Mar 2008 19:43:38 +0000</pubDate>
		<dc:creator>jens</dc:creator>
				<category><![CDATA[Computers]]></category>
		<category><![CDATA[Languages]]></category>

		<guid isPermaLink="false">http://mooseyard.com/Jens/2008/03/macruby/</guid>
		<description><![CDATA[MacRuby &#8220;is a version of Ruby that runs on top of Objective-C. More precisely, MacRuby is currently a port of the Ruby 1.9 implementation for the Objective-C runtime and garbage collector. The rationale behind this effort is to solve in a very efficient way all the bridging problems RubyCocoa, the Ruby bridge to the Objective-C [...]]]></description>
			<content:encoded><![CDATA[	<p><a href="http://trac.macosforge.org/projects/ruby/wiki/MacRuby" title="">MacRuby</a> &#8220;is a version of Ruby that runs on top of Objective-C. More precisely, MacRuby is currently a port of the Ruby 1.9 implementation for the Objective-C runtime and garbage collector. The rationale behind this effort is to solve in a very efficient way all the bridging problems RubyCocoa, the Ruby bridge to the Objective-C runtime, has to work around.&#8221; (It&#8217;s still in development, and not ready for prime-time use yet.)</p>

	<p>This is exciting news. Now, Mac <span class="caps">OS X</span> already has excellent Ruby support, with <a href="http://developer.apple.com/tools/developonrailsleopard.html">a complete Ruby 1.8 and Rails stack</a> (including extras like Mongrel and Capistrano) included in the box. There&#8217;s also a glue library called RubyCocoa that bridges between the Ruby and Objective-C languages, making it easy to write all or part of a Cocoa app in Ruby (or to call useful Obj-C frameworks from a Ruby app.)</p>

	<p>So why MacRuby? The big deal is <em>performance</em>. By re-implementing the Ruby object model on top of the Objective-C runtime &#8212; so all Ruby objects are bona fide Objective-C objects and vice versa &#8212; it will eliminate <a href="http://trac.macosforge.org/projects/ruby/wiki/WhyMacRuby" title="">serious amounts of overhead</a>:</p>

	<ul>
		<li>Parameter marshalling whenever a Ruby method has to call into Objective-C or vice versa</li>
		<li>Bookkeeping data structures that map between Ruby and Objective-C objects and classes</li>
		<li>Incompatible threading models</li>
		<li>Multiple garbage collectors</li>
	</ul>

	<p>It also takes advantage of the major speed boost of the new bytecode interpreter in Ruby 1.9.</p>

	<p>From my experiences working on Apple&#8217;s Java runtime, I know that bridging to and from native code is a major performance issue. The amount of overhead for a single inter-language call may not seem like much (a few microseconds), but it&#8217;s one of those &#8220;death by a thousand cuts&#8221; effects, because in an application that uses native libraries it happens so often.</p>

	<p>(For example, one of the ways I was able to speed up <span class="caps">AWT</span> graphics performance by about 5x in <span class="caps">MRJ 2</span>.1 was to pipeline the calls, so instead of each java.awt.Graphics method calling directly into the native QuickDraw <span class="caps">API</span>, it just appended the parameters to a display-list array, which periodically got flushed through a single call to a native C++ method that walked the list and made all the QuickDraw calls. I didn&#8217;t initially believe this would be a big win, but Steve Zellers wrote a prototype version that proved it.)</p>

Another nice feature of MacRuby is that it slightly extends Ruby&#8217;s method-call syntax to allow Objective-C style interleaved keywords and arguments. So when calling an Objective-C -setFirstName:lastName: method, instead of writing
<pre>    person.setFirstName_lastName(first,last)</pre>
you write
<pre>    person.setFirstName first, lastName: last</pre>
or alternatively
<pre>    person.setFirstName first, :lastName =&gt; last</pre>
This may offend Ruby purists, and it&#8217;s really just syntactic sugar, but by eliminating <em>conceptual</em> bridging overhead, I think it&#8217;ll improve <em>programmer</em> performance, which is also an important optimization.

	<p>Finally, another good thing that I didn&#8217;t realize right away is that it&#8217;s an official Apple open-source project. So not only does that make it very likely that a finished version of MacRuby will be built into 10.6; it also means that, if it&#8217;s necessary to tweak the Objective-C runtime to make MacRuby work better, that&#8217;s likely to happen too. The people working on runtimes and scripting languages at Apple are <em>really smart</em>, and I&#8217;m glad to see they&#8217;re not resting on their laurels after all they achieved in 10.5.</p>
 ]]></content:encoded>
			<wfw:commentRss>http://jens.mooseyard.com/2008/03/macruby/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Network Barbie Says &#8220;Asynchrony Is Hard!&#8221;</title>
		<link>http://jens.mooseyard.com/2008/02/network-barbie-says-asynchrony-is-hard/</link>
		<comments>http://jens.mooseyard.com/2008/02/network-barbie-says-asynchrony-is-hard/#comments</comments>
		<pubDate>Wed, 06 Feb 2008 17:54:35 +0000</pubDate>
		<dc:creator>jens</dc:creator>
				<category><![CDATA[Computers]]></category>
		<category><![CDATA[Ideas]]></category>
		<category><![CDATA[Languages]]></category>

		<guid isPermaLink="false">http://mooseyard.com/Jens/2008/02/network-barbie-says-asynchrony-is-hard/</guid>
		<description><![CDATA[You can&#8217;t avoid asynchrony when writing network code, since operations can take an arbitrary amount of time, and often do. To keep the app responsive it has to be able to get other things done while a slow operation is in progress. My first exposure to network programming was in Java, whose approach to asynchrony [...]]]></description>
			<content:encoded><![CDATA[	<p>You can&#8217;t avoid asynchrony when writing network code, since operations can take an arbitrary amount of time, and often do. To keep the app responsive it has to be able to get other things done while a slow operation is in progress.</p>

	<p>My first exposure to network programming was in Java, whose approach to asynchrony is to use threads. Lots of &#8216;em. The <span class="caps">API</span> calls are [almost] all blocking, so you run them on background threads. This is good because it makes the way the <span class="caps">API</span> works more intuitive: you call a method, and it returns a value when it finishes. This makes your own code more intuitive, as it just performs the operations in order, like: open connection, send request, read response, parse response, close connection, return.</p>

	<p>The downside is that making heavily threaded code work correctly can be very hard, and the problems are subtle, hard to understand and debug, and sometimes almost impossible to reproduce. Edward Lee&#8217;s paper <a href="http://www.artist-embedded.org/artist/The-Trouble-With-Threads.html" title="">The Trouble With Threads</a> describes a complex Java server that was excruciatingly well-designed, code-reviewed and tested.</p>

	<blockquote>&#8220;No problems were observed until the code deadlocked in April 2004, four years later. Our relatively rigorous software engineering practice had identiﬁed and ﬁxed many concurrency bugs. But that a problem as serious as a deadlock could go undetected for four years despite this practice is alarming.&#8221;</blockquote>

	<p>My teammates and I had similar experiences while implementing the Java <span class="caps">AWT</span> libraries on Mac <span class="caps">OS 9</span> and 10.0. We kept running into weird concurrency bugs, fixing them (which often meant restructuring code and making it more complex), and then running into more. By the time we were done, threading didn&#8217;t seem like such a good idea anymore.</p>

	<p>Cocoa (and the underlying CFNetwork framework) instead make the asynchrony explicit, by using an event- and callback-based model. When you invoke a network operation, the call returns immediately; but you set up a delegate object or callback function that will later be invoked when the operation completes. A fancy event loop called a &#8220;runloop&#8221; at the top level takes care of dispatching events in order. For the most part, you can write your code using only a single thread and still let your app perform tasks in parallel without blocking the UI.</p>

	<p>The drawback is that your code gets more complex, since the high-level control flow no longer matches the semantics of the programming language. A multi-step operation now involves a separate function to implement each step, and any state has to be stored externally in instance variables. Explicit state machines often pop up. There&#8217;s a lot of boilerplate for setting delegates, adding and removing observers, parsing notification dictionaries, and so forth, which obscures the actual logic of the code.</p>

	<p>As you can guess, I&#8217;m knee-deep in this sort of thing right now. Just like many times before, I&#8217;m wishing there were a third way. I&#8217;d like to be able to keep each operation&#8217;s flow of control simple, as with threads, but at the same time limit the interactions between operations to keep the <em>overall</em> flow of control from turning into race-condition spaghetti.</p>

	<p>Oh, and I want to keep writing in Objective-C. There are intriguing multiprocessing techniques in some higher-level languages, like <a href="http://lua.org's" title="">Lua</a> coroutines and <a href="http://iolanguage.com's" title="">Io</a> actors, but I don&#8217;t think I&#8217;m ready to start writing the core of my app in one of them. I wonder if there&#8217;s a way to implement some of those techniques cleanly in Objective-C.</p>

	<p>I&#8217;m not offering any solutions, just describing the problem I see. Maybe I&#8217;ll get one of those great &#8220;What, you&#8217;re not using <span class="caps">XYZ</span>? It does exactly what you want&#8221; comments! Or maybe not. Anyway, here are some things I&#8217;ve run across that offer useful techniques:</p>

	<ul>
		<li><a href="http://www.dpompa.com/HigherOrderMessaging" title="">Higher-Order Messaging</a> framework&#8212;adds some higher-level functional primitives for Cocoa programming, that makes delegation and callbacks simpler to use. ( <strong>Update 3PM:</strong> Appears to be incompatible with 10.5. It compiles, with warnings, but the self-tests quickly fail, with exceptions raised by NSInvocation. Dang!)</li>
	</ul>

	<ul>
		<li><a href="http://www.dekorte.com/projects/opensource/libcoroutine/" title="">libcoroutine</a>, a cross-platform implementation of <a href="http://en.wikipedia.org/wiki/Coroutine" title="">coroutines</a> (similar to <a href="http://en.wikipedia.org/wiki/Cooperative_multitasking" title="">cooperative threads</a>) in C. (There are several of these; this is a newish one by Steve Dekorte which, unlike others I&#8217;ve tried, works correctly on Mac <span class="caps">OS X</span>.)</li>
	</ul>

	<ul>
		<li>Actors, as found in the <a href="http://www.iolanguage.com/scm/git/checkout/Io/docs/IoGuide.html#Concurrency-Actors" title="">Io language</a> and others. An actor is basically an object that runs a private event loop and thread: you communicate with it by sending it messages. This helps encapsulate state, since the object itself is only doing one thing at a time, and its internal state is hidden from other threads.</li>
	</ul>

	<ul>
		<li><strong>Update:</strong> I forgot to list NSOperation and NSOperationQueue, new classes in <span class="caps">OS X 10</span>.5. These aren&#8217;t &#8220;magic&#8221; in the way the above are, they&#8217;re just a little framework for managing simultaneous tasks, but they&#8217;re useful. I&#8217;m just getting started with NSOperation: it&#8217;s what I&#8217;m using for now to manage the nasty asynchronous bits.</li>
	</ul>

	<p>Got any more useful links or ideas? Post them, and I&#8217;ll add &#8216;em here.</p>
 ]]></content:encoded>
			<wfw:commentRss>http://jens.mooseyard.com/2008/02/network-barbie-says-asynchrony-is-hard/feed/</wfw:commentRss>
		<slash:comments>4</slash:comments>
		</item>
		<item>
		<title>96 Characters Ought To Be Enough For Anyone</title>
		<link>http://jens.mooseyard.com/2008/01/96-characters-ought-to-be-enough-for-anyone/</link>
		<comments>http://jens.mooseyard.com/2008/01/96-characters-ought-to-be-enough-for-anyone/#comments</comments>
		<pubDate>Wed, 30 Jan 2008 18:01:48 +0000</pubDate>
		<dc:creator>jens</dc:creator>
				<category><![CDATA[Computers]]></category>
		<category><![CDATA[Languages]]></category>
		<category><![CDATA[Web]]></category>

		<guid isPermaLink="false">http://mooseyard.com/Jens/2008/01/96-characters-ought-to-be-enough-for-anyone/</guid>
		<description><![CDATA[Famous Hacker Paul Graham on his new LISP dialect, Arc: &#8220;Arc only supports Ascii. MzScheme, which the current version of Arc compiles to, has some more advanced plan for dealing with characters. But it would probably have taken me a couple days to figure out how to interact with it, and I don&#8217;t want to [...]]]></description>
			<content:encoded><![CDATA[	<p>Famous Hacker Paul Graham <a href="http://paulgraham.com/arc0.html" title="">on his new <span class="caps">LISP</span> dialect, Arc</a>:</p>

	<blockquote>&#8220;Arc only supports Ascii. MzScheme, which the current version of Arc compiles to, has some more advanced plan for dealing with characters. But it would probably have taken me a couple days to figure out how to interact with it, and I don&#8217;t want to spend even one day dealing with character sets. Character sets are a black hole. I realize that supporting only Ascii is uninternational to a point that&#8217;s almost offensive [...] <em>But the kind of people who would be offended by that wouldn&#8217;t like Arc anyway.</em>&#8221;</blockquote>

	<p>That last bit [emphasis mine] sort of flummoxed me. Is he saying that <span class="caps">LISP</span> only appeals to native English speakers?[1] Or that no one in their right mind would use <span class="caps">LISP</span> to write software for end-users?[2] Or maybe that internationalization is just some sort of abstract feel-good political-correctness issue, since none of those third-worlders even have computers anyway?[3]</p>

	<p>He makes similarly eye-opening assertions about <span class="caps">HTML</span>, too. Arc has <span class="caps">HTML</span>-generating libraries, but they &#8220;just do everything with tables&#8221; instead of <span class="caps">CSS</span>. Why? Because apparently <span class="caps">CSS</span>-based Web designs are <em>less agile</em> than ones made out of tables. Somehow I don&#8217;t think most people who&#8217;ve done web design both ways would agree&#8212;those old-school layouts made with infinitely-nested tables were about as agile as a house of cards.</p>

	<p>Anyway. Normally I&#8217;m a big slut for new programming languages, but it would probably take me a couple days to figure out Arc, and I don&#8217;t want to spend even one day on Yet Another <span class="caps">LISP </span>Variant that won&#8217;t even let me write code I could use in the real world.<br />
<hr /><p>Wait&#8212;let me not end on such a snarky note. Since I&#8217;m such a big smarty, what would I have done differently? I would simply have made the language&#8217;s &#8220;character&#8221; data type 16 bits wide instead of 8, and provided four trivial library routines to convert such strings to and from <span class="caps">UTF</span>-8 and CP-1252 encodings for I/O purposes. That&#8217;s about an hour&#8217;s work, and all you need for really basic Unicode support; once you have that, you can add further Unicode niceties (and there are admittedly a zillion of &#8216;em) a few at a time without completely breaking old code.</p><br />
<hr /><p>[1] And only those native English speakers who don&#8217;t care about foo-foo details like &#8220;curly quotes&#8221;&#8212;or emdashes&#8212;or other Arcane Symbols&#8482;. Remember, to the true old-school hacker, even lowercase letters are an inessential frill.</p>

	<p>[2] Like, say, <a href="http://reddit.com" title="">Reddit.com</a>. From SecretGeek&#8217;s awesome <a href="http://www.secretgeek.net/lisp_truth.asp" title="">overview of <span class="caps">LISP</span></a>:</p>

	<blockquote>&#8220;Reddit is proof that lisp is really powerful. Paul Graham originally wrote reddit, in lisp, on the back of a napkin while he was waiting for a coffee. it was so powerful that it had to be rewritten in python just so that ordinary computers could understand it. Because it was written in lisp it was almost no effort to rewrite the entire thing, and the rewrite was completed in-between two processor cycles.&#8221;</blockquote>

	<p>[3] I&#8217;m reminded of a meeting between Apple and Sun (JavaSoft) back in 1996. I was there to discuss OpenDoc and JavaBeans, but each company also had text and <span class="caps">I18N</span> engineers there, who were talking about Unicode and text-layout technology for the upcoming Java2D graphics engine. There was an exchange that went something like this:</p>

	<p>Apple engineer: &#8230;and the layout needs to take into account ligatures and contextual forms, where adjacent letters change glyphs depending on neighboring characters, or even merge into a single glyph.</p>

	<p>Sun engineer: C&#8217;mon, is this important? How many people need advanced typographic features like that, anyway?</p>

	<p>Apple engineer: <em>[after a pause]</em> Well, there are over 900 million of them in <a href="http://en.wikipedia.org/wiki/Devanagari#Ligatures" title="">India</a> alone, and another 200 million or so in the <a href="http://en.wikipedia.org/wiki/Arabic_alphabet#Modified_letters.</p" title="">Arabic world</a>></p>
 ]]></content:encoded>
			<wfw:commentRss>http://jens.mooseyard.com/2008/01/96-characters-ought-to-be-enough-for-anyone/feed/</wfw:commentRss>
		<slash:comments>19</slash:comments>
		</item>
	</channel>
</rss>

