<?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>delicious robots blog</title>
	<atom:link href="http://blog.deliciousrobots.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://blog.deliciousrobots.com</link>
	<description>an exploration of electronic space and other things</description>
	<lastBuildDate>Mon, 15 Feb 2010 10:10:26 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Please, you wretched cookie-cutter forums of the internet, you can do so much better.</title>
		<link>http://blog.deliciousrobots.com/2010/02/15/please-you-wretched-cookie-cutter-forums/</link>
		<comments>http://blog.deliciousrobots.com/2010/02/15/please-you-wretched-cookie-cutter-forums/#comments</comments>
		<pubDate>Mon, 15 Feb 2010 10:10:26 +0000</pubDate>
		<dc:creator>steveth45</dc:creator>
				<category><![CDATA[Geek Stuff]]></category>
		<category><![CDATA[failure]]></category>
		<category><![CDATA[forums]]></category>
		<category><![CDATA[internet]]></category>
		<category><![CDATA[PHP]]></category>

		<guid isPermaLink="false">http://blog.deliciousrobots.com/?p=302</guid>
		<description><![CDATA[Once upon a time I answered a question on LinuxQuestions, a slow, ugly website that uses vBulletin&#8211;your average, run-of-the-mill, commercial, PHP-based forum software. I couldn&#8217;t just answer the question, I had to create an account, and give them an email address. Without warning, they started sending me weekly emails, reminding me how awesome they are, [...]]]></description>
			<content:encoded><![CDATA[<p>Once upon a time I answered a question on LinuxQuestions, a slow, ugly website that uses vBulletin&#8211;your average, run-of-the-mill, commercial, PHP-based forum software. I couldn&#8217;t just answer the question, I had to create an account, and give them an email address. Without warning, they started sending me weekly emails, reminding me how awesome they are, what&#8217;s &#8220;happening&#8221; with the site and why I should return. Well, I&#8217;ve never been back to the site. There are a few reasons why I haven&#8217;t been back to the site.</p>
<ol>
<li>It&#8217;s ugly.</li>
<li>It requires me to remember a password, or go through the hassle of resetting the password through email verification.</li>
<li>I don&#8217;t like to hang out on forums. Or, at least, I don&#8217;t anymore.</li>
<li>I actively avoid the site&#8217;s links when they show up in search results.</li>
</ol>
<p>vBulletin is bad. It powers a lot of forums in the world, and you can recognize it a mile away. I looked up vBulletin. Amazingly, people pay <em>money</em> for it. I could enumerate its sins for a long time, but I need to focus on one. In order to not receive weekly emails, you have to change your account settings. To change your account settings, you have to log into your account. If, like me, you don&#8217;t remember passwords for sites you rarely visit, you have to reset the password through email. This is a familiar song and dance to many, but it angers me immensely. It is trivially easy to generate a single, safe, instant unsubscribe link for mailing list emails. Why don&#8217;t LinuxQuestions emails have a one-click unsubscribe? Easy, they are idiots. They are stupid for using crappy, commercial forum software. They are idiots for spamming my inbox and forcing me to jump through a bunch of hoops to unsubscribe.</p>
<p>Sites like Stack Overflow use my OpenID login, which I know well. I can remember a single username / password for multiple sites without the fear of one site secretly stealing my password and using it to log in to another site as me. Trust me, more sites than you want to know store your password as plain text in their databases. Why am I picking on this one website? They are trying to get more visitors who provide more content, so they can extract advertising money or satisfaction that they are helping Linux users everywhere. They want to be a popular destination for a certain niche, but they completely fail to create a compelling experience in any way at all. It is so bad, I actively ignore search hits for their site.</p>
<p>Software matters. Usability matters. You can&#8217;t just punt on the software. Invest in it. Pay a real designer and get a real programmer on your team. Unless you are independently wealthy, you need a software developer on your team. This software developer will want to make the site awesome, since he is personally invested in the success of the site. People want to know what makes a website successful. Ultimately, its the folks writing the code who make or break a site. The software developer needs access to the code, so don&#8217;t even dream of buying a closed-source, commercial solution. Start with the best open source solution (let the software developer pick), pay for a good design (don&#8217;t do a design competition, that&#8217;s exploitative), and let the software developer merge the two. Your software developer uses social web software all the time. He knows what&#8217;s good. Force him to eat his own dogfood (use the site himself), and he&#8217;ll work harder at improving the experience. Elicit feedback from your users and implement popular features. </p>
<p>The tragedy is that these old, broken forums end up loaded with useful information donated by the users despite the shortcomings of the software the site rests on. Don&#8217;t post useful information on these sites. Please, don&#8217;t encourage them.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.deliciousrobots.com/2010/02/15/please-you-wretched-cookie-cutter-forums/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Compiler code generation: just flatten the tree</title>
		<link>http://blog.deliciousrobots.com/2010/02/09/compiler-code-generation-just-flatten-the-tree/</link>
		<comments>http://blog.deliciousrobots.com/2010/02/09/compiler-code-generation-just-flatten-the-tree/#comments</comments>
		<pubDate>Tue, 09 Feb 2010 09:53:09 +0000</pubDate>
		<dc:creator>steveth45</dc:creator>
				<category><![CDATA[Geek Stuff]]></category>
		<category><![CDATA[bison]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[compiler]]></category>
		<category><![CDATA[flex]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://blog.deliciousrobots.com/?p=286</guid>
		<description><![CDATA[I spent some time tonight working on the compiler I am writing for my own education. I started this project while only understanding bits and pieces of what it takes to write a compiler, but I am quickly learning more. Various texts on compilers start with creating a simple calculator program that takes a string [...]]]></description>
			<content:encoded><![CDATA[<p>I spent some time tonight working on the compiler I am writing for my own education. I started this project while only understanding bits and pieces of what it takes to write a compiler, but I am quickly learning more. Various texts on compilers start with creating a simple calculator program that takes a string like &#8220;10 + 2 * 6 &#8211; 4 / 2&#8243; and calculates the result. For this example the problem can be broken down into a few simple steps which are merely transformations:</p>
<ol>
<li>Transform the input into a string of discrete tokens. &#8220;10 + 3 * 4&#8243; becomes &#8220;10&#8243;, &#8220;+&#8221;, &#8220;3&#8243;, &#8220;*&#8221;, &#8220;4&#8243;. This is achieved with a scanner/lexer like lex, or the newer flex.</li>
<li>Transfom the token stream into a parse tree using a BNF description of the grammar with a parser tool like yacc or the newer bison. Using infix notation, our stream becomes (&#8220;+&#8221;, &#8220;10&#8243;, (&#8220;*&#8221;, &#8220;3&#8243;, &#8220;4&#8243;)).</li>
<li>Reduce the tree by walking it and calculating the value of each node, depth first, until you have calculated the value of the root node which is the value of the whole tree.
<ol>
<li>Evaluate left side of &#8220;+&#8221; operation. It is the constant value 10.</li>
<li>Evaluate right side of &#8220;+&#8221; operation.
<ol>
<li>Evaluate left side of &#8220;*&#8221; operation. It is the constant value 3.</li>
<li>Evaluate right side of &#8220;*&#8221; operation. It is the constant value 4.</li>
<li>Reduce: 3 * 4 is 12</li>
</ol>
<p>			It is the numerical value 12.
		</li>
<li>Reduce: 10 + 12 is 22</li>
</ol>
</li>
</ol>
<p>The whole thing is straight-forward and almost insultingly easy. The tools I&#8217;ve been using are Flex and Bison which generate the scanner and parser, respectively, in C. While writing these parts to generate a parse tree, which could also be called an abstract syntax tree or AST, I had a realization. Code generation, whether it is byte-code or machine-code, is simple. The code does the same tree-walk, in the same order, but instead of evaluating and reducing the elements, just generate byte-code or machine-code which will do those simple operations at runtime. The program spits out a flat sequence of code. You are merely writing code that flattens the tree.</p>
<p>To put it another way, the sequence of mathematical operations that reduce the parse tree to a single value is the same ordered sequence of operations that the compiled code should represent. It&#8217;s so obvious, it&#8217;s painful. I just hadn&#8217;t thought about it before. I thought there would be some deep voodoo involved in compilation, but there isn&#8217;t. Your program is likely to be a series of expressions, not just one. All you have to do is evaluate them in order, tacking the byte-code or machine code for each expression on to the end of the code.</p>
<p>Here is a series of transformations that does what I am talking about:</p>
<ol>
<li>Input : &#8220;x = 10 + y * 3&#8243;</li>
<li>Scanner : &#8220;10&#8243;, &#8220;+&#8221;, &#8220;y&#8221;, &#8220;*&#8221;, &#8220;3&#8243;</li>
<li>Parser : (&#8220;=&#8221;, &#8220;x&#8221;, (&#8220;+&#8221;, &#8220;10&#8243;, (&#8220;*&#8221;, &#8220;y&#8221;, &#8220;3&#8243;)))</li>
<li>Tree walk :
<ol>
<li>Store the location of &#8220;x&#8221; in R0</li>
<li>Store &#8220;10&#8243; in R1.</li>
<li>Store the value at location &#8220;y&#8221; in R2</li>
<li>Store &#8220;3&#8243; in R3</li>
<li>Store the product of R2 and R3 in R4</li>
<li>Store the product of R1 and R4 in R5</li>
<li>Store R5 at the location specified in R0</li>
</ol>
</li>
</ol>
<p>It&#8217;s just one more step to take the list of operations and turn them into a list of coded instructions. At that point you are done.</p>
<p>One of the things I wanted with my compiler is type inference, so when I get to an &#8220;=&#8221; or &#8220;assignment&#8221; node of my tree, I evaluate the right side first, which gives me a type, then I know what the type of the identifier on the left should be. The first time an identifier is used should always be an assignment, and the type of the identifier is set at that time, if another assignment to the same identifier is made of a different type, the program should fail to compile. Here is the messy / incomplete code that handles this right now:</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #993333;">int</span> genAssignByteCode<span style="color: #009900;">&#40;</span>simpleblock b<span style="color: #339933;">,</span> simplenode n<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>n<span style="color: #339933;">-&gt;</span>R <span style="color: #339933;">==</span> NULL<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;No R-value in assignment!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        exit<span style="color: #009900;">&#40;</span>EXIT_FAILURE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>n<span style="color: #339933;">-&gt;</span>L <span style="color: #339933;">==</span> NULL<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;No L-value in assignment!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        exit<span style="color: #009900;">&#40;</span>EXIT_FAILURE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>n<span style="color: #339933;">-&gt;</span>L<span style="color: #339933;">-&gt;</span>type <span style="color: #339933;">!=</span> IDENTIFIER<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;L-value is not an identifier!<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        exit<span style="color: #009900;">&#40;</span>EXIT_FAILURE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #993333;">int</span> reg_num <span style="color: #339933;">=</span> genByteCode<span style="color: #009900;">&#40;</span>b<span style="color: #339933;">,</span> n<span style="color: #339933;">-&gt;</span>R<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>reg_num <span style="color: #339933;">==</span> <span style="color: #339933;">-</span><span style="color: #0000dd;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;The r-value has no value. Lame.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        exit<span style="color: #009900;">&#40;</span>EXIT_FAILURE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000000; font-weight: bold;">enum</span> reg_type rval_type <span style="color: #339933;">=</span> get_reg_type<span style="color: #009900;">&#40;</span>b<span style="color: #339933;">,</span> reg_num<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    simplesymbol inmap <span style="color: #339933;">=</span> in_map<span style="color: #009900;">&#40;</span>b<span style="color: #339933;">,</span> n<span style="color: #339933;">-&gt;</span>L<span style="color: #339933;">-&gt;</span>data.<span style="color: #993333;">string</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #993333;">struct</span> byte_code bc<span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>inmap <span style="color: #339933;">==</span> NULL<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// new value with big, scary TYPE INFERENCE</span>
        <span style="color: #993333;">int</span> newr <span style="color: #339933;">=</span> new_reg<span style="color: #009900;">&#40;</span>b<span style="color: #339933;">,</span>rval_type<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        new_symbol<span style="color: #009900;">&#40;</span>b<span style="color: #339933;">,</span> n<span style="color: #339933;">-&gt;</span>data.<span style="color: #993333;">string</span><span style="color: #339933;">,</span> newr<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>rval_type <span style="color: #339933;">==</span> rt_int<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            bc.<span style="color: #202020;">t</span> <span style="color: #339933;">=</span> BCI_STORE_INT<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>rval_type <span style="color: #339933;">==</span> rt_string<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            bc.<span style="color: #202020;">t</span> <span style="color: #339933;">=</span> BCI_STORE_STR<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        bc.<span style="color: #202020;">reg_a</span> <span style="color: #339933;">=</span> newr<span style="color: #339933;">;</span>
        bc.<span style="color: #202020;">reg_b</span> <span style="color: #339933;">=</span> reg_num<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #666666; font-style: italic;">// existing value, better do some static type checks</span>
        <span style="color: #000000; font-weight: bold;">enum</span> reg_type lval_type <span style="color: #339933;">=</span> get_reg_type<span style="color: #009900;">&#40;</span>b<span style="color: #339933;">,</span>inmap<span style="color: #339933;">-&gt;</span>reg_num<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>rval_type <span style="color: #339933;">!=</span> lval_type<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;You got the wrong type, pal.<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            exit<span style="color: #009900;">&#40;</span>EXIT_FAILURE<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #b1b100;">if</span><span style="color: #009900;">&#40;</span>rval_type <span style="color: #339933;">==</span> rt_int<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            bc.<span style="color: #202020;">t</span> <span style="color: #339933;">=</span> BCI_STORE_INT<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>rval_type <span style="color: #339933;">==</span> rt_string<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            bc.<span style="color: #202020;">t</span> <span style="color: #339933;">=</span> BCI_STORE_STR<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        bc.<span style="color: #202020;">reg_a</span> <span style="color: #339933;">=</span> inmap<span style="color: #339933;">-&gt;</span>reg_num<span style="color: #339933;">;</span>
        bc.<span style="color: #202020;">reg_b</span> <span style="color: #339933;">=</span> reg_num<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    new_code<span style="color: #009900;">&#40;</span>b<span style="color: #339933;">,</span>bc<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> bc.<span style="color: #202020;">reg_a</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>Basically, <code>simpleblock</code> is a typedef&#8217;ed pointer to a struct that represents a block of code. It keeps an array of virtual registers allocated, array of instructions, and linked list of symbols (identifiers). This code generates the byte-code for the assignment operation. It is called from the <code>genByteCode</code> function which it calls recursively to evaluate the right side of the statement. The line <code>new_code(b,bc);</code> actually appends the byte-code <code>bc</code> to the array of instructions in block instance <code>b</code>. This is C code, and as such, it is really ugly. Also, the error messages are unhelpful and quite rude. This is a rough-draft of sorts. There are a lot of parts not in the code that would help explain it, too.</p>
<p>While constructing this part, I realized that it is very easy to do type inference. Java and C# took years and years to acquire type inference of any kind, but I don&#8217;t see what all the fuss is about, it is quite simple to implement, really. Compilers are not scary, go write one of your own already. Go create that perfect programming language. Do it now, or else you&#8217;ll spend the rest of your life writing software in programming languages that you don&#8217;t like and you&#8217;ll only have yourself to blame.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.deliciousrobots.com/2010/02/09/compiler-code-generation-just-flatten-the-tree/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Create your own programming language for great justice</title>
		<link>http://blog.deliciousrobots.com/2010/02/07/create-your-own-programming-language-for-great-justice/</link>
		<comments>http://blog.deliciousrobots.com/2010/02/07/create-your-own-programming-language-for-great-justice/#comments</comments>
		<pubDate>Mon, 08 Feb 2010 05:52:12 +0000</pubDate>
		<dc:creator>steveth45</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://blog.deliciousrobots.com/?p=282</guid>
		<description><![CDATA[In an earlier post I wrote my wish-list for the perfect programming language (for me.) I believe most programmers have a perfect programming language in mind. It usually goes something like this: &#8220;I really like the object model of language W and the functional aspects of language X and the syntax of language Y. It [...]]]></description>
			<content:encoded><![CDATA[<p>In an earlier post I wrote my wish-list for the perfect programming language (for me.) I believe most programmers have a perfect programming language in mind. It usually goes something like this: &#8220;I really like the object model of language W and the functional aspects of language X and the syntax of language Y. It would be great if it ran on Z runtime / virtual machine, too.&#8221; Then there are the language zealots who say that language Foo is perfect, or at least better than all the other commonly used languages and with such a large user base and library of reusable code, that even if they could fix the bits of the language they don&#8217;t like, it wouldn&#8217;t be worth it because it would break existing code. In a sense, they&#8217;ve settled, put their tent pegs in the ground and encourage others to set up camp where they are, so they can all share code. There is certainly a lot of value is just spending a lot of time one language so you can go deep with it and develop a strong set of idioms and patterns. It also leads to a large number of very exclusive camps. To be fair, many programmers dabble in multiple languages and paradigms, but most do not.</p>
<p>I often am offended by programming language zealots. Just like religious zealots, they are myopic, self-important and insulting towards outsiders. I love much of the writings of Paul Graham, but I find that his statements about Lisp and all that which is not Lisp to be difficult and often insulting. In his article &#8220;Beating the Averages,&#8221; Graham talks about a hypothetical language Blub that a programmer gets attached to. This programmer sees less powerful programming languages for what they are, but doesn&#8217;t understand the abstractions and power of better programming languages. The programmer thinks he is using the best programming language, but he isn&#8217;t. He isn&#8217;t using the best programming language, because&#8230; he isn&#8217;t using Lisp. Period. Graham states explicitly, &#8220;Lisp is so great not because of some magic quality visible only to devotees, but because it is simply the most powerful language available. And the reason everyone doesn&#8217;t use it is that programming languages are not merely technologies, but habits of mind as well, and nothing changes slower.&#8221; Again, I like Paul Graham&#8217;s writings for the most part, but saying that he is correct about this because he seems to be correct about so many other things would be a fallacy as much as saying that he is wrong because everything else he says is wrong.</p>
<p>I agree with many points in Graham&#8217;s article, more or less, but I don&#8217;t think that most programmers are small-minded Blub programmers. I agree that Lisp macros are powerful, that it is a powerful concept to write programs that write programs etc, but Lisp is hardly the only language that does that. Boo, a language that actually fulfills my wish-list, has macros which are very similar in functionality to Lisp macros. You get access to the actual parse tree and can create or change or augment language features in the language itself. It is a powerful feature, but it is not the most powerful feature, at least not for me. It&#8217;s not that I have a small mind, either. I understand that Lisp macros are awesome, but it is not an abstraction I need in day to day programming to get things done. I&#8217;m a big fan of getting things done, and building systems to help get them done faster and better. Graham argues that he used Lisp with lots of macros in a business that did well, with software that delivered functionality not present in the competitor&#8217;s projects, and that this was all because of Lisp macros, which comprised at least 20% of his product&#8217;s code. I&#8217;m sure there was a correlation between having features and being successful, but the 20% macros thing may just be significant of the fact that he had to add a lot of things to Lisp to be able to build the software he wanted. It could also be significant of the fact that he knows how to write macros. Just because the competitors did not deliver the same features does not mean that they didn&#8217;t or couldn&#8217;t because they used some less powerful programming language. The software he wrote could likely have been written in any number of languages by any number of programmers. The programmers that toil away in so-called Blub languages, oftentimes are quite good at what they do, and can deliver features if they can think them up or are allowed to add them or whatever. Look at Facebook, they rock the world with the power of PHP, a pretty awful language by the estimation of many. I just doubt that Lisp was the reason for Paul Graham&#8217;s success.</p>
<p>I said all that to say this: Lisp worship aside, it is a good idea for programmers to learn things about programming languages that they don&#8217;t understand. It is healthy to stretch your mind with different perspectives. I believe that it is a universally accepted truth that looking at a problem from different mental perspectives causes us to have breakthroughs of new ideas and understanding. One excellent way to expand your mind as a programmer is to create your own programming language&#8211;not just a theoretical grammar, but an actual working compiler and/or interpreter.</p>
<p>I have to admit, I&#8217;ve been a little lackadaisical regarding hacking code in my free time. I think I haven&#8217;t been able to get excited about a project. I&#8217;ve announced, prematurely, some projects in the past that have been since abandoned. Perhaps it is that I spend so much time at work &#8220;getting things done&#8221; that I relish the freedom to not get things done outside work. It&#8217;s true, I like messing around with different programming languages and not committing to projects. However, since the programming language wish-list post, I&#8217;ve been drawn to my newest project over and over. I am working on a new programming language. It is really in the very earliest of toy language stages right now. I am really just learning things as I go. I am learning how to use flex and bison, the descendants of lex and yacc, what abstract syntax tree is, and many other things. I&#8217;ve read a lot of articles about different aspects of compiler / interpreter design and techniques. Some things have inspired me a great deal, like V8, the JavaScript engine that Google created. I&#8217;ve been interested and inspired by byte-code interpreters, JIT compilation, garbage collection, stack-less interpreters, coroutines, closures, and many other things.</p>
<p>As you can imagine, I have a laundry-list of things I would like to implement in my very own programming language, but this project is mostly about learning, not the end product. I know most folks with a BSCS have taken a course on compilers and have written a compiler of some form or other. With all the instruction on compilers that has taken place, why do we settle for such mediocre languages. Python 3, was a very small step forward from Python 2.x. It was such a small step, that I wonder what the point was. They cleaned up the syntax and some of the libraries a little bit, but for a backwards compatibility breaking change, they sure didn&#8217;t do much to fix the inadequacies of the language. The &#8220;hot&#8221; programming languages these days are incredibly old&#8211;at least in internet years. Ruby is 15 years old. Python is 19 years old. Why, after developing a science around compilers, do we still use languages that require semicolons on nearly every line? If Ruby and Python are each at least a decade and a half old, do so many companies write code in C#, Java, PHP, and so many other languages that have vestigial syntax from C? Perhaps it is the slowness of businesses. I don&#8217;t think so. I think it is laziness.</p>
<p>Software developers have the ability to write their own compilers. Yet, they do not. It is intellectual laziness. We are blacksmiths. We can forge our own tools, yet we use the crappy ones handed down to us from old. There are established techniques for creating new tools, yet we forge on with the tools given to us. For shame. What if every developer wrote their own programming language? Sure, most would fall by the way-side and we need developers to write libraries, too and to research other concerns like concurrency, that don&#8217;t necessarily need to be solved at the language level. There are millions of software developers in the world. Python is dead. If you have the chance, fire up the python interpreter some time and type in &#8220;import this&#8221; and hit enter. You will discover a creed, a philosophy that underpins many of the decisions surrounding the creation and maintenance of Python. It is a philosophy I disagree with on many points. Not only that, but the Python language fails to fulfill a good number of them. &#8220;In the face of ambiguity, refuse the temptation to guess.&#8221; Python does not allow type annotations, everything is guesswork. &#8220;foo = bar&#8221; could mean a lot of things, it could be changing the value referenced by &#8220;bar&#8221; or it could be creating a new reference. &#8220;bar&#8221; could be anything, a number, a string, an object. Looking at Python code is often a lot of guesswork. Duck typing is the only typing available. When you create a function, you can only name the inputs, not define the type. It seems to fail here. Here is another : &#8220;There should be one&#8211; and preferably only one &#8211;obvious way to do it.&#8221; That line gets a lot of deserved criticism. It&#8217;s not even true. Python is a very powerful language that offers a very large number of way to solve any problem, and usually several of them could be considered obvious, depending on the style of code you normally write. Regardless, even if you could write a language that only had one obvious way to solve each problem, you would have a very poor language. &#8220;If the implementation is hard to explain, it&#8217;s a bad idea.&#8221; CPython has the GIL, both hard to explain, and a very bad idea.</p>
<p>I encourage every developer to write their own programming language. It will break us out of the complacency that allows us to toil day-in and day-out using the same leaky, broken abstractions. We can write our own tools. I&#8217;ve seen the HTTP 1.1 RFC, it isn&#8217;t that complicated, and I think the tools we use now to implement server-side code for web development are quite broken. PHP is just awful. Python is a little better, as is Ruby. V8 / JavaScript / Node.js has some promise, but JavaScript is a pretty broken language, too. It&#8217;s got that semi-colon problem, plus wierd object / array / function rules that lead to ambiguity and confusion. At the end of the day, you can&#8217;t have everything in one language, though they are certainly trying to do that with Perl 6. We can have better choices, or at least break through the complacency.</p>
<p>I don&#8217;t want to sound completely critical. Change is happening. People who are stuck with certain runtimes, like the JVM or .NET have created or ported better languages for the platform. There is quite a bit of excitement about new compiler tools, runtimes, front-ends, back-end etc. The LLVM project has gained a lot of attention, lately, for being completely self-hosting.</p>
<p>My own project is just barely getting going. I have the code constructing a complete AST, and I am in the process of writing the part that generates byte-code. I&#8217;ve learned so much already about grammars, tree manipulation, and parsing. I have ideas about register allocation and other things. It is quite the experience to get my hands dirty writing C code again. I am taking advantage of tools I didn&#8217;t know about in the past, like the Boehm garbage collector. I&#8217;ve spent too long in garbage collected languages to have to worry about freeing every bit of memory by hand now. That and generous use of typedefs make the experience not entirely unpleasant.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.deliciousrobots.com/2010/02/07/create-your-own-programming-language-for-great-justice/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Genie: Write Python-like code that runs as fast as pure C</title>
		<link>http://blog.deliciousrobots.com/2010/02/03/genie-write-python-like-code-that-runs-as-fast-as-pure-c/</link>
		<comments>http://blog.deliciousrobots.com/2010/02/03/genie-write-python-like-code-that-runs-as-fast-as-pure-c/#comments</comments>
		<pubDate>Wed, 03 Feb 2010 09:51:08 +0000</pubDate>
		<dc:creator>steveth45</dc:creator>
				<category><![CDATA[Geek Stuff]]></category>
		<category><![CDATA[c]]></category>
		<category><![CDATA[genie]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[python]]></category>

		<guid isPermaLink="false">http://blog.deliciousrobots.com/?p=275</guid>
		<description><![CDATA[There is an article on the Postabon Blog titled &#8220;Make Lisp 15x faster than Python or 4x faster than Java.&#8221; In his benchmark, which is a tightly nested loop doing some trigonometric calculations, he achieves speed similar to Java with unoptimized Lisp and a 4X speed increase over that with some Lisp optimizations. He also [...]]]></description>
			<content:encoded><![CDATA[<p>There is an <a href="http://blog.postabon.com/make-lisp-15x-faster-than-python-or-4x-faster">article</a> on the Postabon Blog titled &#8220;Make Lisp 15x faster than Python or 4x faster than Java.&#8221; In his benchmark, which is a tightly nested loop doing some trigonometric calculations, he achieves speed similar to Java with unoptimized Lisp and a 4X speed increase over that with some Lisp optimizations. He also compares it with Python which is silly because Python&#8217;s slowness is a known and insurmountable problem with the language.</p>
<p>I&#8217;ve been looking for a programming language with some of the feel of Python but with static, inferred typing. There are a couple, but <a href="http://live.gnome.org/Genie">Genie</a> caught my eye so I wrote his algorithm in Genie. Basically, the differences between Genie and Python I noticed in porting the code are that functions need type declarations (something I have yet to see inferred in any statically typed language) and that variables are initialized with the &#8220;var&#8221; keyword. Genie does away with Python&#8217;s use of the colon, which is redundant, and instead uses it for type declarations.</p>
<p>Here is the new code.</p>

<div class="wp_syntax"><div class="code"><pre class="python" style="font-family:monospace;"><span style="color: black;">&#91;</span>indent=<span style="color: #ff4500;">4</span><span style="color: black;">&#93;</span>
uses
    GLib
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> radians<span style="color: black;">&#40;</span>n : double<span style="color: black;">&#41;</span> : double
    <span style="color: #ff7700;font-weight:bold;">return</span> n <span style="color: #66cc66;">*</span> <span style="color: #ff4500;">0.0174532925</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> distance<span style="color: black;">&#40;</span>latA : double, lngA : double, latB : double, lngB : double<span style="color: black;">&#41;</span> : double
    var radius = <span style="color: #ff4500;">6371.0</span>
    var latAr = radians<span style="color: black;">&#40;</span>latA<span style="color: black;">&#41;</span>
    var lngAr = radians<span style="color: black;">&#40;</span>lngA<span style="color: black;">&#41;</span>
    var latBr = radians<span style="color: black;">&#40;</span>latB<span style="color: black;">&#41;</span>
    var lngBr = radians<span style="color: black;">&#40;</span>lngB<span style="color: black;">&#41;</span>
&nbsp;
    var deltaLat = latBr - latAr
    var deltaLng = lngBr - lngAr
&nbsp;
    <span style="color: #ff7700;font-weight:bold;">return</span> radius <span style="color: #66cc66;">*</span> <span style="color: #ff4500;">2</span> <span style="color: #66cc66;">*</span> Math.<span style="color: black;">asin</span><span style="color: black;">&#40;</span>Math.<span style="color: black;">sqrt</span><span style="color: black;">&#40;</span>Math.<span style="color: #008000;">pow</span><span style="color: black;">&#40;</span>Math.<span style="color: black;">sin</span><span style="color: black;">&#40;</span>deltaLat/<span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>,<span style="color: #ff4500;">2.0</span><span style="color: black;">&#41;</span> \
        + Math.<span style="color: black;">cos</span><span style="color: black;">&#40;</span>latAr<span style="color: black;">&#41;</span> <span style="color: #66cc66;">*</span> Math.<span style="color: black;">cos</span><span style="color: black;">&#40;</span>latBr<span style="color: black;">&#41;</span> <span style="color: #66cc66;">*</span> <span style="color: black;">&#40;</span>Math.<span style="color: #008000;">pow</span><span style="color: black;">&#40;</span>Math.<span style="color: black;">sin</span><span style="color: black;">&#40;</span>deltaLng/<span style="color: #ff4500;">2</span><span style="color: black;">&#41;</span>,<span style="color: #ff4500;">2.0</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span><span style="color: black;">&#41;</span>
&nbsp;
<span style="color: #ff7700;font-weight:bold;">def</span> bench<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span>
    var increment = <span style="color: #ff4500;">2.5</span>
&nbsp;
    var latA = -<span style="color: #ff4500;">90.0</span>
    <span style="color: #ff7700;font-weight:bold;">while</span><span style="color: black;">&#40;</span>latA <span style="color: #66cc66;">&lt;</span>= <span style="color: #ff4500;">90.0</span><span style="color: black;">&#41;</span>
        var lngA = -<span style="color: #ff4500;">180.0</span>
        <span style="color: #ff7700;font-weight:bold;">while</span><span style="color: black;">&#40;</span>lngA <span style="color: #66cc66;">&lt;</span>= <span style="color: #ff4500;">180.0</span><span style="color: black;">&#41;</span>
            var latB = -<span style="color: #ff4500;">90.0</span>
            <span style="color: #ff7700;font-weight:bold;">while</span><span style="color: black;">&#40;</span>latB <span style="color: #66cc66;">&lt;</span>= <span style="color: #ff4500;">90.0</span><span style="color: black;">&#41;</span>
                var lngB = -<span style="color: #ff4500;">180.0</span>
                <span style="color: #ff7700;font-weight:bold;">while</span><span style="color: black;">&#40;</span>lngB <span style="color: #66cc66;">&lt;</span>= <span style="color: #ff4500;">180.0</span><span style="color: black;">&#41;</span>
                    distance<span style="color: black;">&#40;</span>latA, lngA, latB, lngB<span style="color: black;">&#41;</span>
                    lngB = lngB + increment
                latB = latB + increment
            lngA = lngA + increment
        latA = latA + increment
&nbsp;
init
    bench<span style="color: black;">&#40;</span><span style="color: black;">&#41;</span></pre></div></div>

<p>This code runs on my laptop in about 35 seconds, which as far as I can tell from the blog post, is about as fast as the optimized Lisp. Of course, Genie is translating the code into pure C, and then compiling it to an executable. Syntax is a matter of personal taste, and I like the way Genie works. Since it compiles to a pure binary, there is no runtime. It leverages existing C libraries, using GLib objects as its native object system. The C API is universal and Genie can emit C header files too, so I could see myself writing Genie code that integrated with almost any software development platform.</p>
<p>For comparison, here is the somewhat verbose C code generated by <code>valac</code>, the Genie / Vala compiler.</p>

<div class="wp_syntax"><div class="code"><pre class="c" style="font-family:monospace;"><span style="color: #339933;">#include &lt;glib.h&gt;</span>
<span style="color: #339933;">#include &lt;glib-object.h&gt;</span>
<span style="color: #339933;">#include &lt;float.h&gt;</span>
<span style="color: #339933;">#include &lt;math.h&gt;</span>
<span style="color: #339933;">#include &lt;stdlib.h&gt;</span>
<span style="color: #339933;">#include &lt;string.h&gt;</span>
&nbsp;
<span style="color: #993333;">double</span> radians <span style="color: #009900;">&#40;</span><span style="color: #993333;">double</span> n<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #993333;">double</span> distance <span style="color: #009900;">&#40;</span><span style="color: #993333;">double</span> latA<span style="color: #339933;">,</span> <span style="color: #993333;">double</span> lngA<span style="color: #339933;">,</span> <span style="color: #993333;">double</span> latB<span style="color: #339933;">,</span> <span style="color: #993333;">double</span> lngB<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #993333;">void</span> bench <span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #993333;">void</span> _main <span style="color: #009900;">&#40;</span><span style="color: #993333;">char</span><span style="color: #339933;">**</span> args<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> args_length1<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #993333;">double</span> radians <span style="color: #009900;">&#40;</span><span style="color: #993333;">double</span> n<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">double</span> result<span style="color: #339933;">;</span>
    result <span style="color: #339933;">=</span> n <span style="color: #339933;">*</span> <span style="color:#800080;">0.0174532925</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> result<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">double</span> distance <span style="color: #009900;">&#40;</span><span style="color: #993333;">double</span> latA<span style="color: #339933;">,</span> <span style="color: #993333;">double</span> lngA<span style="color: #339933;">,</span> <span style="color: #993333;">double</span> latB<span style="color: #339933;">,</span> <span style="color: #993333;">double</span> lngB<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">double</span> result<span style="color: #339933;">;</span>
    <span style="color: #993333;">double</span> radius<span style="color: #339933;">;</span>
    <span style="color: #993333;">double</span> latAr<span style="color: #339933;">;</span>
    <span style="color: #993333;">double</span> lngAr<span style="color: #339933;">;</span>
    <span style="color: #993333;">double</span> latBr<span style="color: #339933;">;</span>
    <span style="color: #993333;">double</span> lngBr<span style="color: #339933;">;</span>
    <span style="color: #993333;">double</span> deltaLat<span style="color: #339933;">;</span>
    <span style="color: #993333;">double</span> deltaLng<span style="color: #339933;">;</span>
    radius <span style="color: #339933;">=</span> <span style="color:#800080;">6371.0</span><span style="color: #339933;">;</span>
    latAr <span style="color: #339933;">=</span> radians <span style="color: #009900;">&#40;</span>latA<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    lngAr <span style="color: #339933;">=</span> radians <span style="color: #009900;">&#40;</span>lngA<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    latBr <span style="color: #339933;">=</span> radians <span style="color: #009900;">&#40;</span>latB<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    lngBr <span style="color: #339933;">=</span> radians <span style="color: #009900;">&#40;</span>lngB<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    deltaLat <span style="color: #339933;">=</span> latBr <span style="color: #339933;">-</span> latAr<span style="color: #339933;">;</span>
    deltaLng <span style="color: #339933;">=</span> lngBr <span style="color: #339933;">-</span> lngAr<span style="color: #339933;">;</span>
    result <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>radius <span style="color: #339933;">*</span> <span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> asin <span style="color: #009900;">&#40;</span>sqrt <span style="color: #009900;">&#40;</span>pow <span style="color: #009900;">&#40;</span>sin <span style="color: #009900;">&#40;</span>deltaLat <span style="color: #339933;">/</span> <span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color:#800080;">2.0</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#40;</span>cos <span style="color: #009900;">&#40;</span>latAr<span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> cos <span style="color: #009900;">&#40;</span>latBr<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">*</span> pow <span style="color: #009900;">&#40;</span>sin <span style="color: #009900;">&#40;</span>deltaLng <span style="color: #339933;">/</span> <span style="color: #0000dd;">2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> <span style="color:#800080;">2.0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> result<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">void</span> bench <span style="color: #009900;">&#40;</span><span style="color: #993333;">void</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">double</span> increment<span style="color: #339933;">;</span>
    <span style="color: #993333;">double</span> latA<span style="color: #339933;">;</span>
    increment <span style="color: #339933;">=</span> <span style="color:#800080;">2.5</span><span style="color: #339933;">;</span>
    latA <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color:#800080;">90.0</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span>TRUE<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #993333;">double</span> lngA<span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #009900;">&#40;</span>latA <span style="color: #339933;">&lt;=</span> <span style="color:#800080;">90.0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        lngA <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color:#800080;">180.0</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span>TRUE<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #993333;">double</span> latB<span style="color: #339933;">;</span>
            <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #009900;">&#40;</span>lngA <span style="color: #339933;">&lt;=</span> <span style="color:#800080;">180.0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            latB <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color:#800080;">90.0</span><span style="color: #339933;">;</span>
            <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span>TRUE<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #993333;">double</span> lngB<span style="color: #339933;">;</span>
                <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #009900;">&#40;</span>latB <span style="color: #339933;">&lt;=</span> <span style="color:#800080;">90.0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
                lngB <span style="color: #339933;">=</span> <span style="color: #339933;">-</span><span style="color:#800080;">180.0</span><span style="color: #339933;">;</span>
                <span style="color: #b1b100;">while</span> <span style="color: #009900;">&#40;</span>TRUE<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #009900;">&#40;</span>lngB <span style="color: #339933;">&lt;=</span> <span style="color:#800080;">180.0</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        <span style="color: #000000; font-weight: bold;">break</span><span style="color: #339933;">;</span>
                    <span style="color: #009900;">&#125;</span>
                    distance <span style="color: #009900;">&#40;</span>latA<span style="color: #339933;">,</span> lngA<span style="color: #339933;">,</span> latB<span style="color: #339933;">,</span> lngB<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                    lngB <span style="color: #339933;">=</span> lngB <span style="color: #339933;">+</span> increment<span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span>
                latB <span style="color: #339933;">=</span> latB <span style="color: #339933;">+</span> increment<span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
            lngA <span style="color: #339933;">=</span> lngA <span style="color: #339933;">+</span> increment<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
        latA <span style="color: #339933;">=</span> latA <span style="color: #339933;">+</span> increment<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">void</span> _main <span style="color: #009900;">&#40;</span><span style="color: #993333;">char</span><span style="color: #339933;">**</span> args<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> args_length1<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    bench <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #993333;">int</span> main <span style="color: #009900;">&#40;</span><span style="color: #993333;">int</span> argc<span style="color: #339933;">,</span> <span style="color: #993333;">char</span> <span style="color: #339933;">**</span> argv<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    g_type_init <span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    _main <span style="color: #009900;">&#40;</span>argv<span style="color: #339933;">,</span> argc<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #0000dd;">0</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

]]></content:encoded>
			<wfw:commentRss>http://blog.deliciousrobots.com/2010/02/03/genie-write-python-like-code-that-runs-as-fast-as-pure-c/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Moving on from visualizations</title>
		<link>http://blog.deliciousrobots.com/2010/02/01/moving-on-from-visualizations/</link>
		<comments>http://blog.deliciousrobots.com/2010/02/01/moving-on-from-visualizations/#comments</comments>
		<pubDate>Mon, 01 Feb 2010 10:51:32 +0000</pubDate>
		<dc:creator>steveth45</dc:creator>
				<category><![CDATA[Geek Stuff]]></category>
		<category><![CDATA[mysql]]></category>
		<category><![CDATA[philosophy]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://blog.deliciousrobots.com/?p=268</guid>
		<description><![CDATA[I am beginning to question the appropriateness of visual aids in professional software development. When I only occasionally had to look at database structures and test out SQL queries, I used tools like phpMyAdmin and MySQL Workbench which provide GUI visualizations and click-friendly database management. I now look back at those times in the same [...]]]></description>
			<content:encoded><![CDATA[<p>I am beginning to question the appropriateness of visual aids in professional software development. When I only occasionally had to look at database structures and test out SQL queries, I used tools like phpMyAdmin and MySQL Workbench which provide GUI visualizations and click-friendly database management. I now look back at those times in the same way that a young adult reader might look back at a time when he cherished picture books. Once I managed to overcome the intimidating nature of the mysql command-line tool, I found myself spending more and more time in that tool and less and less in the visual tools. Visual design tools have always been a crutch&#8211;useful for getting started, but eventually a hindrance to getting things done quickly and efficiently.</p>
<p>Using the mysql command line tool helps me be a better database designer. First of all, I have to communicate with the tool in SQL with a few helper functions. It responds by printing out information in ASCII formatted tables. I only need to know a handful of basic commands to get started.</p>

<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">USE</span> dbname <span style="color: #808080; font-style: italic;">-- sets the active database</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SHOW</span> <span style="color: #993333; font-weight: bold;">TABLES</span> <span style="color: #808080; font-style: italic;">-- lists the tables in the active database</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">DESC</span> tablename <span style="color: #808080; font-style: italic;">-- shows the structure of the table</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="sql" style="font-family:monospace;"><span style="color: #993333; font-weight: bold;">SHOW</span> <span style="color: #993333; font-weight: bold;">CREATE</span> <span style="color: #993333; font-weight: bold;">TABLE</span> tablename <span style="color: #808080; font-style: italic;">-- shows SQL to create table</span></pre></div></div>

<p>The SHOW CREATE TABLE command is ultimately more useful than the DESC command because it gives you a better idea of how the table was created, and how to create similar tables in the future. I learned quite a bit about MySQL by using this command to inspect various tables. </p>
<p>Using command-line tools to interact with systems like databases puts me closer to the action. After awhile I feel like I am getting my hands dirty, and I get a feel for the data. At times this feeling can be negative. Working with MyISAM tables can be painful when the database allows me to put the database into invalid states. I get a visceral sense for the inadequacy and brittleness of MyISAM when I manipulate it by hand. InnoDB tables feel a little more constricting at times, being much stricter about data integrity, but this strictness also brings confidence in the safety of an ACID compliant database.</p>
<p>MySQL is just one example of how native command-line interfaces, however daunting, are often more powerful tools for interacting with computing systems than the fancy GUI tools. Perhaps, with concerted effort, the visual tools could include some of the efficiencies of the command-line tools, but for the most part they feel slow and stupid. Other tools I use more on the command line than anywhere else are revision control tools and testing frameworks.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.deliciousrobots.com/2010/02/01/moving-on-from-visualizations/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Getting Things Done</title>
		<link>http://blog.deliciousrobots.com/2010/01/27/getting-things-done/</link>
		<comments>http://blog.deliciousrobots.com/2010/01/27/getting-things-done/#comments</comments>
		<pubDate>Wed, 27 Jan 2010 09:43:42 +0000</pubDate>
		<dc:creator>steveth45</dc:creator>
				<category><![CDATA[Geek Stuff]]></category>
		<category><![CDATA[hack]]></category>
		<category><![CDATA[knowledge]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://blog.deliciousrobots.com/?p=264</guid>
		<description><![CDATA[I work at a very small software company that has as clients other small businesses. We do custom software. In 2010 that means we do web development. We bid on projects, complete the projects we get, fix any bugs for free for 6 months, and charge a very competitive hourly rate for new features. We [...]]]></description>
			<content:encoded><![CDATA[<p>I work at a very small software company that has as clients other small businesses. We do custom software. In 2010 that means we do web development. We bid on projects, complete the projects we get, fix any bugs for free for 6 months, and charge a very competitive hourly rate for new features. We don&#8217;t have failed projects, per se. Sometimes a customer might pay us to create something that doesn&#8217;t end up helping their business, but the software we create is of reasonably high quality and does what it claims to do. It&#8217;s tough times economically, so when a customer wants us to create a new feature on their web page, we generally try to get it done in hours, not days. Some days I close several tickets. I feel like I am delivering quite a bit of value to our customers. Now we support our products with a 6 month guarantee of free bug fixes, which means if I spend 2 hours on a small feature, I need to be reasonably certain that it works correctly, because if it comes back with a major issue two weeks later, we don&#8217;t charge the customer for the time it takes to fix. The more time I spend on a feature, the less perceived value the client is getting because it is costing them more. However, if I do the feature in less time, and it turns out to have a bug, any cost savings the customer might have seen go away the moment that the feature fails in production and they suffer real business losses. There are books upon books written on this topic, but I&#8217;m just going to share a few things that I&#8217;ve learned in my short career thus far in software development.</p>
<p>Software development doesn&#8217;t need to be slow to be high quality. I have been a slow convert to the unit testing religion, but now I am just about there. These days when a client wants a new feature, I find the test classes that correlate to the class I&#8217;m messing with, write some tests for the new functionality which fail. I type in some code, run the tests and repeat until they are all passing. I run all the unit tests for the project (which currently runs very quickly as we haven&#8217;t been doing this for a long time) to make sure I didn&#8217;t inadvertently break anything else and ship it. It&#8217;s fast and I see very few bugs slip through this way. </p>
<p>Develop a small set of simple, high quality tools. Most of our work is in PHP. After you are done groaning about how awful PHP is, read on. A lot of our work consists of rescue operations. Small business owners and budding entrepreneurs, bless their little hearts, don&#8217;t usually have a lot of cash to throw around, so they take the lowest bid for their website work. The lowest bid is usually a company from India or Romania or some other exotic locale where US dollars are still worth quite a bit. These thrifty folks end up with a half-working, extremely buggy piece of software which is basically a single folder with a whole bunch of files in it ending with &#8220;.php&#8221;. The internals of these files would make any software developer worth his or her salt cringe. You and I know that the best thing to do would be to throw the whole thing out, and start from scratch, preferably with a language like Python or Ruby. Well, the client doesn&#8217;t know that, they come to us to fix the code. Well, we have a tool, it is a very lightweight PHP framework that can integrate nicely with existing code. I have used it so much that I am at a point where it feels natural to use it to solve problems. Our clients do pay us to fix their code which we do gladly, and efficiently, usually replacing the broken parts completely with clean, MVC style code. </p>
<p>Imperative, object-oriented code is efficient for writing web software. I&#8217;ve read a bunch about heavy OO patterns and this and that, and I think most of it is crap. Nobody needs all that. What customers need is to create related hunks of data, which I call &#8220;rows&#8221; from a database perspective, or &#8220;objects&#8221; from a programming perspective. I have created my very own simple, Active Record style ORM for PHP and MySQL. Basically, you give me an existing database table. In about 2 minutes I can slap together about 20 lines of boilerplate code (mostly informing the ORM what the name and data type is of each column) which gives me the ability to easily do CRUD operations (create, read, update, delete) on your database table. The ORM generates all the SQL in the background, so the only code I have to write is PHP. The columns of your database become public members of the class instances (objects). Now, according to the rules of OO, this is a big mistake. Well, really, it&#8217;s not. You&#8217;ve got collections of data to move around and use, why hide everything behind a bunch of pointless accessors? All that extra data-hiding cruft just slows things down. What the customer wants is a bunch of data that gets dumped into the correct parts of the HTML. The customer doesn&#8217;t care about &#8220;proper OO&#8221; and neither should you. They are not paying you hourly to implement latest design pattern you read about.</p>
<p>Use a dynamic, &#8220;scripting&#8221; language like PHP, Python, or Ruby (or Perl if you are a masochist). I now point Apache directly at my project folder so when I&#8217;m done typing, I hit a few keys which save my work, switch to a console to run a test or switch to a browser and reload the page. I see the results of what I typed within about one second of when I finished typing. There is no deploy step, no compile step, no waiting, no distraction, just instant feedback. It&#8217;s amazing, and it has revolutionized my workflow. Remember, our clients are paying hourly rates, which when you break it down into minutes is generally some amount of money greater than $1 per minute. A five minute compile/deploy step costs the client several dollars each time. I remember writing code in Visual Studio, I believe the F5 button was the compile/run shortcut. Hitting that on a reasonably complex piece of software was like an invitation to go get a snack, start up a conversation with a coworker or get distracted with interesting articles on the internet. The big secret is of course that every programming language gets compiled, it&#8217;s just that C++ compiles really slowly, has an extra pre-processor step and has to be linked with a bunch of other code in a time consuming way. Things like Python get compiled as they are read into the interpreter into byte code which is then executed by the interpreter immediately. That just happens to be really fast, plus the byte code is cached to disk oftentimes. In the .01% of the time when the simplest possible code in your scripting language of choice isn&#8217;t fast enough for your client&#8217;s need, first check to see if you can fix it with a better algorithm, if not, rewrite the critical, innermost loop in C.</p>
<p>In conclusion, I may use a lame tool (PHP) and butcher &#8220;best OO practices&#8221; and make babies cry by completely ignoring &#8220;functional&#8221; programming, but I get things done. Not only that, but I get things done well, with a low defect rate, and quickly. The code I write is easily maintainable and ships with good test coverage. The &#8220;good, fast, cheap: pick two&#8221; thing is a lie. With a familiar, well-honed collection of tools any software developer can do all three.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.deliciousrobots.com/2010/01/27/getting-things-done/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>My Programming Language Wish-List</title>
		<link>http://blog.deliciousrobots.com/2010/01/14/my-programming-language-wish-list/</link>
		<comments>http://blog.deliciousrobots.com/2010/01/14/my-programming-language-wish-list/#comments</comments>
		<pubDate>Thu, 14 Jan 2010 10:13:03 +0000</pubDate>
		<dc:creator>steveth45</dc:creator>
				<category><![CDATA[Geek Stuff]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[ruby]]></category>
		<category><![CDATA[tools]]></category>

		<guid isPermaLink="false">http://blog.deliciousrobots.com/?p=254</guid>
		<description><![CDATA[I write custom software for a living. It&#8217;s usually fun, sometimes challenging, often tedious, but overall gratifying in a number of ways. One of these ways is that I get to be creative and build my own tools that help me write better software that is quicker to write, easier to support, and is more [...]]]></description>
			<content:encoded><![CDATA[<p>I write custom software for a living. It&#8217;s usually fun, sometimes challenging, often tedious, but overall gratifying in a number of ways. One of these ways is that I get to be creative and build my own tools that help me write better software that is quicker to write, easier to support, and is more stable. Software is often boring, and the boring parts get all the tools, abstractions, DSL&#8217;s, and the like. Programming languages provide the abstraction layer to build these things. We use programming languages like tools in a tool belt, pulling out the &#8220;right tool, for the right job.&#8221; Many of these &#8220;right tools&#8221; are just general purpose programming languages that happen to have a feature or abstraction that another general purpose programming language doesn&#8217;t. Even though we&#8217;ve been developing programming languages for the last 60 years, software is still being written in languages that are so severely lacking that programmers are little more than data entry drones.</p>
<p>I don&#8217;t propose that any particular language is THE ANSWER. Such a thing does not exist, at least not yet. There aren&#8217;t even any particular fantastic ones. The more I investigate other languages, the more I see features that I wish existed in the languages I use to get things done with every day. So, I&#8217;d like to compile a list. There&#8217;s no reason why these things can&#8217;t coexist in the same programming language. Most of the programming languages we use are very old and as certain ideas drift into other languages, the need for backwards compatibility causes new features to be implemented in strange ways. For example, object oriented programming being tacked onto old programming languages has created all sorts of syntactical abominations: C++, Perl 5 blessed references, and Objective-C to name a few.</p>
<p>Last rant before the list: I&#8217;ve been told that every Computer Science student takes a class on compilers and yet, we still write programs in PHP, Java, and C++. I&#8217;m just a lowly English major but I think we can do better. I&#8217;ve been reading Coders at Work, a collection of interviews with important software developers. Many of the interviews are with designers or people influential in the creation or design of popular or important programming languages such as JavaScript, Haskell, Erlang, Common Lisp, and Smalltalk. It&#8217;s really interesting stuff, but it shows how most of these languages have flashes of brilliance, pieces that add to the puzzle, but are also missing out on a lot of innovations. That said, here is my list:</p>
<p>1. Classes and namespaces. It doesn&#8217;t need to be object oriented, as that implies that everything has to be an object, even the entry point of your program. Java is object oriented, and I consider it a fault. Too many objects and too many classes. That said, classes of objects are nice abstractions that allow the programmer to arrange code and data in useful ways. Similarly, namespaces are a nice way of avoiding name collisions and organizing code. I want to be able to put stand-alone or helper functions in namespaces, and not be forced to create static functions as part of class definitions. Not everything has to be part of a class. Python gets this right (modules as namespaces).</p>
<p>2, Arbitrary data structures. Sometimes data enters a program from an external source like a database, or from a web browser, or a file. It is very handy to be able to take something like a string of JSON or XML and just convert it into a schema-less blob of data, native to your programming environment. JavaScript does this particularly well with dynamic objects and arrays that can nest in arbitrary ways. The literal notation for such lists and objects is so simple and straight-forward in JavaScript that it is the de facto standard for data exchange, thanks in part to Doug Crockford who codified it. Most languages we would consider &#8220;dynamically typed&#8221; do a decent job of this like Python, Perl, Ruby, and even PHP. Most languages we consider &#8220;statically typed&#8221; do not. Do I even have to name names?</p>
<p>3. First class functions. Functions need to be able to move around, breathe, combine and create other functions. This allows the programmer to create innumerable abstractions. C++, Java, and PHP just completely fail at this. Python does a half-assed job&#8211;sorry, but Python just kind of sucks this way. JavaScript, Perl, and many other languages do a better job.</p>
<p>4. Static and dynamic typing. I want it all. There are compelling reasons for both. I see no reason why I can&#8217;t have both. Some languages already have this, like Boo, C# 4.0, Delphi, and maybe PHP. Maybe what I want is called optional static typing. That&#8217;s not quite right, as it is really is a matter of perspective. It could be called optional dynamic typing, too, I suppose. In any event, it&#8217;s not voodoo. I don&#8217;t know why stubborn languages like Java and Python will not allow optional &#8220;other-style&#8221; typing. A lot of energy is spent with statically typed languages dealing with arbitrary data structures in sane ways or initializing objects with excessive typing: &#8220;Circle mycircle = new Circle()&#8221;. Conversely, people in the purely dynamic languages camp spend a lot of time agonizing over compiler and runtime optimization when it would be so simple for the programmer to just let a profiler tell him where the &#8220;hotspots&#8221; are so he or she can just go in and add a few hints or type constraints into the code which allow the compiler or runtime to generate much faster code. In Python, you generally &#8220;drop down to C&#8221; to optimize the bits of code that run too slow. That&#8217;s a shame, since it can destroy the cross-platform capability of your code and/or tie it to a single language implementation. It is also needlessly complex. Boo is really the best language I&#8217;ve seen in this space. It looks like a dynamically typed language but it actually uses type inference and is statically typed by default but allows explicit dynamic typing.</p>
<p>5. Good Concurrency Options. I like Erlang&#8217;s actor model. I don&#8217;t care much for the rest of the language. Much has been written about concurrency recently so I won&#8217;t say much. There needs to be a way to share data between different threads and/or processes in a safe, sane way and the ability to effectively utilize multiple processing cores efficiently.</p>
<p>6. Clean syntax. I normally can adapt pretty well, but some languages just drive me bonkers with their awful syntax. Perl is one of those languages. I am not a fan of Lisp and it&#8217;s many direct offspring. S-expressions are so mundane as to be mind-erasingly boring. Those languages sorely need some variation&#8211;some different operators or <em>something</em>. There is a healthy balance, leaning toward the simple. Python and Ruby are both decent. CoffeeScript, a mini-language that compiles to JavaScript has some neat ideas in this space. Using different symbols and operators allow languages to be expressive, and make it easier to catch the meaning of a bit of code at a glance, but are often superfluous. Most commas, semicolons, and curly braces you see are completely useless. 99% of the time, statements are separated by new lines, why do you need a semicolon, too? You don&#8217;t. Items in a list are often separated by a comma and some white space, usually one space character. Do you need a comma? You don&#8217;t. Usually when you see curly brackets, only one of them is adding any meaning to the code: the last one.</p>
<p>There are more things that I want from programming languages, but those are some big ones. If I could find a language that does them all, I&#8217;d be a happier programmer. Ultimately, software development is about getting things done and no profession has a perfect set of tools, but I still think that we can do much better.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.deliciousrobots.com/2010/01/14/my-programming-language-wish-list/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>My life without God</title>
		<link>http://blog.deliciousrobots.com/2009/12/10/my-life-without-god/</link>
		<comments>http://blog.deliciousrobots.com/2009/12/10/my-life-without-god/#comments</comments>
		<pubDate>Thu, 10 Dec 2009 10:40:38 +0000</pubDate>
		<dc:creator>steveth45</dc:creator>
				<category><![CDATA[News and Updates]]></category>
		<category><![CDATA[philosophy]]></category>

		<guid isPermaLink="false">http://blog.deliciousrobots.com/?p=247</guid>
		<description><![CDATA[This is awkward.
Most of my family and friends consider me to be a devout Christian. Mostly they believe this because that&#8217;s what I&#8217;ve been until relatively recently. I have a few things to get off my chest about this and to set the record straight.
I was raised in a very devout and loving Christian home. [...]]]></description>
			<content:encoded><![CDATA[<p>This is awkward.</p>
<p>Most of my family and friends consider me to be a devout Christian. Mostly they believe this because that&#8217;s what I&#8217;ve been until relatively recently. I have a few things to get off my chest about this and to set the record straight.</p>
<p>I was raised in a very devout and loving Christian home. These are the types of homes that create strong Christians. I was one of those for a very long time. In college I got involved in a very intense church and college group. I felt a strong ownership of my faith and my involvement in the church. I believed the Bible deeply, especially everything about Jesus. I very much believed the core Gospel message and considered myself to have a personal relationship with Jesus. I prayed a lot. I read the Bible a lot. I attended many meetings, and worshiped, and spoke in tongues and everything. I did it all, and did it all wholeheartedly&#8211;for years.</p>
<p>Let nobody say that I didn&#8217;t have a real experience of Christianity&#8211;that I thought I was a Christian but wasn&#8217;t. I was the real-deal, if such a thing can exist. I had spiritual epiphanies and experiences. I often confessed my sins and shortcomings to God and other believers. I truly felt remorse over sin and asked God for forgiveness on many occasions. I had little fear of death as I was utterly convinced of a very pleasant after-life. I had faith.</p>
<p>All I ever got in return for my faith was warm fuzzy feelings and friendships with other Christians. I believed that God healed me when I was sick in the hospital but the fact is that my internal bleed was detected, and that surgeons took care of the physical reason for the internal bleeding. I just ascribed my good fortune to God. I never had anything happen that qualified as a miracle. All I ever had was faith. Other things happened that I prayed about that seemed to have no divine intervention. God did not seem to solve things that I prayed about. Financial problems, health problems, relational problems, and other calamities just continued for me at the pace that, I believe, most people experience them. God did nothing in response to prayer.</p>
<p>I gave God a lot. I gave him many, many hours of my time. I gave him a lot of money. Ten percent of my gross income turned into over half my disposable income when it came to tithing. I seriously paid a lot of money. I even tithed when I was in massive debt. I was tithing to a God that I thought was the kind of God who spoke to people and healed people and responded. However, everything that I had taken as responses from God were easily explainable from a materialist perspective. The Bible shows God talking directly to people. Somehow this never happened with me. All I had were vague impressions and feelings or coincidences that I ascribed to God.</p>
<p>It took me a long time to realize that it is OK to give up on one-sided relationships. It is normal and healthy to stop communicating with those who never respond. Send a few emails to somebody without ever getting a response, and eventually you stop. I prayed to God tens of thousands of times. If God wants to communicate with me, the line is always open on my end. He just hasn&#8217;t called. I&#8217;ve done plenty for God and given him plenty of chances to show himself as God, but he either doesn&#8217;t exist, doesn&#8217;t care, or doesn&#8217;t want me to believe in him. Either way, it leaves me in the clear to safely ignore him.</p>
<p>Having spent the last three years not attending church or involving myself much in reading the Bible, praying, worshiping and the like, I can attest that it hasn&#8217;t made much of a difference in my life. I do not feel that I have become a worse person. If anything, I have relaxed a bit. In the last three years I have become happier. I&#8217;ve left behind some unhealthy addictions. Instead of looking forward to an afterlife, I can focus on the present life. Instead of tithing, I support a child in the third world. Instead of feeling guilty for not shoving the gospel down other people&#8217;s throats, I only feel some regret for the times in the now distant past when I did. I have not become unethical or amoral. I just care more for people and less for God.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.deliciousrobots.com/2009/12/10/my-life-without-god/feed/</wfw:commentRss>
		<slash:comments>3</slash:comments>
		</item>
		<item>
		<title>Root.js : A Skeletal MVC Framework for Node.js</title>
		<link>http://blog.deliciousrobots.com/2009/12/04/root-js-a-skeletal-mvc-framework-for-node-js/</link>
		<comments>http://blog.deliciousrobots.com/2009/12/04/root-js-a-skeletal-mvc-framework-for-node-js/#comments</comments>
		<pubDate>Sat, 05 Dec 2009 05:47:49 +0000</pubDate>
		<dc:creator>steveth45</dc:creator>
				<category><![CDATA[Geek Stuff]]></category>
		<category><![CDATA[Grokking JavaScript]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[nodejs]]></category>
		<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://blog.deliciousrobots.com/?p=225</guid>
		<description><![CDATA[OK, by skeletal, I mean absolutely no fat whatsoever. What you are about to witness is merely an illustration of a thought. Here&#8217;s the thought: I want to do serious server-side web development with JavaScript and Node.js. Why? Node.js is a convergence. The creation of Node.js was precipitated by several phenomena including the rise of [...]]]></description>
			<content:encoded><![CDATA[<p>OK, by skeletal, I mean absolutely no fat whatsoever. What you are about to witness is merely an illustration of a thought. Here&#8217;s the thought: I want to do serious server-side web development with JavaScript and Node.js. Why? Node.js is a convergence. The creation of Node.js was precipitated by several phenomena including the rise of JavaScript as the central programming language of the web, increased interest in functional programming, and advances in event-based programming such as epoll system call in Linux 2.6.</p>
<p>Without further ado, here is Root.js in primordial form:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> sys <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;sys&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> http <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;http&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> posix <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;posix&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">function</span> loadfolder <span style="color: #009900;">&#40;</span><span style="color: #000066;">name</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> output <span style="color: #339933;">=</span> sys.<span style="color: #660066;">exec</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;ls &quot;</span> <span style="color: #339933;">+</span> <span style="color: #000066;">name</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">wait</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> files <span style="color: #339933;">=</span> output<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">split</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> j <span style="color: #000066; font-weight: bold;">in</span> files<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>files<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">match</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/\.js$/</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            <span style="color: #003366; font-weight: bold;">var</span> modname <span style="color: #339933;">=</span> files<span style="color: #009900;">&#91;</span>j<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">replace</span><span style="color: #009900;">&#40;</span><span style="color: #009966; font-style: italic;">/\.js$/</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            sys.<span style="color: #660066;">puts</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Loading &quot;</span> <span style="color: #339933;">+</span> <span style="color: #000066;">name</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;/&quot;</span> <span style="color: #339933;">+</span> modname<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            data<span style="color: #009900;">&#91;</span>modname<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;./&quot;</span> <span style="color: #339933;">+</span> <span style="color: #000066;">name</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;/&quot;</span> <span style="color: #339933;">+</span> modname<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">return</span> data<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> models <span style="color: #339933;">=</span> loadfolder<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;models&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> views <span style="color: #339933;">=</span> loadfolder<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;views&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> controllers <span style="color: #339933;">=</span> loadfolder<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;controllers&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> x <span style="color: #000066; font-weight: bold;">in</span> controllers<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    controllers<span style="color: #009900;">&#91;</span>x<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">load</span><span style="color: #009900;">&#40;</span>models<span style="color: #339933;">,</span>views<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #003366; font-weight: bold;">var</span> routemap <span style="color: #339933;">=</span> 
<span style="color: #009900;">&#91;</span>
    <span style="color: #009900;">&#91;</span><span style="color: #339933;">/^</span>\<span style="color: #339933;">/</span>other<span style="color: #009900;">&#40;</span>.<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>$<span style="color: #339933;">/,</span> <span style="color: #3366CC;">&quot;other&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#91;</span><span style="color: #339933;">/^</span>\<span style="color: #339933;">/</span><span style="color: #009900;">&#40;</span>.<span style="color: #339933;">*</span><span style="color: #009900;">&#41;</span>$<span style="color: #339933;">/,</span> <span style="color: #3366CC;">&quot;default&quot;</span><span style="color: #009900;">&#93;</span>
<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
http.<span style="color: #660066;">createServer</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>request<span style="color: #339933;">,</span> response<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">for</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #000066; font-weight: bold;">in</span> routemap<span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> route <span style="color: #339933;">=</span> routemap<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
        <span style="color: #003366; font-weight: bold;">var</span> matches <span style="color: #339933;">=</span> route<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">0</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">exec</span><span style="color: #009900;">&#40;</span>request.<span style="color: #660066;">uri</span>.<span style="color: #660066;">path</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">if</span><span style="color: #009900;">&#40;</span>matches <span style="color: #339933;">!=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span>
        <span style="color: #009900;">&#123;</span>
            matched <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
            <span style="color: #003366; font-weight: bold;">var</span> c <span style="color: #339933;">=</span> controllers<span style="color: #009900;">&#91;</span>route<span style="color: #009900;">&#91;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#93;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
            c.<span style="color: #660066;">handle</span><span style="color: #009900;">&#40;</span>matches.<span style="color: #660066;">slice</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>request<span style="color: #339933;">,</span>response<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span> 
            <span style="color: #000066; font-weight: bold;">return</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">listen</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">8000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
sys.<span style="color: #660066;">puts</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Server running at http://127.0.0.1:8000/&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>This file expects a few things. It expects there will be folders named <code>models</code>, <code>views</code>, and <code>controllers</code>. In its current configuration the routemap will expect to find files named <code>default.js</code> and <code>other.js</code> in the <code>controllers</code> folder. Any controllers should export a function called <code>load</code> that provides a way to pass in the models and views during initialization and a function called <code>handle</code> that is called when there is an incoming request. Here is a simple example controller (<code>default.js</code>):</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> models <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> views <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span>
&nbsp;
exports.<span style="color: #660066;">load</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>m<span style="color: #339933;">,</span>v<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span> models <span style="color: #339933;">=</span> m<span style="color: #339933;">;</span> views <span style="color: #339933;">=</span> v<span style="color: #339933;">;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
exports.<span style="color: #660066;">handle</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>matches<span style="color: #339933;">,</span> request<span style="color: #339933;">,</span> response<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    models.<span style="color: #660066;">mymodel</span>.<span style="color: #660066;">getData</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        views.<span style="color: #660066;">defaultview</span>.<span style="color: #660066;">show</span><span style="color: #009900;">&#40;</span>response<span style="color: #339933;">,</span>data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This controller calls the getData function exported by the <code>mymodel</code> model which would be a file named <code>mymodel.js</code> in the <code>models</code> folder looking something like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> data <span style="color: #339933;">=</span> 
<span style="color: #009900;">&#123;</span>
    title <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Page Title&quot;</span><span style="color: #339933;">,</span>
    message <span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Hello, JS World&quot;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
exports.<span style="color: #660066;">getData</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>callback<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    callback<span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>In this case, the model does not need to do an asynchronous call to get the data, but in case it did, the controller passes in a callback that the model can pass the data to asynchronously, in typical Node.js fashion. The last thing we need is the view (<code>views/defaultview.js</code>):</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> posix <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;posix&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> jsontemplate <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;../json-template&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">jsontemplate</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> templates <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
loadtemplates <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    templates<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'basic'</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> jsontemplate.<span style="color: #660066;">Template</span><span style="color: #009900;">&#40;</span>
        posix.<span style="color: #660066;">cat</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;./templates/basic.html&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">wait</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
exports.<span style="color: #660066;">show</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>response<span style="color: #339933;">,</span> data<span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#123;</span>
    response.<span style="color: #660066;">sendHeader</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">200</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;Content-Type&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;text/html&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> text <span style="color: #339933;">=</span> templates<span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'basic'</span><span style="color: #009900;">&#93;</span>.<span style="color: #660066;">expand</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    response.<span style="color: #660066;">sendBody</span><span style="color: #009900;">&#40;</span>text<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    response.<span style="color: #660066;">finish</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
loadtemplates<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Oh yes, and the template (<code>templates/basic.html</code>):</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;!DOCTYPE html&gt;
&lt;head&gt;
&lt;title&gt;{title}&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
&lt;h3&gt;{message}&lt;/h3&gt;
&lt;/body&gt;</pre></div></div>

<p>This example uses JSON Template which I briefly discussed <a href="/2009/11/26/node-js-and-json-template-a-delicious-pairing/">here</a>.</p>
<p>The preceding code actually works. It&#8217;s missing the <code>controllers/other.js</code> controller, but it&#8217;s enough to get the general idea. I could sit down with this as a starting point and create a web application. In fact, I just may do this. This framework has basically zero helper functions. I&#8217;ve noticed that the best helper functions and other abstractions of useful functionality spring forth naturally from the act of programming and active refactoring. When I notice myself repeating work, or pondering the idea of copy-pasting code from one part of an application to another, that&#8217;s generally the time I do the work of building infrastructure. If I decide to use this framework to do something useful, then it will develop naturally. Right now it is a blank canvas.</p>
<p>I recently used <a href="http://code.google.com/p/redis/">Redis</a> to solve some problems as work. There is already a node.js library for accessing Redis <a href="http://github.com/fictorial/redis-node-client">here</a>, so I&#8217;m thinking about building something with that. Redis is essentially a persistent key-value store that supports simple data structures with atomic operations such as lists and sets. The &#8220;list&#8221; solved a very specific problem for us which had previously been partially solved (in an ultimately broken way) by building a queue on top of Memcached using a locking system built on the atomic increment and decrement operations. We dropped in the Redis list and everything started working perfectly.</p>
<p>If I decide that I need a traditional SQL backend, I would probably use <a href="http://code.nytimes.com/projects/dbslayer">DBSlayer</a>, a http/JSON interface for MySQL, though Ryan Dahl has promised native support for some traditional databases in future versions of Node.js. Cool.</p>
]]></content:encoded>
			<wfw:commentRss>http://blog.deliciousrobots.com/2009/12/04/root-js-a-skeletal-mvc-framework-for-node-js/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Node.js and JSON Template : A delicious pairing</title>
		<link>http://blog.deliciousrobots.com/2009/11/26/node-js-and-json-template-a-delicious-pairing/</link>
		<comments>http://blog.deliciousrobots.com/2009/11/26/node-js-and-json-template-a-delicious-pairing/#comments</comments>
		<pubDate>Thu, 26 Nov 2009 09:26:57 +0000</pubDate>
		<dc:creator>steveth45</dc:creator>
				<category><![CDATA[Geek Stuff]]></category>
		<category><![CDATA[Grokking JavaScript]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[JSON]]></category>
		<category><![CDATA[json-template]]></category>
		<category><![CDATA[nodejs]]></category>
		<category><![CDATA[web]]></category>

		<guid isPermaLink="false">http://blog.deliciousrobots.com/?p=220</guid>
		<description><![CDATA[I recently blogged about the virtues of Node.js and my personal desire to use JavaScript on the server. Well, I decided to start putting the pieces together. First of all, every good web framework needs a template system. I found JSON Template, a simple enough template system written in JavaScript and intended for client-side HTML [...]]]></description>
			<content:encoded><![CDATA[<p>I recently blogged about the virtues of Node.js and my personal desire to use JavaScript on the server. Well, I decided to start putting the pieces together. First of all, every good web framework needs a template system. I found <a href="http://json-template.googlecode.com/svn/trunk/doc/Introducing-JSON-Template.html">JSON Template</a>, a simple enough template system written in JavaScript and intended for client-side HTML template work.</p>
<p>I decided to combine the &#8220;Hello, World&#8221; examples from Node.js and JSON Template. This is what I came up with.</p>
<p>The first thing you have to do is make JSON Template conform to the <a href="http://commonjs.org/specs/modules/1.0.html">CommonJS standard</a> which Node.js so wisely implements. To do that, add the following line to the end of the json-template.js file:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;">exports.<span style="color: #660066;">jsontemplate</span> <span style="color: #339933;">=</span> jsontemplate<span style="color: #339933;">;</span></pre></div></div>

<p>That was easy enough. Put that file in the same folder as your Node.js project and include it with the following line:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> jsontemplate <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;./json-template&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">jsontemplate</span><span style="color: #339933;">;</span></pre></div></div>

<p>And now for the big finale, Node.js with JSON Template:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> sys <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;sys&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> http <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;http&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #003366; font-weight: bold;">var</span> jsontemplate <span style="color: #339933;">=</span> require<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;./json-template&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">jsontemplate</span><span style="color: #339933;">;</span>
&nbsp;
http.<span style="color: #660066;">createServer</span><span style="color: #009900;">&#40;</span> <span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>request<span style="color: #339933;">,</span> response<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    response.<span style="color: #660066;">sendHeader</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">200</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span><span style="color: #3366CC;">&quot;Content-Type&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;text/plain&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> t <span style="color: #339933;">=</span> jsontemplate.<span style="color: #660066;">Template</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'Hello {name}'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003366; font-weight: bold;">var</span> text <span style="color: #339933;">=</span> t.<span style="color: #660066;">expand</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span><span style="color: #3366CC;">'name'</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'world'</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    response.<span style="color: #660066;">sendBody</span><span style="color: #009900;">&#40;</span>text<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    response.<span style="color: #660066;">finish</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">listen</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">8000</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
sys.<span style="color: #660066;">puts</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Server running at http://127.0.0.1:8000/&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></div></div>

<p>Thanks to <a href="http://mvalente.eu/">MV</a> for referring me to <a href="http://commonjs.org/">CommonJS</a>. He seems to have realized the power of an all JavaScript web development stack for some time and has some good rants on the topic. </p>
]]></content:encoded>
			<wfw:commentRss>http://blog.deliciousrobots.com/2009/11/26/node-js-and-json-template-a-delicious-pairing/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>
