<?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>Iterative.ly</title>
	<atom:link href="http://iterative.ly/feed/" rel="self" type="application/rss+xml" />
	<link>http://iterative.ly</link>
	<description>One step at a time</description>
	<lastBuildDate>Sun, 25 Jul 2010 00:04:01 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.0</generator>
		<item>
		<title>Introducing Chlorine: Keep your Rails app clean</title>
		<link>http://iterative.ly/2010/07/24/introducing-chlorine-keep-your-rails-app-clean/</link>
		<comments>http://iterative.ly/2010/07/24/introducing-chlorine-keep-your-rails-app-clean/#comments</comments>
		<pubDate>Sun, 25 Jul 2010 00:04:01 +0000</pubDate>
		<dc:creator>Trevor Burnham</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[chlorine]]></category>
		<category><![CDATA[coffeescript]]></category>
		<category><![CDATA[rails]]></category>
		<category><![CDATA[sass]]></category>

		<guid isPermaLink="false">http://iterative.ly/?p=244</guid>
		<description><![CDATA[One annoyance I’ve had as a Rails developer is that when I use Sass, I end up with a bunch of CSS files; when I use CoffeeScript, I end up with a bunch of JavaScript files; and when I rename or delete a Sass or CoffeeScript file, the CSS or JavaScript file is still there, lurking, potentially causing problems. Sure, I can delete these remnants by hand, but that makes refactoring a chore, and one of the most important things an application framework should do is make refactoring easy.]]></description>
			<content:encoded><![CDATA[<p>One annoyance I’ve had as a Rails developer is that when I use Sass, I end up with a bunch of CSS files; when I use CoffeeScript, I end up with a bunch of JavaScript files; and when I rename or delete a Sass or CoffeeScript file, the CSS or JavaScript file is <em>still there</em>, lurking, potentially causing problems. Sure, I can delete these remnants by hand, but that makes refactoring a chore, and one of the most important things an application framework should do is <strong>make refactoring easy</strong>.</p>

<p>So, I created a gem, <a href="http://github.com/TrevorBurnham/Chlorine" class="liexternal">Chlorine</a>. It’s very simple: With just 3 lines of configuration, you can say, “I want to delete everything but <code>lib/</code> and <code>sass/</code> in my <code>public/stylesheets</code> folder every time I start my server in <code>development</code>” mode. Pretty handy, I’d say. Use with caution, of course.</p>
]]></content:encoded>
			<wfw:commentRss>http://iterative.ly/2010/07/24/introducing-chlorine-keep-your-rails-app-clean/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Cross-browser rotations: The Sass Way</title>
		<link>http://iterative.ly/2010/07/02/cross-browser-rotations-the-sass-way/</link>
		<comments>http://iterative.ly/2010/07/02/cross-browser-rotations-the-sass-way/#comments</comments>
		<pubDate>Fri, 02 Jul 2010 22:03:59 +0000</pubDate>
		<dc:creator>Trevor Burnham</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[csssandpaper]]></category>
		<category><![CDATA[sass]]></category>

		<guid isPermaLink="false">http://iterative.ly/?p=241</guid>
		<description><![CDATA[If you're aiming an app at modern, non-IE browsers only, you can now rotate any HTML element an arbitrary number of degrees with a single line of Sass. Just use this handy function.]]></description>
			<content:encoded><![CDATA[<p>If you’re aiming an app at modern, non-IE browsers only, you can now rotate any HTML element an arbitrary number of degrees with a single line of Sass. Just use <a href="http://gist.github.com/461956" class="liexternal">this handy function</a>.</p>

<p>Now, why not IE? IE has actually had a lot of power for many years, in the form of filters, and it seems like you should be able to transform anything an arbitrary number of degrees using a Matrix Transform. But, I have no idea whether it’s even <em>possible</em> to do that in Sass given a number of degrees. The documentation baffles me. If you can find an example of using IE filters to do rotation to an arbitrary number of degrees (not just multiples of 90), please post a comment about it.</p>

<p>I should also mention the <a href="http://www.useragentman.com/blog/csssandpaper-a-css3-javascript-library/">cssSandpaper</a> library, which looks very exciting. You define a <code>-sand-transform</code> CSS property and it translates it to whatever the browser needs. <a href="http://www.useragentman.com/blog/2010/03/09/cross-browser-css-transforms-even-in-ie/" class="liexternal">Demo here</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://iterative.ly/2010/07/02/cross-browser-rotations-the-sass-way/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jQuery plugin: Is this element in the DOM?</title>
		<link>http://iterative.ly/2010/07/01/jquery-plugin-is-this-element-in-the-dom/</link>
		<comments>http://iterative.ly/2010/07/01/jquery-plugin-is-this-element-in-the-dom/#comments</comments>
		<pubDate>Thu, 01 Jul 2010 19:04:04 +0000</pubDate>
		<dc:creator>Trevor Burnham</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[coffeescript]]></category>
		<category><![CDATA[jquery]]></category>

		<guid isPermaLink="false">http://iterative.ly/?p=235</guid>
		<description><![CDATA[The extensive jQuery API makes it very easy to add and remove elements in the DOM, yet oddly, there’s no straightforward way to ask if a given jQuery element is in the DOM or not. I took the question to Stack Overflow, where I got a fine answer. Now I’ve wrapped that answer in a very short plugin, jqIsInDOM.]]></description>
			<content:encoded><![CDATA[<p>The extensive jQuery API makes it very easy to add and remove elements in the DOM, yet oddly, there’s no straightforward way to ask if a given jQuery element is in the DOM or not. I took the question to Stack Overflow, where I got <a href="http://stackoverflow.com/questions/3086068/how-do-i-check-whether-a-jquery-element-is-in-the-dom/3086084#3086084" class="liexternal">a fine answer</a>. Now I’ve wrapped that answer in a very short plugin (in fact, it could easily be shortened to 2 lines, but I find the 5-line version more readable), <a href="http://gist.github.com/460395" class="liexternal">jqIsInDOM</a>.</p>

<p>It adds two new jQuery functions, <code>isInDOM</code> and <code>notInDOM</code>, which do just what you’d expect. For instance, <code>$foo.remove().notInDOM()</code> is always <code>true</code>. Simple, right? I hope you find it useful.</p>
]]></content:encoded>
			<wfw:commentRss>http://iterative.ly/2010/07/01/jquery-plugin-is-this-element-in-the-dom/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>jQuery doAndBind</title>
		<link>http://iterative.ly/2010/06/20/jquery-doandbind/</link>
		<comments>http://iterative.ly/2010/06/20/jquery-doandbind/#comments</comments>
		<pubDate>Mon, 21 Jun 2010 00:10:30 +0000</pubDate>
		<dc:creator>Trevor Burnham</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://iterative.ly/?p=231</guid>
		<description><![CDATA[I created a simple little jQuery plugin to make my code just a bit more DRY. It adds a function called doAndBind to your jQuery objects. As the name suggests, doAndBind does two things: It executes the given functions right away, and it binds them to the given event.]]></description>
			<content:encoded><![CDATA[<p>When building web applications in jQuery, it’s common for me to want to do something twice: Once now, and once when some event is triggered. For instance, if I have to do custom layout relative to the window size, I need to do it once in <code>$(document).ready</code> and again in <code>$(window).resize</code>. That’s not so hard; just create a function and add it in both places. But if it’s just 1–2 lines of code, it’s annoying to have to either copy+paste or put that code in a named function.</p>

<p>So I created a simple little jQuery plugin to make my code just a bit more DRY. It adds a function called <code>doAndBind</code> to your jQuery objects. As the name suggests, <code>doAndBind</code> does two things: It executes the given functions right away, and it binds them to the given event. (Note that in jQuery, event functions like <code>click(func)</code> are just wrappers around <code>bind(eventType, func)</code>. <code>bind</code> is all you need.) For instance, with this plugin I can say (in CoffeeScript):</p>


<div class="wp_syntax"><div class="code"><pre class="coffeescript">$<span style="color: black;">&#40;</span>window<span style="color: black;">&#41;</span>.<span style="color: black;">doAndBind</span> <span style="color: #483d8b;">'resize'</span><span style="color: #66cc66;">,</span> <span style="color: #66cc66;">-&gt;</span>
 $<span style="color: black;">&#40;</span><span style="color: #483d8b;">'#stickyFooter'</span><span style="color: black;">&#41;</span>.<span style="color: black;">css</span> <span style="color: black;">&#123;</span>marginTop<span style="color: #66cc66;">:</span> $<span style="color: black;">&#40;</span>window<span style="color: black;">&#41;</span>.<span style="color: black;">height</span><span style="color: black;">&#40;</span><span style="color: black;">&#41;</span> <span style="color: #66cc66;">-</span> <span style="color: #ff4500;">100</span><span style="color: black;">&#125;</span></pre></div></div>


<p>Ah, sweet syntactic sugar.</p>

<p>The plugin should be capable of handling all of the forms of the <code>bind()</code> function described in the <a href="http://api.jquery.com/bind/" class="liexternal">API docs</a>, but please let me know if you encounter any problems. Here’s the download link: <a href="http://gist.github.com/446235" title="jqDoAndBind gist" class="liexternal">jqDoAndBind</a></p>
]]></content:encoded>
			<wfw:commentRss>http://iterative.ly/2010/06/20/jquery-doandbind/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>SVG: Beware Smooth Edges</title>
		<link>http://iterative.ly/2010/06/18/svg-beware-smooth-edges/</link>
		<comments>http://iterative.ly/2010/06/18/svg-beware-smooth-edges/#comments</comments>
		<pubDate>Fri, 18 Jun 2010 17:49:33 +0000</pubDate>
		<dc:creator>Trevor Burnham</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[raphaeljs]]></category>

		<guid isPermaLink="false">http://iterative.ly/?p=223</guid>
		<description><![CDATA[I spent an hour today trying to get Raphaël.js to draw straight, solid black lines, 1px thick. Yes, I am duly embarrassed. But it’s harder than it sounds.]]></description>
			<content:encoded><![CDATA[<p>I spent an hour today trying to get <a href="http://raphaeljs.com/" class="liexternal">Raphaël.js</a> to draw straight, solid black lines, 1px thick.</p>

<p>Yes, I am duly embarrassed. But it’s harder than it sounds.</p>

<p>By default, if you draw a 1px line in Raphaël, you get something fuzzy, thick and grey, even when you (superfluously) specify <code>stroke: 'black'</code> and <code>'stroke-width': 1</code>. It turns out that this is due to SVG’s subpixel precision and anti-aliasing gone awry, and that there are two fixes:</p>

<ol>
<li><strong>Shift the line by 0.5px.</strong> This is the approach that <a href="http://g.raphaeljs.com/" class="liexternal">gRaphaël</a> takes when it draws axes.</li>
<li><strong>Set the path’s shapeRendering to <code>'crispEdges'</code>.</strong> I discovered <a href="http://github.com/DmitryBaranovskiy/raphael/issues/#issue/164" title="Raphaël.js issue 164: SVG 1px line width fix" class="liexternal">this issue</a>, which suggested that it should be the default behavior for Raphaël.</li>
</ol>

<p>The latter option seems much cleaner to me, so I’m going with it for now. I’m a little wary, though, because why does Dmitry himself use Method #1? And what happens in IE?</p>
]]></content:encoded>
			<wfw:commentRss>http://iterative.ly/2010/06/18/svg-beware-smooth-edges/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>GeSHi and CoffeeScript</title>
		<link>http://iterative.ly/2010/06/09/geshi-and-coffeescript/</link>
		<comments>http://iterative.ly/2010/06/09/geshi-and-coffeescript/#comments</comments>
		<pubDate>Wed, 09 Jun 2010 12:52:39 +0000</pubDate>
		<dc:creator>Trevor Burnham</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[coffeescript]]></category>

		<guid isPermaLink="false">http://iterative.ly/?p=215</guid>
		<description><![CDATA[Yesterday I put together a new syntax for GeSHi, the PHP syntax highlighter, to add CoffeeScript support. Why? Well, GeSHi is used in many places, including the WP-syntax plugin that powers the code snippets in this blog.]]></description>
			<content:encoded><![CDATA[<p>Yesterday I put together a new syntax for <a href="http://geshi.org/" class="liexternal">GeSHi</a>, the PHP syntax highlighter, to add CoffeeScript support. Why? Well, GeSHi is used in many places, including the <a href="http://wordpress.org/extend/plugins/wp-syntax/" class="liwp">WP-syntax</a> plugin that powers the code snippets in this blog.</p>

<p>I’m rooting for CoffeeScript to become a mainstream language, and mainstream languages need an ecosystem of tools in order to catch on. There are already <a href="http://jashkenas.github.com/coffee-script/#resources" title="CoffeeScript resources" class="liexternal">plugins</a> for it in TextMate, Emacs, Vim and IntelliJ, and some cool stuff that makes using CoffeeScript in Rails apps a breeze.</p>

<p>The patch for CoffeeScript support in GeSHi can be downloaded from <a href="http://sourceforge.net/tracker/index.php?func=detail&#038;aid=3003823&#038;group_id=114997&#038;atid=670234" class="liexternal">this ticket</a>. I’m sure it’s not perfect (yet), but it’s one small step toward CoffeeScript world domination.</p>
]]></content:encoded>
			<wfw:commentRss>http://iterative.ly/2010/06/09/geshi-and-coffeescript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Sassy Block Comments</title>
		<link>http://iterative.ly/2010/06/04/sassy-block-comments-2/</link>
		<comments>http://iterative.ly/2010/06/04/sassy-block-comments-2/#comments</comments>
		<pubDate>Fri, 04 Jun 2010 20:59:46 +0000</pubDate>
		<dc:creator>Trevor Burnham</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[sass]]></category>

		<guid isPermaLink="false">http://iterative.ly/?p=210</guid>
		<description><![CDATA[Sass has been making my stylesheet work easier for a long while. But, a recent change to the block comment syntax cost me a couple of hours this morning.]]></description>
			<content:encoded><![CDATA[<p>Sass has been making my stylesheet work easier for a long while. But, a recent change to the block comment syntax cost me a couple of hours this morning. The following is a warning:</p>

<p>You see, in <a href="http://github.com/TrevorBurnham/twenty-sassy" title="Twenty Sassy theme for WordPress" class="liexternal">Twenty Sassy</a>, I had some block comments of the form</p>


<div class="wp_syntax"><div class="code"><pre class="css"><span style="color: #808080; font-style: italic;">/*
* LAYOUT: Two columns
* DESCRIPTION: Two-column fixed layout with one sidebar right of content
*/</span></pre></div></div>


<p>This was no problem in Sass 3.0.5, but under 3.0.9, it utterly broke the CSS. How? Well, instead of rendering as the CSS block comment you’d expect, it now renders as</p>


<div class="wp_syntax"><div class="code"><pre class="css"><span style="color: #808080; font-style: italic;">/*
 * LAYOUT: Two columns
 *
 * DESCRIPTION: Two-column fixed layout with one sidebar right of content
 *
 */</span> <span style="color: #00AA00;">*</span>/</pre></div></div>


<p>and that trailing <code>*/</code> wreaks utter havoc. You can have a block comment <code>/* like this */</code>, but you can’t put <code>*/</code> on its own line.</p>

<p>Next time, Sass, how about a compiler warning?</p>
]]></content:encoded>
			<wfw:commentRss>http://iterative.ly/2010/06/04/sassy-block-comments-2/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Refactor My Stylesheets: WordPress 3.0 Edition</title>
		<link>http://iterative.ly/2010/05/28/twenty-sassy/</link>
		<comments>http://iterative.ly/2010/05/28/twenty-sassy/#comments</comments>
		<pubDate>Fri, 28 May 2010 17:18:16 +0000</pubDate>
		<dc:creator>Trevor Burnham</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[sass]]></category>
		<category><![CDATA[wordpress]]></category>

		<guid isPermaLink="false">http://iterative.ly/?p=206</guid>
		<description><![CDATA[With RC1 just out and the point release due within a month, WordPress 3.0 has a lot of cool stuff going for it. A ton of themes is not one of them; 3.0 breaks backward compatibility so thoroughly that your best bet may be to modify the new default theme, Twenty Ten, rather than trying to port a theme aimed at WordPress 2.x.]]></description>
			<content:encoded><![CDATA[<p>With RC1 just out and the point release due within a month, WordPress 3.0 has a <a href="http://codex.wordpress.org/Version_3.0" title="What’s new in WordPress 3.0?" class="liwp">lot of cool stuff</a> going for it. A ton of themes is not one of them; 3.0 breaks backward compatibility so thoroughly that your best bet may be to modify the new default theme, <a href="http://2010dev.wordpress.com/" title="WordPress Twenty Ten theme" class="liexternal">Twenty Ten</a>, rather than trying to port a theme aimed at WordPress 2.x.</p>

<p>But where to begin?  The Twenty Ten source is well-commented (good job, WP team), but it’s also verbose and unorganized. There’s no map telling you, “OK, if you want to change this, do that.”</p>

<p>So, let’s tweak the stylesheets. There are four of them: <code>style.css</code>, <code>editor-style.css</code>, <code>rtl.css</code> and <code>editor-style-rtl.css</code>. These last two are just adaptations of the first two for right-to-left text (e.g. Arabic); they’re used in place of the first two depending on your blog’s language setting. I’m going to ignore them for now. <code>editor-style.css</code> is, as it suggests, custom styles for the visual editor. So if you’re modifying Twenty Ten, you should start with <code>style.css</code> and then, once you’re happy with the aesthetic, update <code>editor-style.css</code> to match.</p>

<p>One problem: <code>style.css</code> is 1355 lines long! So I did a quick <code>sass-convert style.css style.sass</code>, which worked beautifully, but the result was still 1100 lines long. 1100 lines of styling!? What blog needs that? Inspired by Chris Eppstein’s <a href="http://chriseppstein.github.com/blog/2010/05/25/refactor-my-stylesheets-digg-edition/" class="liexternal">Refactor My Stylesheets: Digg Edition</a>, I decided to go through it, organizing and tightening the code while making it easier to understand and modify. The result, <a href="http://github.com/TrevorBurnham/Twenty-Sassy" title="Twenty Sassy – Sass port of Twenty Ten theme" class="liexternal">Twenty Sassy</a>, can be found on Github. All of the stylings have been preserved, as have the semantic comments (those that are meaningful to the <a href="http://wordpress.org/extend/themes/" class="liwp">WordPress Theme Directory</a>). More importantly, variables you’re likely to want to modify have been moved up to the top.</p>

<p>There’s still a lot of room for improvement, but it’s a decent first pass. Instructions can be found at the <a href="http://github.com/TrevorBurnham/Twenty-Sassy" title="Twenty Sassy – Sass port of Twenty Ten theme" class="liexternal">Github repo</a>. Enjoy, and please fork!</p>
]]></content:encoded>
			<wfw:commentRss>http://iterative.ly/2010/05/28/twenty-sassy/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Introducing JSON.coffee</title>
		<link>http://iterative.ly/2010/05/22/introducing-json-coffee/</link>
		<comments>http://iterative.ly/2010/05/22/introducing-json-coffee/#comments</comments>
		<pubDate>Sun, 23 May 2010 02:53:06 +0000</pubDate>
		<dc:creator>Trevor Burnham</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[coffeescript]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[json]]></category>

		<guid isPermaLink="false">http://iterative.ly/?p=196</guid>
		<description><![CDATA[Today I took the latest reference JSON parser/stringifier implementation from json.org and translated it into CoffeeScript. The result is about 50 lines shorter (excluding reductions made in the comments), bringing it down to a nicely perusable 250.]]></description>
			<content:encoded><![CDATA[<p>Today I took the <a href="http://www.json.org/json2.js" class="liexternal">latest reference JSON parser/stringifier implementation</a> from json.org and translated it into CoffeeScript. The result is about 50 lines shorter (excluding reductions made in the comments), bringing it down to a nicely perusable 250. The gains are mainly from punctuation reduction. For instance,</p>


<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">function</span> walk<span style="color: #009900;">&#40;</span>holder<span style="color: #339933;">,</span> key<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #003366; font-weight: bold;">var</span> k<span style="color: #339933;">,</span> v<span style="color: #339933;">,</span> value <span style="color: #339933;">=</span> holder<span style="color: #009900;">&#91;</span>key<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
  <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>value <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000066; font-weight: bold;">typeof</span> value <span style="color: #339933;">===</span> <span style="color: #3366CC;">'object'</span><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>k <span style="color: #000066; font-weight: bold;">in</span> value<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>Object.<span style="color: #660066;">hasOwnProperty</span>.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span>value<span style="color: #339933;">,</span> k<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        v <span style="color: #339933;">=</span> walk<span style="color: #009900;">&#40;</span>value<span style="color: #339933;">,</span> k<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>v <span style="color: #339933;">!==</span> undefined<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
          value<span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> v<span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
          <span style="color: #000066; font-weight: bold;">delete</span> value<span style="color: #009900;">&#91;</span>k<span style="color: #009900;">&#93;</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;">&#125;</span>
  <span style="color: #000066; font-weight: bold;">return</span> reviver.<span style="color: #660066;">call</span><span style="color: #009900;">&#40;</span>holder<span style="color: #339933;">,</span> key<span style="color: #339933;">,</span> value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>


<p>becomes</p>


<div class="wp_syntax"><div class="code"><pre class="ruby">walk: <span style="color:#006600; font-weight:bold;">&#40;</span>holder, key<span style="color:#006600; font-weight:bold;">&#41;</span> <span style="color:#006600; font-weight:bold;">-&gt;</span>
  value: holder<span style="color:#006600; font-weight:bold;">&#91;</span>key<span style="color:#006600; font-weight:bold;">&#93;</span>
  <span style="color:#9966CC; font-weight:bold;">if</span> value <span style="color:#006600; font-weight:bold;">&amp;&amp;</span> typeof value == <span style="color:#996600;">'object'</span>
    <span style="color:#9966CC; font-weight:bold;">for</span> k of value
      <span style="color:#9966CC; font-weight:bold;">if</span> <span style="color:#CC00FF; font-weight:bold;">Object</span>.<span style="color:#9900CC;">hasOwnProperty</span>.<span style="color:#9900CC;">call</span> value, k
        v = walk value, k
        <span style="color:#9966CC; font-weight:bold;">if</span> v != undefined
          value<span style="color:#006600; font-weight:bold;">&#91;</span>k<span style="color:#006600; font-weight:bold;">&#93;</span> = v
        <span style="color:#9966CC; font-weight:bold;">else</span>
          delete value<span style="color:#006600; font-weight:bold;">&#91;</span>k<span style="color:#006600; font-weight:bold;">&#93;</span>
  <span style="color:#0000FF; font-weight:bold;">return</span> reviver.<span style="color:#9900CC;">call</span> holder, key, value</pre></div></div>


<p>I haven’t tried to change the actual structure of the original code, but I may in the future. This was exceptionally readable, neatly commented JavaScript code in the first place; rewriting it was very straightforward.</p>

<p>This was mainly a learning exercise, but I’ve uploaded the results to Github as <a href="http://github.com/TrevorBurnham/JSON.coffee" class="liexternal">JSON.coffee</a>. Try it out, fork it, make it better. Perhaps it should be trimmed down to a tight stringification engine; jQuery gives you <a href="http://api.jquery.com/jQuery.parseJSON/" class="liexternal">JSON parsing</a>, but leaves you to look elsewhere when you need a string.</p>
]]></content:encoded>
			<wfw:commentRss>http://iterative.ly/2010/05/22/introducing-json-coffee/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>for x in JavaScript</title>
		<link>http://iterative.ly/2010/05/22/for-x-in-javascript/</link>
		<comments>http://iterative.ly/2010/05/22/for-x-in-javascript/#comments</comments>
		<pubDate>Sat, 22 May 2010 17:24:16 +0000</pubDate>
		<dc:creator>Trevor Burnham</dc:creator>
				<category><![CDATA[Uncategorized]]></category>
		<category><![CDATA[coffeescript]]></category>
		<category><![CDATA[javascript]]></category>

		<guid isPermaLink="false">http://iterative.ly/?p=173</guid>
		<description><![CDATA[One of the most glaring oddities in JavaScript syntax is its "for in" syntax. Internally, arrays in JavaScript are just hashes from numerical indices to values. This is a common point of confusion in JavaScript, and has led to several techniques to work around it]]></description>
			<content:encoded><![CDATA[<p>One of the most glaring oddities in JavaScript syntax is this:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">var</span> obj <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'A'</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'B'</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">3</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'C'</span><span style="color: #009900;">&#125;</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> x <span style="color: #000066; font-weight: bold;">in</span> obj<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>x<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">// Console output: 1, 2, 3</span></pre></div></div>


<p>Coming from Ruby or Java (5+), you’d expect the output to be <code>A, B, C</code>. (Python’s <code>for...in</code> syntax behaves the same way on dictionaries, though not on arrays.) That is, you expect <code>x in obj</code> to assign each <em>value</em> of <code>obj</code> to <code>x</code>. Instead, JavaScript iterates over each <em>key</em>. If this doesn’t worry you, consider this:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">var</span> arr <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'A'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'B'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'C'</span><span style="color: #009900;">&#93;</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> x <span style="color: #000066; font-weight: bold;">in</span> arr<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>x<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">// Console output: 0, 1, 2</span></pre></div></div>


<p>Do you see what’s going on here? Internally, arrays in JavaScript are just hashes from numerical indices to values. This is a common point of confusion in JavaScript, and has led to several techniques to work around it:</p>

<h3>1. Just use the key</h3>

<p>The most direct approach is to plug the key into the object to get each value:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">var</span> arr <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'A'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'B'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'C'</span><span style="color: #009900;">&#93;</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> x <span style="color: #000066; font-weight: bold;">in</span> arr<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>arr<span style="color: #009900;">&#91;</span>x<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">// Console output: A, B, C</span></pre></div></div>


<p>There are two problems with this: First, the code isn’t very clear to readers. It doesn’t say “I’m iterating over keys, but what I really care about are the values.” (Renaming <code>x</code> to <code>key</code> would help a bit.) Second, there’s a small performance hit from each <code>arr[x]</code> call; this might be an issue in performance-critical loops that reference the same value repeatedly. You can correct this by putting <code>var value = arr[x];</code> at the top of the loop.</p>

<h3>2. Use an auto-executing function</h3>

<p>A slightly more sophisticated approach is to define an anonymous function that takes the key from each loop iteration:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">var</span> arr <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">'A'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'B'</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">'C'</span><span style="color: #009900;">&#93;</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> x <span style="color: #000066; font-weight: bold;">in</span> arr<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span> <span style="color: #009900;">&#40;</span>value<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>value<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
  <span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>arr<span style="color: #009900;">&#91;</span>x<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
<span style="color: #006600; font-style: italic;">// Console output: A, B, C</span></pre></div></div>


<p>Of course, this is a lot of boilerplate. Which brings us to…</p>

<h3>3. Use jQuery</h3>

<p>jQuery lets you get rid of the confusing <code>for</code> syntax altogether by using the built-in <code>each</code> function, as follows:</p>


<div class="wp_syntax"><div class="code"><pre class="javascript"><span style="color: #003366; font-weight: bold;">var</span> obj <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span><span style="color: #CC0000;">1</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'A'</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">2</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'B'</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">3</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">'C'</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
$.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span>obj<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>key<span style="color: #339933;">,</span> value<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
  console.<span style="color: #660066;">log</span><span style="color: #009900;">&#40;</span>key <span style="color: #339933;">+</span> <span style="color: #3366CC;">':'</span> <span style="color: #339933;">+</span> value<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: #006600; font-style: italic;">// Console output: 1:A, 2:B, 3:C</span></pre></div></div>


<p>Now we’re talking! Just one line of code that lets us loop over both the keys and the values of any collection. Of course, we can do one better with…</p>

<h3>4. CoffeeScript</h3>

<p>In <a href="http://coffeescript.org" title="JavaScript meets Ruby and Python" class="liexternal">CoffeeScript</a>, the <code>for...in</code> syntax is reserved for arrays and iterates over values rather than keys, as you’d hope:</p>


<div class="wp_syntax"><div class="code"><pre class="ruby">arr: <span style="color:#006600; font-weight:bold;">&#91;</span><span style="color:#996600;">'A'</span>, <span style="color:#996600;">'B'</span>, <span style="color:#996600;">'C'</span><span style="color:#006600; font-weight:bold;">&#93;</span>
<span style="color:#9966CC; font-weight:bold;">for</span> value <span style="color:#9966CC; font-weight:bold;">in</span> arr
  console.<span style="color:#9900CC;">log</span> value
<span style="color:#008000; font-style:italic;"># Console output: A, B, C</span></pre></div></div>


<p>Beautiful! But what if we need to iterate over an object’s keys? Well, then we use <code>of</code> instead of <code>in</code>.</p>


<div class="wp_syntax"><div class="code"><pre class="ruby">obj: <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006666;">1</span>: <span style="color:#996600;">'A'</span>, <span style="color:#006666;">2</span>: <span style="color:#996600;">'B'</span>, <span style="color:#006666;">3</span>: <span style="color:#996600;">'C'</span><span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#9966CC; font-weight:bold;">for</span> key of obj
  console.<span style="color:#9900CC;">log</span> key
<span style="color:#008000; font-style:italic;"># Console output: 1, 2, 3</span></pre></div></div>


<p>And what if we’d like to get the keys and values at the same time? We just put two comma-separated variables before <code>of</code>:</p>


<div class="wp_syntax"><div class="code"><pre class="ruby">obj: <span style="color:#006600; font-weight:bold;">&#123;</span><span style="color:#006666;">1</span>: <span style="color:#996600;">'A'</span>, <span style="color:#006666;">2</span>: <span style="color:#996600;">'B'</span>, <span style="color:#006666;">3</span>: <span style="color:#996600;">'C'</span><span style="color:#006600; font-weight:bold;">&#125;</span>
<span style="color:#9966CC; font-weight:bold;">for</span> key, value of obj
  console.<span style="color:#9900CC;">log</span> key <span style="color:#006600; font-weight:bold;">+</span> <span style="color:#996600;">':'</span> <span style="color:#006600; font-weight:bold;">+</span> value
<span style="color:#008000; font-style:italic;"># Console output: 1:A, 2:B, 3:C</span></pre></div></div>


<p>No more function wrappers! Note that <code>for...in</code> is meaningless in CoffeeScript when applied to a non-array; you should nearly always use <code>for...of</code>.</p>

<h3>Concluding caveats</h3>

<p><code>for x in</code> has another JavaScript quirk you need to be aware of: It iterates over not only items attached to an object directly, via <code>obj.property =</code> or <code>obj['property'] =</code>, but also those added to its prototype, as in <code>obj.prototype.property =</code>. This is especially confusing if you use a library like Prototype that extends the built-in JavaScript types; for instance, because it adds functions to the <code>Array</code> prototype, using <code>for x in [1, 2, 3]</code> after including Prototype will iterates over more than 3 items!</p>

<p>jQuery’s <a href="http://api.jquery.com/jQuery.each/" class="liexternal"><code>each</code></a> is smart about this, using <a href="http://phrogz.net/js/hasOwnProperty.html" class="liexternal">hasOwnProperty</a> internally to filter out prototype items, as does CoffeeScript.</p>

<p><pre lang="Ruby"></pre></p>
]]></content:encoded>
			<wfw:commentRss>http://iterative.ly/2010/05/22/for-x-in-javascript/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
	</channel>
</rss>
