<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"><channel><atom:link rel="hub" href="http://tumblr.superfeedr.com/" xmlns:atom="http://www.w3.org/2005/Atom"/><description>Devin Naquin
Recent Rice alum. Living and coding.</description><title>Post-Rice Haze</title><generator>Tumblr (3.0; @postricehaze)</generator><link>http://blog.naqu.in/</link><item><title>JavaScript Inheritance</title><description>&lt;p&gt;&lt;span&gt; &lt;/span&gt;JavaScript inheritance can be done dozens of different ways. One of the major plusses of the language is that it’s flexible enough to allow many possible paradigms. However, this also turns into a major language detractor, since everyone does it just a little differently. Here are a couple and some reasons I lean one way most of the time.&lt;/p&gt;
&lt;p&gt;&lt;span&gt; &lt;/span&gt;First, there’s a functional pattern. I actually like this a lot. Probably because I’ve written way too much Scheme in my time, but it’s elegant and, through functional scope, provides privacy otherwise lacking in JavaScript.&lt;/p&gt;
&lt;p&gt;&lt;span&gt; &lt;/span&gt;For example:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;var make_rectangle = function(length, width) {

	// length, width in private scope

	var area = function() {

		return length * width;

	};

	return {

		area: area

	};

};

var make_square = function(length) {

	return make_rectangle(length, length);

};

var r = make_rectangle(1, 2);

var s = make_square(2);

r.area();

s.area();&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;span&gt; &lt;/span&gt;This is sometimes called the Module pattern and sometimes uses constructors with the &lt;code&gt;new&lt;/code&gt; keyword, but that all amounts to the same thing: a function that generates an object.&lt;/p&gt;
&lt;p&gt;&lt;span&gt; &lt;/span&gt;The functional pattern is great, and I like it for lots of things but it has significant drawbacks. Privacy is rarely really needed and blocks lots of fun extensions once the object is created (e.g. &lt;code&gt;r.perimeter = function() { return 2*this.length + 2*this.width; };&lt;/code&gt;) The subtle drawback is the performance hit it entails, which I will come back to.&lt;/p&gt;
&lt;p&gt;&lt;span&gt; &lt;/span&gt;Then there’s a classical-styled inheritance pattern. Which has it’s drawbacks but has grown on me in recent months.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;function Rectangle(length, width) {

	this.length = length;

	this.width = width;

};

Rectangle.prototype.area = function () {

	return this.length * this.width;

};



function Square(length) {

	/* could use parent constructor here, but requires some more work. */

	this.length = this.width = length;

};

Square.prototype = new Rectangle();



var r = new Rectangle(1, 2);

var s = new Square(2);

r.area();

s.area();&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;span&gt; &lt;/span&gt;This is pretty straightforward and understandable for someone coming from the OO world of doing things. &lt;code&gt;Rectangle&lt;/code&gt; constructs an object that has a &lt;code&gt;length&lt;/code&gt; and a &lt;code&gt;width&lt;/code&gt;. The next line adds a function to it’s prototype. &lt;code&gt;Square&lt;/code&gt; constructs similarly. And sets it’s prototype to an instance of &lt;code&gt;Rectangle&lt;/code&gt; (it’s actual data doesn’t matter; so here, length and width are &lt;code&gt;undefined&lt;/code&gt;).&lt;/p&gt;
&lt;p&gt;&lt;span&gt; &lt;/span&gt;Now this approach has it’s drawbacks too. There’s no easy way to call a parent’s constructor. This can be buttered over with some convenience functions if necessary. And the &lt;code&gt;new&lt;/code&gt; keyword has it’s problems: without the &lt;code&gt;new&lt;/code&gt;, calls to &lt;code&gt;Rectangle&lt;/code&gt; and &lt;code&gt;Square&lt;/code&gt; have this bound to the global namespace which can cause all kinds of crazy problems. As Crockford says, “There is no compile warning, and there is no runtime warning.” This used to bother me, but I’ve learned to live with lint. A capitalized function must always get called with new, done and done. Besides, if you’re extra paranoid, there’s always:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;function Rectangle(length, width) {

	if (!(this instanceof Rectangle)) {

		return new Rectangle(length, width);

	}

	

	this.length = length;

	this.width = width;	

}&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;span&gt; &lt;/span&gt;But this also has it’s advantages. For example, not only can I now extend individual objects, but I can also add functionality to the prototype itself. For example:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;r.perimeter = function () { return 2*this.length + 2*this.width; };

r.perimeter();&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;span&gt; &lt;/span&gt;Or:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;Rectangle.prototype.perimeter = function () { return 2*this.length + 2*this.width; };

r.perimeter();

s.perimeter();&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;&lt;span&gt; Which otherwise, the privacy would block. &lt;/span&gt;The subtle advantage is that the classical actually performs much better than our first pattern. A quick profile shows:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;&gt;&gt;&gt; function exercise_functional() {

	for (var i=0; i&lt;100000; i++) {

		make_rectangle(1, 2).area();

	}

};

&gt;&gt;&gt; function exercise_classical() {

	for (var i=0; i&lt;100000; i++) {

		(new Rectangle(1, 2)).area();

	}

};&lt;/code&gt;&lt;/pre&gt;
&lt;pre&gt;function&lt;span&gt; &lt;/span&gt;calls&lt;span&gt; &lt;/span&gt;time&lt;span&gt; &lt;/span&gt;average

make_rectangle&lt;span&gt; &lt;/span&gt;100000&lt;span&gt; &lt;/span&gt;418.666ms&lt;span&gt; &lt;/span&gt;0.004ms

Rectangle&lt;span&gt; &lt;/span&gt;100000&lt;span&gt; &lt;/span&gt;95.361ms&lt;span&gt; &lt;/span&gt;0.001ms&lt;/pre&gt;
&lt;p&gt;&lt;span&gt; &lt;/span&gt;A pretty big difference and a huge factor for objects in tight loops.&lt;/p&gt;
&lt;p&gt;&lt;span&gt; &lt;/span&gt;The classical pattern is much faster, because it’s doing much less. It makes one &lt;code&gt;area&lt;/code&gt; function assigned to &lt;code&gt;Rectangle&lt;/code&gt;’s prototype and the language delegates all calls to it up to that instance with the correct data bound to &lt;code&gt;this&lt;/code&gt;. The functional pattern is making a new area function for every object. And assigning it to it’s own property. Not only wasting a bunch of time, but also memory.&lt;/p&gt;
&lt;p&gt;&lt;span&gt; &lt;/span&gt;JavaScript is fun. It lets you do all this any way you choose. Which is a blessing and a curse. My choice tends to change fairly frequently.&lt;/p&gt;</description><link>http://blog.naqu.in/post/440763525</link><guid>http://blog.naqu.in/post/440763525</guid><pubDate>Thu, 11 Mar 2010 01:16:00 -0600</pubDate></item><item><title>Shoes</title><description>&lt;p&gt;Nelly’s “Air Force Ones” is a classic. I remember riding around in the car in high school surfing radio stations in the attempt to always have “Air Force Ones” playing. It was ridiculous how long we could actually keep this up.&lt;/p&gt;
&lt;p&gt;
&lt;object width="425" height="344"&gt;
&lt;param name="movie" value="http://www.youtube.com/v/Due_rpoldM0&amp;hl=en_US&amp;fs=1&amp;"&gt;
&lt;param name="allowFullScreen" value="true"&gt;
&lt;param name="allowscriptaccess" value="always"&gt;
&lt;embed src="http://www.youtube.com/v/Due_rpoldM0&amp;hl=en_US&amp;fs=1&amp;" type="application/x-shockwave-flash" allowscriptaccess="always" allowfullscreen="true" width="425" height="344"&gt;&lt;/embed&gt;&lt;/object&gt;
&lt;/p&gt;
&lt;p&gt;Since “My Adidas” was released by Run DMC in 1986, shoes have been a classic trope in hip hop, but nothing takes it to the extreme like “Air Force Ones”. And it’s the extremes that make “Air Force Ones” great. The complete necessity of something as trivial as a shoe. In multiples. “I said give me two pairs,/I need two pairs,/So I can get to stomping in my Air Force Ones.”&lt;/p&gt;
&lt;p&gt;The song is just getting started when Murphy Lee comes in with a verse that’s only rhyme is the one word “man”.&lt;/p&gt;
&lt;blockquote&gt;Don’t get me wrong man, now Murphy Lee ain’t dumb man,&lt;br/&gt;‘Cause if the shoe is on the shelf, you should have some man&lt;br/&gt;You can not sit up and tell me that you have none man&lt;br/&gt;You may not have three or four but you got one man.&lt;/blockquote&gt;
&lt;p&gt;I got into a long argument once in college when someone mentioned they hated 50 Cent because he rhymed lines with the same word. Murphy Lee’s verse is a solid example of why I rarely take offense to this. Just look at the endings “dumb man”, “some man”, “none man”, “one man”. Enough said.&lt;/p&gt;
&lt;p&gt;Then Kyjuan comes in with a verse that mixes the shoes with the another hip-hop staple: the car. Another necessity that’s completely trivial.&lt;/p&gt;
&lt;blockquote&gt;O. K. ‘li, I treat my shoe like my ride,&lt;br/&gt;Chrome on the fat laces and put wood on the inside&lt;br/&gt;Spray candy on the swoosh with electric roof&lt;br/&gt;Since I put a kit on the sole, now got a wider shoe&lt;br/&gt;You see that low mid, skittle purple poof, I’m drivin them.&lt;br/&gt;(Kyjuan, where you gettin’ them colors, are you dying them?)&lt;br/&gt;…&lt;/blockquote&gt;
&lt;p&gt;Every time I hear this verse, I just picture it all literally. A tricked out high top complete with chrome, wood interior and candy paint. And I giggle. But they &lt;i&gt;are&lt;/i&gt; both just accessories.&lt;/p&gt;
&lt;p&gt;Finally, Nelly rounds it out with a verse about the white on whites. My favorites, though mine are scuffed.&lt;/p&gt;
&lt;blockquote&gt;Now don’t nothing get the hype on first site like white on whites&lt;br/&gt;Them three quarters, them lows; they all tight&lt;br/&gt;The only problem they only good for one night,&lt;br/&gt;‘Cause once you scuff them you fucked up your whole night.&lt;/blockquote&gt;
&lt;p&gt;Look at that first line. The whole verse, like most of the song’s verses, is centered around a single rhyme. But here, it’s the first line that makes it all work. “Now don’t nothing get the hype on first site like white on whites”. It’s overloaded “hype”, “first site”, “like”, “white on whites”. A completely excessive line that matches the theme of the song.&lt;/p&gt;
&lt;p&gt;I think I might need some new shoes.&lt;/p&gt;</description><link>http://blog.naqu.in/post/414928680</link><guid>http://blog.naqu.in/post/414928680</guid><pubDate>Sat, 27 Feb 2010 01:02:48 -0600</pubDate></item><item><title>Dead Simple Concurrency with Clojure</title><description>&lt;p&gt;Concurrency is hard. In an environment where processors don’t get faster, we just get more of them, concurrency is going to get more important. We’ll take these as givens.&lt;/p&gt;
&lt;p&gt;Enter Clojure. Promising a &lt;a href="http://clojure.org/concurrent_programming"&gt;vast array of interesting features promising to make concurrency easier.&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;So in my attempts to learn the language, my goal was to get something running concurrently as quickly as possible. I started with merge sort.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;(use 'clojure.contrib.seq-utils)

; merge side of the merge sort. naive tail recursive solution.
(defn ms-merge [left right]
  (loop [head [] l left r right]
    (if (empty? l) (concat head r)
      (if (empty? r) (concat head l)
        (if (&gt; (first l) (first r))
          (recur (conj head (first r)) l (rest r))
          (recur (conj head (first l)) (rest l) r))))))

; split side of the merge sort. naive recursive solution.
(defn naive-merge-sort [l]
  (if (&lt; (count l) 2) l
    (apply ms-merge (map naive-merge-sort (split-at (/ (count l) 2) l)))))
&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;This naive solution is slow and clearly doesn’t use both my cores.&lt;/p&gt;
&lt;p&gt;&lt;img alt="Merge Sort Single Threaded" src="http://dl.dropbox.com/u/1736/clojure/merge.single.png" height="80" width="256"/&gt;&lt;/p&gt;
&lt;p&gt;With a total running time of 21.98s to sort a shuffled list of 1M integers.&lt;/p&gt;
&lt;p&gt;From here, I split the process between my cores.&lt;/p&gt;
&lt;pre&gt;; wrapper around merge sort that just delegates both sides
; to threads running the naive solution.
(defn parallel-merge-sort [l]
  (apply ms-merge (pmap naive-merge-sort (split-at (/ (count l) 2) l))))
&lt;/pre&gt;
&lt;p&gt;The approach is just to split the input in half and sort both sides in different threads. This is trivial to do using pmap. The end job of merging these two together is easy enough and tail-recursive, so the downside of running the merge on just a single thread is less than you think (roughly 0.9% of total processing time).&lt;/p&gt;
&lt;p&gt;&lt;img alt="Merge Sort Dual Threaded" src="http://dl.dropbox.com/u/1736/clojure/merge.dual.png"/&gt;&lt;/p&gt;
&lt;p&gt;And with a total running time of 16.25s to sort a shuffled list of 1M integers.&lt;/p&gt;
&lt;p&gt;In this, I don’t use Clojure’s key win, STM, just the pmap syntactic sugar around managing multiple threads, so I’m only getting a little benefit from Clojure.&lt;/p&gt;
&lt;p&gt;Also, this only splits the task into two parts, therefore only making use of at most two cores. Not a completely portable solution.&lt;/p&gt;
&lt;p&gt;But the fact that my “hello world” in a new language uses both my cores this easily, is impressive.&lt;/p&gt;</description><link>http://blog.naqu.in/post/238607678</link><guid>http://blog.naqu.in/post/238607678</guid><pubDate>Mon, 09 Nov 2009 19:24:00 -0600</pubDate></item><item><title>Really Liking Ruby</title><description>&lt;p&gt;&lt;pre&gt;class Node

  def initialize(label, left, right)
    @label  = label
    @left   = left
    @right  = right
  end

  attr_accessor :label, :left, :right

  def breadth_first&lt;br/&gt;    unvisited = [self]

    while not unvisited.empty?
      node = unvisited.shift

      yield node

      unvisited &lt;&lt; node.left if node.left
      unvisited &lt;&lt; node.right if node.right
    end
  end

end
&lt;/pre&gt;&lt;/p&gt;</description><link>http://blog.naqu.in/post/200259788</link><guid>http://blog.naqu.in/post/200259788</guid><pubDate>Tue, 29 Sep 2009 13:37:22 -0500</pubDate></item><item><title>Migrated west until I hit an ocean. From the bayou to Houston to Austin. Skipped the desert and hit...</title><description>&lt;p&gt;Migrated west until I hit an ocean. From the bayou to Houston to Austin. Skipped the desert and hit a beach. Cold as all hell up the foothills. I-10 always the closest thing to a home I had. Until I ran out of mile markers and there’s only east to go.&lt;/p&gt;</description><link>http://blog.naqu.in/post/168334484</link><guid>http://blog.naqu.in/post/168334484</guid><pubDate>Fri, 21 Aug 2009 13:50:00 -0500</pubDate></item><item><title>"People. This is me again. I hate to cut things down like this. But, uh. There’s a crowd of..."</title><description>“People. This is me again. I hate to cut things down like this. But, uh. There’s a crowd of kids. And this is to whom I’m talking. Mostly to whom. Are you ready for that? Um. There’s kids like crowding around over the walls, and like, trying to break down doors and everything. Thinking the Beatles are here. And uh. And uh that last comment. Hahaha alright. Except that they’re trying to break things down, crawling over ceilings and walls, and like. Thinking the Beatles are here. And they’re not. You. Those of you. And uh, they can come in if they want to come in. The Beatles aren’t here. Yeah. There’s great things happening anyway. Yeah, except that, just don’t, you know. Don’t like, bring down the ceilings and walls and everything. And uh, carry on.”&lt;br/&gt;&lt;br/&gt; - &lt;em&gt;Bill Graham at Monterrey Pop during the Grateful Dead’s set.&lt;/em&gt;</description><link>http://blog.naqu.in/post/162357797</link><guid>http://blog.naqu.in/post/162357797</guid><pubDate>Thu, 13 Aug 2009 17:57:31 -0500</pubDate></item><item><title>Jurassic Park</title><description>&lt;p&gt;MALCOLM&lt;br/&gt;The problem with scientific power you’ve used is it didn’t require any discipline to attain it. You read what others had done and you took the next step. You didn’t earn the knowledge yourselves, so you didn’t take the responsibility for it. You stood on the shoulders of geniuses to accomplish something as fast as you could, and before you knew what you had, you patented it, packaged it, slapped it on a plastic lunch box, and now you want to sell it.&lt;br/&gt;&lt;br/&gt; HAMMOND&lt;br/&gt;You don’t give us our due credit. Our scientists have done things no one could ever do before.&lt;br/&gt;&lt;br/&gt; MALCOLM&lt;br/&gt;Your scientists were so preoccupied &lt;i&gt;with whether or not they could that they didn’t stop to think if they should.&lt;/i&gt; Science can create pesticides, but it can’t tell us not to use them. Science can make a nuclear reactor, but it can’t tell us not to build it!&lt;/p&gt;</description><link>http://blog.naqu.in/post/147753536</link><guid>http://blog.naqu.in/post/147753536</guid><pubDate>Thu, 23 Jul 2009 15:35:53 -0500</pubDate></item><item><title>"…that inside this building is the president, and not just any president—though..."</title><description>“…that inside this building is the president, and not just any president—though admittedly that probably would not have mattered—but this is a president that, fuck, we have some sort of crush on this man. He speaks like a president, not always authoritative or anything but he can form sentences, complex sentences with beginnings and ends, subordinate clauses—you can hear his semicolons! He knows the answers to questions. He knows acronyms and the names of foreign leaders, their deputies. It is heartening, it makes our country look smart, and this is an important thing, something we have too long been without.”&lt;br/&gt;&lt;br/&gt; - &lt;em&gt;Dave Eggers about Clinton circa 1994 in &lt;i&gt;A Heartbreaking work of Staggering Genius&lt;/i&gt;.&lt;/em&gt;</description><link>http://blog.naqu.in/post/98352466</link><guid>http://blog.naqu.in/post/98352466</guid><pubDate>Mon, 20 Apr 2009 20:49:45 -0500</pubDate></item><item><title>"I need to see what’s inside that box. If I learned anything from 24, you’re going to..."</title><description>“I need to see what’s inside that box. If I learned anything from 24, you’re going to want to zoom in and enhance.”&lt;br/&gt;&lt;br/&gt; - &lt;em&gt;&lt;a href="http://www.hulu.com/watch/64637/30-rock-apollo-apollo"&gt;30 Rock, “Apollo, Apollo”&lt;/a&gt;&lt;/em&gt;</description><link>http://blog.naqu.in/post/91062001</link><guid>http://blog.naqu.in/post/91062001</guid><pubDate>Sun, 29 Mar 2009 19:34:27 -0500</pubDate></item><item><title>A lot of people have used The Cure’s “Close to...</title><description>&lt;object width="400" height="336"&gt;&lt;param name="movie" value="http://www.youtube.com/v/D1KyB8PPOyw&amp;rel=0&amp;egm=0&amp;showinfo=0&amp;fs=1"&gt;&lt;/param&gt;&lt;param name="wmode" value="transparent"&gt;&lt;/param&gt;&lt;param name="allowFullScreen" value="true"&gt;&lt;/param&gt;&lt;embed src="http://www.youtube.com/v/D1KyB8PPOyw&amp;rel=0&amp;egm=0&amp;showinfo=0&amp;fs=1" type="application/x-shockwave-flash" width="400" height="336" allowFullScreen="true" wmode="transparent"&gt;&lt;/embed&gt;&lt;/object&gt;&lt;br/&gt;&lt;br/&gt;&lt;p&gt;A lot of people have used The Cure’s “Close to Me”, Diplo included. &lt;a target="_blank" title="NME" href="http://www.nme.com/blog/index.php?blog=122&amp;title=lady_sovereign_so_human_free_mp3_1&amp;more=1&amp;c=1&amp;tb=1&amp;pb=1"&gt;NME&lt;/a&gt; gives a rundown, but uses a terrible Diplo example.&lt;/p&gt;
&lt;p&gt;Now Lady Sovereign’s thrown her two cents in with her new single. I like. Plus it’ll give me good material for Lady Sovereign/Diplo/The Cure extended transitions.&lt;/p&gt;</description><link>http://blog.naqu.in/post/86998380</link><guid>http://blog.naqu.in/post/86998380</guid><pubDate>Mon, 16 Mar 2009 13:20:00 -0500</pubDate></item><item><title>For some reason, I followed Barack’s senate race in 2004....</title><description>&lt;embed type="application/x-shockwave-flash" src="http://blog.naqu.in/swf/audio_player.swf?audio_file=http://www.tumblr.com/audio_file/85046121/39oYf8OJBkvf2oufbG6aLQp0&amp;color=FFFFFF" height="27" width="207" quality="best"&gt;&lt;/embed&gt;&lt;br/&gt;&lt;br/&gt;&lt;p&gt;For some reason, I followed Barack’s senate race in 2004. Though living in Texas and being from New Orleans meant it was of little relevance to me.&lt;/p&gt;
&lt;p&gt;The gem that came out of the race was this little track. When Barack started running for president, I kept wishing I could find this track, but it had gotten lost somewhere deep in my hard drive. Well. I found it.&lt;/p&gt;</description><link>http://blog.naqu.in/post/85046121</link><guid>http://blog.naqu.in/post/85046121</guid><pubDate>Mon, 09 Mar 2009 19:56:57 -0500</pubDate></item><item><title>Worst Line of Code and Why</title><description>&lt;p&gt;I needed to grab the latest Link object for a certain Forum from Django’s ORM.&lt;/p&gt;
&lt;p&gt;Normally, I’d do this with:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;link = forum.link_set.order_by('-id')[0]&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;But for some reason, adding the slice (which adds a limit to the SQL query), was considerably slowing down the call. Whereas, just getting the iterator was fine.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;link_qs = forum.link_set.order_by('-id')&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Which is disturbing. I haven’t figured out why, but I wanted to get the code I was working on done before figuring out why this query was slow, so I hacked around it.&lt;/p&gt;

&lt;p&gt;So, of course, I could just convert that QuerySet to a list and index the list with:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;link = list(forum.link_set.order_by('-id'))[0]&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;But that would load the entire result into memory. My results could potentially be extremely large, so I didn’t want to go down that route.&lt;/p&gt;
&lt;p&gt;So I wrote the obvious solution, using the iterator, but only saving the first result. Which turned out to be one of the worst lines of code I’ve ever written.&lt;/p&gt;
&lt;p&gt;In all it’s glory:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;for link in forum.link_set.order_by('-id'):
    break&lt;/code&gt;&lt;/pre&gt;</description><link>http://blog.naqu.in/post/84221933</link><guid>http://blog.naqu.in/post/84221933</guid><pubDate>Fri, 06 Mar 2009 18:14:01 -0600</pubDate></item><item><title>"Trust me, they don’t apply to your app. If in doubt, explain your use case for XML to an..."</title><description>“Trust me, they don’t apply to your app. If in doubt, explain your use case for XML to an experienced Python developer. Or, if you have a thick skin and don’t mind being laughed at, try explaining to a Lisp programmer why your application needs XML!”&lt;br/&gt;&lt;br/&gt; - &lt;em&gt;&lt;a target="_self" title="Python is not Java" href="http://dirtsimple.org/2004/12/python-is-not-java.html"&gt;Python is not Java&lt;/a&gt;&lt;/em&gt;</description><link>http://blog.naqu.in/post/81504055</link><guid>http://blog.naqu.in/post/81504055</guid><pubDate>Wed, 25 Feb 2009 15:58:34 -0600</pubDate></item><item><title>i've lost it</title><description>&lt;p&gt;i grew up. learned how to take care of myself.&lt;br/&gt;buy beer. car insurance.&lt;/p&gt;
&lt;p&gt;and suddenly the words don’t pretty. oh! forgot the insomnia of restless sparkling girls’ eyes from across the bar. two sets at a time.&lt;/p&gt;
&lt;p&gt;oh!&lt;/p&gt;
&lt;p&gt;suddenly midnights are:&lt;br/&gt; consumption.&lt;br/&gt;alcohol. television. transportation. oh!&lt;/p&gt;
&lt;p&gt;nothing geography can’t solve?&lt;/p&gt;</description><link>http://blog.naqu.in/post/77538935</link><guid>http://blog.naqu.in/post/77538935</guid><pubDate>Wed, 11 Feb 2009 14:00:27 -0600</pubDate></item><item><title>Elegant Fibonacci Iterator</title><description>&lt;p&gt;After a bunch of versions of Fibonacci functions months back, the following iterator appeared in my notebook.&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;def fib():

    future = [0, 1]

    while True:

        future.append(sum(future))

        yield future.pop(0)&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Though of no particular engineering marvel. It is, at O(n), slower than the best case O(log(n)) I can do using matrix multiplication of powers of two.&lt;/p&gt;
&lt;p&gt;I find it particularly elegant.&lt;/p&gt;</description><link>http://blog.naqu.in/post/72887391</link><guid>http://blog.naqu.in/post/72887391</guid><pubDate>Sat, 24 Jan 2009 18:21:00 -0600</pubDate></item><item><title>Whitney and I picked up a stray kitten a little while back. My...</title><description>&lt;img src="http://26.media.tumblr.com/39oYf8OJBhyicusiHsbmMpPqo1_500.jpg"/&gt;&lt;br/&gt;&lt;br/&gt;&lt;p&gt;Whitney and I picked up a stray kitten a little while back. My first real pet.&lt;/p&gt;</description><link>http://blog.naqu.in/post/66875660</link><guid>http://blog.naqu.in/post/66875660</guid><pubDate>Fri, 26 Dec 2008 08:53:02 -0600</pubDate></item><item><title>"Numbers that fool the Fermat test are called Carmichael numbers, and little is known about them..."</title><description>“Numbers that fool the Fermat test are called Carmichael numbers, and little is known about them other than that they are extremely rare. There are 255 Carmichael numbers below 100,000,000. The smallest few are 561, 1105, 1729, 2465, 2821, and 6601. In testing primality of very large numbers chosen at random, the chance of stumbling upon a value that fools the Fermat test is less than the chance that cosmic radiation will cause the computer to make an error in carrying out a “correct” algorithm. Considering an algorithm to be inadequate for the first reason but not for the second illustrates the differences between mathematics and engineering.”&lt;br/&gt;&lt;br/&gt; - &lt;em&gt;footnote pg. 53 SICP&lt;/em&gt;</description><link>http://blog.naqu.in/post/66273250</link><guid>http://blog.naqu.in/post/66273250</guid><pubDate>Mon, 22 Dec 2008 13:53:11 -0600</pubDate></item><item><title>Mocking with Python</title><description>&lt;p&gt;After recent conversations about the surgery I did to the DISQUS IMAP processing code in order to make it testable, I was steered toward Python mock modules. Of which there are &lt;a title="google python mock" href="http://www.google.com/search?q=python+mock&amp;ie=utf-8&amp;oe=utf-8&amp;aq=t&amp;rls=org.mozilla:en-US:official&amp;client=firefox-a"&gt;a lot.&lt;/a&gt;&lt;/p&gt;
&lt;p&gt;So I installed the &lt;a title="python-mock" href="http://python-mock.sourceforge.net/"&gt;python-mock&lt;/a&gt; module and coded up a quick example use case.&lt;/p&gt;
&lt;p&gt;Often when writing unit tests, you stumble upon dependencies that the code you’re testing uses, but are orthogonal to the actual functionality being tested.&lt;/p&gt;
&lt;p&gt;At DISQUS, this was IMAP processing code that clearly depended heavily on an api into an IMAP mail server. But the functionality being tested was just what the processing did to incoming email from said server. So I wrote a fake IMAP api that just passed around test messages.&lt;/p&gt;
&lt;p&gt;However this is generally easier to do using mock objects. The mock objects can catch &lt;i&gt;all&lt;/i&gt; methods called on them, so there’s no need to strictly match the expected api. And we can inspect the actual calls made to make sure they are what we expected.&lt;/p&gt;
&lt;p&gt;So for an example I coded up a simple logger. In a production environment, it would be helpful to log data for debugging, etc. But while testing this is unnecessary.&lt;/p&gt;
&lt;p&gt;The following code does BFS using a simple logger:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;	def search(self, label):&lt;br/&gt;		"""&lt;br/&gt;		Search for label in graph. Return list of nodes visited in path.&lt;br/&gt;         Note: Uses BFS.&lt;br/&gt;		"""&lt;br/&gt;		visited = set()&lt;br/&gt;		queue = [(self, [])]&lt;br/&gt;		while queue:&lt;br/&gt;			node, path = queue.pop(0)&lt;br/&gt;			self.logger.log('Visiting node %s with path %s.' %&lt;br/&gt;                      (node.label, [n.label for n in path]), severity=1)&lt;br/&gt;&lt;br/&gt;			if node in visited:&lt;br/&gt;				continue&lt;br/&gt;			visited.add(node)&lt;br/&gt;&lt;br/&gt;			path.append(node)&lt;br/&gt;			if node.label==label:&lt;br/&gt;				return path&lt;br/&gt;			else:&lt;br/&gt;				for edge in node.edges:&lt;br/&gt;					edge_path = copy.copy(path)&lt;br/&gt;					queue.append((edge, edge_path))&lt;br/&gt;&lt;br/&gt;		self.logger.log('Not found.', severity=2)&lt;br/&gt;		return None&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Which is easy enough to test, except that we’ll be leaving log files around with this information. Instead of a real logger, we can pass in a mocked object as such:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;class NodeTests(unittest.TestCase):&lt;br/&gt;&lt;br/&gt;	def setUp(self):&lt;br/&gt;		logger = mock.Mock( {'log' : 'log called'} )&lt;br/&gt;&lt;br/&gt;		austin = Node('austin', [], logger)&lt;br/&gt;		dallas = Node('dallas', [austin], logger)&lt;br/&gt;		houston = Node('houston', [austin, dallas], logger)&lt;br/&gt;		san_antonio = Node('san antonio', [austin, houston], logger)&lt;br/&gt;&lt;br/&gt;		self.logger = logger&lt;br/&gt;		self.graph = san_antonio&lt;br/&gt;&lt;br/&gt;	def testSearch(self):&lt;br/&gt;		result = self.graph.search('dallas')&lt;br/&gt;		labels = [node.label for node in result]&lt;br/&gt;&lt;br/&gt;		self.assertEqual(['san antonio', 'houston', 'dallas'], labels)&lt;br/&gt;		self.assertEqual(5, len(self.logger.mockGetNamedCalls('log')))&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;Easy enough. And a lot easier that writing up a fake logger that matches the expected api.&lt;/p&gt;</description><link>http://blog.naqu.in/post/64431237</link><guid>http://blog.naqu.in/post/64431237</guid><pubDate>Fri, 12 Dec 2008 00:24:46 -0600</pubDate></item><item><title>Tackling JavaScript</title><description>&lt;p&gt;Lately, I’ve been looking at JavaScript a little differently. Instead of writing JavaScript as dirty hacks to do DOM manipulation, handle JavaScript as a first-class language in itself and write &lt;i&gt;elegant&lt;/i&gt; code to do DOM manipulation. Something I should have been doing all along. Toward that goal, I tackled a little project to exercise writing &lt;i&gt;elegant&lt;/i&gt; JavaScript code.&lt;/p&gt;
&lt;p&gt;At least until I can have python in the browser.&lt;/p&gt;
&lt;p&gt;This was sparked by bit of fiddling with &lt;a target="_blank" title="SproutCore" href="http://www.sproutcore.com/"&gt;SproutCore&lt;/a&gt; and reading about the other advanced JavaScript frameworks. In those frameworks, binding a bit of data to a view is the standard. The framework handles the glue code that keeps that data and the display in sync, while the programmer is left to do whatever data manipulation is needed.&lt;/p&gt;
&lt;p&gt;So I wrote a small SproutCore application that read network feeds and updated a displayed HTML list. The code that updates this need only concern itself with the data side of the equation and not the view. Which was all MVC and fine and dandy. Elegant in fact.&lt;/p&gt;
&lt;p&gt;But what bothered me was this data was handled in an intermediate representation defined by the framework. So to manipulate this data, I needed to know the framework’s API for manipulating it’s representation.&lt;/p&gt;
&lt;p&gt;There’s no reason that internal representation cannot be similar (if not exactly like) JavaScript’s own &lt;code&gt;Array.prototype.&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Hence boundarray was born. Just a simple wrapper around the JavaScript &lt;code&gt;Array.prototype&lt;/code&gt; with a near exact implementation that also handles updating a HTML list element. I thought this would be a perfect little project to exercise my JavaScript, so I quickly put up a public &lt;a target="_blank" title="BoundArray on GitHub" href="http://github.com/devin/boundarray"&gt;github repository&lt;/a&gt; and got coding.&lt;/p&gt;
&lt;p&gt;It was certainly a learning experience in writing clean JavaScript code as well as truly getting to know JavaScript’s &lt;code&gt;Array.prototype&lt;/code&gt;.&lt;/p&gt;
&lt;h4&gt;boundarray&lt;/h4&gt;
&lt;p&gt;boundarray provides a constructor called &lt;code&gt;BoundArray&lt;/code&gt; that creates an intermediate representation that behaves exactly as a JavaScript &lt;code&gt;Array&lt;/code&gt; with one single exception.&lt;/p&gt;
&lt;p&gt;An example use of boundarray with code snippets and a working example lives &lt;a target="_blank" title="BoundArray example Selection Sort" href="http://media.naqu.in/standalone/boundarray/example_selectionsort.html"&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;After conversations with a couple of JavaScript expert friends of mine, it became evident that it’s currently impossible to implement &lt;code&gt;Array.prototype&lt;/code&gt; entirely in JavaScript.&lt;/p&gt;
&lt;p&gt;The problem creeps in because &lt;code&gt;Array.prototype&lt;/code&gt; supports implicitly growing the length of the &lt;code&gt;Array&lt;/code&gt;. For example:&lt;/p&gt;
&lt;pre&gt;&lt;code&gt;var numbers = ['one', 'two', 'three'];&lt;br/&gt;numbers[4] = 'five';&lt;/code&gt;&lt;/pre&gt;
&lt;p&gt;The assignment in the second statement is out of the bounds of the array. So JavaScript silently grows the array and makes the assignment leaving an array that looks like: &lt;code&gt;['one', 'two', 'three', undefined, 'five']&lt;/code&gt;&lt;/p&gt;
&lt;p&gt;Currently JavaScript provides no means of catching assignment to undefined properties. There is a way to catch calls to undefined properties with &lt;code&gt;Object.prototype.__noSuchMethod__&lt;/code&gt;, but no similar method for properties.&lt;/p&gt;
&lt;p&gt;But failing that one little difference, a &lt;code&gt;BoundArray&lt;/code&gt; behaves exactly like a JavaScript Array as defined at the &lt;a target="_blank" title="MDC" href="https://developer.mozilla.org/en/Core_JavaScript_1.5_Reference/Global_Objects/Array/prototype"&gt;Mozilla Developer Center&lt;/a&gt;, but has the side effect of updating a displayed HTML list element.&lt;/p&gt;
&lt;p&gt;For more information on BoundArray see it’s public &lt;a target="_blank" title="BoundArray on GitHub" href="http://github.com/devin/boundarray"&gt;github repository&lt;/a&gt;.&lt;/p&gt;
&lt;p&gt;Next on the list is to consider rolling this into a plugin for &lt;a target="_blank" title="jQuery" href="http://jquery.com/"&gt;JQuery&lt;/a&gt; or something similar.&lt;/p&gt;</description><link>http://blog.naqu.in/post/60595282</link><guid>http://blog.naqu.in/post/60595282</guid><pubDate>Wed, 19 Nov 2008 18:15:00 -0600</pubDate><category>javascript</category></item><item><title>Juicy</title><description>&lt;p&gt;I always rap on and on about how hip-hop is the natural extension of postmodernism, but rarely offer up much on the subject. In an effort to start getting some of it on paper, I’m going to start tackling the topic here.&lt;/p&gt;
&lt;p&gt;Saturday I went down to Mountain View for &lt;a href="http://www.guerillaunion.com/rockthebells/"&gt;Rock the Bells&lt;/a&gt;. Mostly for &lt;a href="http://en.wikipedia.org/wiki/Dead_Prez"&gt;Dead Prez&lt;/a&gt;. After their set, I get into the postmodernism speil with April, a poet herself, when a golden gem of an example presented itself.&lt;/p&gt;
&lt;p&gt;Dead Prez’s set included a rap over the beat from The Notorious B.I.G.’s &lt;a href="http://en.wikipedia.org/wiki/Juicy_(The_Notorious_B.I.G._song)"&gt;Juicy&lt;/a&gt; (&lt;a href="http://www.youtube.com/watch?v=noTvdpAYeHE"&gt;video&lt;/a&gt;). But Dead Prez went beyond the simple example of intertextuality of borrowing another’s beat. They offered up their own version of one of the most famous verses in hip-hop, Biggie’s first verse in Juicy:&lt;/p&gt;
&lt;blockquote&gt;It was all a dream,&lt;br/&gt; I used to read Word Up magazine;&lt;br/&gt;Salt-n-Pepa and Heavy D up in the limousine.&lt;br/&gt; Hangin’ pictures on my wall,&lt;br/&gt; Every Saturday: Rap Attack, Mr. Magic, Marley Marl.&lt;br/&gt; I let my tape rock ‘til my tape popped,&lt;br/&gt; Smokin’ weed and bamboo, sippin’ on private stock;&lt;br/&gt; Way back, when I had the red and black lumberjack,&lt;br/&gt; With the hat to match.&lt;/blockquote&gt;
&lt;p&gt;Apparently released as “B.I.G. Respect” on their mixtape &lt;i&gt;Turn off the Radio: The Mixtape Vol. 1&lt;/i&gt;, Dead Prez’s version isn’t just an unrelated rap over the same beat, but follows the same pattern of Biggies’.&lt;/p&gt;
&lt;p&gt;It’s the story of their coming to age into their experience of hip-hop. It even follows the same general pattern.&lt;/p&gt;
&lt;blockquote&gt;It was all a dream!&lt;br/&gt; Started organizing in my late teens,&lt;br/&gt; Malcolm X and Huey P, who I wanna be.&lt;br/&gt; Marcus Garvey on my wall.&lt;br/&gt; I was just a young G, thought I’d been through it all.&lt;br/&gt; Fuckin’ with po-po, used to stay “lock”.&lt;br/&gt; Tryin’ to get my aim right, learning how to shoot a glock.&lt;br/&gt; Way back, used to rock the army green fields jacket,&lt;br/&gt; We was known for that.&lt;/blockquote&gt;
&lt;p&gt;This coming of age story has already been told. It’s part of the established hip-hop dictionary. Dead Prez just fits their own experience into the existing mold.&lt;/p&gt;
&lt;p&gt;Just another example of the death of stylistic innovation common in postmodernism. As Jameson puts it:&lt;/p&gt;
&lt;blockquote&gt;We thus face an aesthetic dilemma: if the experience and the ideology of the unique self is over and done with, then it is no longer clear what artists and writers of the present age are supposed to be doing. It would seem that, in a world in which stylistic innovation is no longer possible, all they can imitate are dead styles.&lt;/blockquote&gt;
&lt;p&gt;However, hip-hop, I believe embraces this dilemma and lives within it. Art doesn’t need to be stylistically innovative.&lt;/p&gt;</description><link>http://blog.naqu.in/post/46466309</link><guid>http://blog.naqu.in/post/46466309</guid><pubDate>Mon, 18 Aug 2008 17:28:00 -0500</pubDate></item></channel></rss>
