<?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>Etherplex &#187; admin</title>
	<atom:link href="http://etherplex.org/archives/author/admin/feed" rel="self" type="application/rss+xml" />
	<link>http://etherplex.org</link>
	<description>Rick Dillon&#039;s home on the net...</description>
	<lastBuildDate>Sat, 14 Aug 2010 07:37:35 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0.1</generator>
		<item>
		<title>Use of Mutable Data Structures in Clojure</title>
		<link>http://etherplex.org/archives/87</link>
		<comments>http://etherplex.org/archives/87#comments</comments>
		<pubDate>Sun, 08 Feb 2009 17:43:54 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://etherplex.org/?p=87</guid>
		<description><![CDATA[One of Clojure&#8217;s biggest strengths is that it is backed by the JVM, and has good interoperability with the Java libraries.  When I needed to implement a simulation with events that should be executed in order of their timestamp, I was immediately tempted to use Java&#8217;s PriorityBlockingQueue, since neither sorted-map nor sorted-set supported two events [...]]]></description>
			<content:encoded><![CDATA[<p>One of Clojure&#8217;s biggest strengths is that it is backed by the JVM, and has good interoperability with the Java libraries.  When I needed to implement a simulation with events that should be executed in order of their timestamp, I was immediately tempted to use Java&#8217;s <a href="http://java.sun.com/j2se/1.5.0/docs/api/java/util/concurrent/PriorityBlockingQueue.html" target="_blank"><span style="font-family: courier new,monospace;">PriorityBlockingQueue</span></a>, since neither <a href="http://clojure.org/data_structures#toc17" target="_blank"><span style="font-family: courier new,monospace;">sorted-map</span></a> nor <a href="http://clojure.org/data_structures#toc22" target="_blank"><span style="font-family: courier new,monospace;">sorted-set</span></a> supported two events with the same time stamp (though I could have used some magic to make it work, but decided simplicity was more important).</p>
<p>I was generating events with several types, and under certain circumstances, events that mapped <span style="font-family: courier new,monospace;">:type</span> to <span style="font-family: courier new,monospace;">:arrival</span> needed to be removed because they were no longer valid given the state of the simulation.  Easy enough:</p>
<p><span style="font-family: courier new,monospace;">(filter #(not= (:type %) :arrival) event-queue)</span></p>
<p>But, <span style="font-family: courier new,monospace;">PriorityBlockingQueue</span> is mutable (oh, and I&#8217;d put it in a <a href="http://clojure.org/refs" target="_blank"><span style="font-family: courier new,monospace;">ref</span></a>, to make things more interesting), so I&#8217;d need to clear it after I did the filter, before repopulating it with the filtered events.  This is a simpler version of the final code:<br />
<br style="font-family: courier new,monospace;" /><span style="font-family: courier new,monospace;">(let [elements (filter #(not= (:type %) :arrival) @event-queue)]</span><br style="font-family: courier new,monospace;" /><span style="font-family: courier new,monospace;"> (.clear @event-queue)</span><br style="font-family: courier new,monospace;" /> <span style="font-family: courier new,monospace;"> (.addAll @event-queue elements))</span></p>
<p>So, when I ran this, I kept getting <span style="font-family: courier new,monospace;">NullPointerException</span>.  Clojure is not well known for its uber-informative stack traces, so I flailed with this for a few hours, looking at it on and off while working on other parts of the code.  Then it hit me: <span style="font-family: courier new,monospace;">filter</span> is lazy, so the filtering of the elements is not happening until I&#8217;m calling in the <span style="font-family: courier new,monospace;">filter</span> when I make the <span style="font-family: courier new,monospace;">addAll</span> call.  By that time, <span style="font-family: courier new,monospace;">@event-queue</span> is empty, so the list of results is <span style="font-family: courier new,monospace;">nil</span>, which will throw a <span style="font-family: courier new,monospace;">NullPointerException</span> when passed to the <span style="font-family: courier new,monospace;">addAll</span> call.  The easy way to fix this is to wrap the call in a <span style="font-family: courier new,monospace;">doall</span> function, which forces the computation:</p>
<p><span style="font-family: courier new,monospace;">(doall (filter #(not= (:type %) :arrival) event-queue))</span></p>
<p>The moral?  Even though Clojure has good support of Java collections, be wary when you use mutable data structures with the standard Clojure <a href="http://clojure.org/sequences" target="_blank"><span style="font-family: courier new,monospace;">seq</span></a> functions; many of them are lazy, and unless you know they won&#8217;t change, you can get subtle temporal errors creeping into your code.  I got lucky that <span style="font-family: courier new,monospace;">addAll</span> threw the exception.  If it hadn&#8217;t my simulation would have ended and I would have spent a lot more time trying to figure out why.</p>
]]></content:encoded>
			<wfw:commentRss>http://etherplex.org/archives/87/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title></title>
		<link>http://etherplex.org/archives/45</link>
		<comments>http://etherplex.org/archives/45#comments</comments>
		<pubDate>Sun, 12 Oct 2008 00:09:05 +0000</pubDate>
		<dc:creator>admin</dc:creator>
				<category><![CDATA[MicroBlog]]></category>

		<guid isPermaLink="false">http://etherplex.org/?p=45</guid>
		<description><![CDATA[Discovered that WordPress has a great built-in feature that allows you to post entries by email. Setting it up was easy&#8230;and it may give me a way to post updates when I&#8217;m behind Nazi firewalls.]]></description>
			<content:encoded><![CDATA[<p>Discovered that WordPress has a great built-in feature that allows you to post entries by email.  Setting it up was easy&#8230;and it may give me a way to post updates when I&#8217;m behind Nazi firewalls.</p>
]]></content:encoded>
			<wfw:commentRss>http://etherplex.org/archives/45/feed</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
