<?xml version="1.0" encoding="utf-8"?><feed xmlns="http://www.w3.org/2005/Atom"><title>Ricardo Tomasi</title><link href="https://ricar.do/atom.xml" rel="self"/><link href="https://ricar.do/"/><updated>2026-11-27T19:30:33Z</updated><id>https://ricar.do/</id><author><name>Ricardo Tomasi</name><email>ricardobeat@gmail.com</email></author><entry><title>Cloth simulation with XPBD</title><link href="https://ricar.do/2026/11/26/cloth-sim.html"/><updated>2026-11-26T00:00:00Z</updated><id>https://ricar.do/2026/11/26/cloth-sim.html</id><content type="html">&lt;p&gt;I&amp;rsquo;ve been writing a small native 3D engine called &lt;a href="https://github.com/ricardobeat/drie"&gt;drie&lt;/a&gt; for the past couple weeks. It uses &lt;a href="https://github.com/floooh/sokol"&gt;sokol_gfx&lt;/a&gt; for rendering, and I recently decided to take a stab at implementing a cloth simulation system.&lt;/p&gt;&#10;&lt;p&gt;It&amp;rsquo;s an &lt;a href="https://matthias-research.github.io/pages/publications/XPBD.pdf"&gt;XPBD&lt;/a&gt; (extended position-based dynamics) solver; the version you see here runs in compute shaders, but was originally running in the CPU — surprisingly the performance gap was not that large, but doing in the GPU makes it less susceptible to stutter.&lt;/p&gt;&#10;&lt;h2 id="features"&gt;Features&lt;/h2&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;deterministic solver (helpful for replaying game scenes)&lt;/li&gt;&#10;&lt;li&gt;crease preserving, so pleated curtains remember their shape&lt;/li&gt;&#10;&lt;li&gt;coarse simulation, fine display for efficiency (tunable)&lt;/li&gt;&#10;&lt;li&gt;two-way collision with objects and other cloth&lt;/li&gt;&#10;&lt;li&gt;aerodynamic drag&lt;/li&gt;&#10;&lt;li&gt;sleep - saves compute when nothing is going on&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;The two-way collision is pretty sweet - as far as I&amp;rsquo;ve researched, very few engines have it without add-ons. The &amp;ldquo;Handkerchief&amp;rdquo; demo below shows what is possible with that.&lt;/p&gt;&#10;&lt;h2 id="problem-1-tangles"&gt;Problem 1: tangles&lt;/h2&gt;&#10;&lt;p&gt;The tricky part is keeping the simulation from tangling itself, as point-based collision still lets individual points slip through gaps in the coarse mesh (aka the cloth folds onto itself irreversibly). After a lot of manual tuning, I&amp;rsquo;ve found a setup that seems to prevent them &lt;em&gt;most of the time&lt;/em&gt;, and the crease-remembering feature helps prevent these in the first place.&lt;/p&gt;&#10;&lt;p&gt;The same problem causes two sheets to &amp;ldquo;merge&amp;rdquo; together when laid on top of each other. To stop this, a &lt;a href="https://simonbw.com/pinball-performance-2/"&gt;spatial hash&lt;/a&gt; is used to compute collisions between cloth entities; points that come into the same region are pushed apart.&lt;/p&gt;&#10;&lt;div class="img-row"&gt;&#10; &lt;img src="/images/cloth-sim-tangle-1.jpg" alt="Cloth folding and tangling on itself" /&gt;&#10; &lt;img src="/images/cloth-sim-tangle-2.jpg" alt="Layered cloth sheets tangling together" /&gt;&#10;&lt;/div&gt;&#10;&lt;h2 id="problem-2-rips"&gt;Problem 2: rips&lt;/h2&gt;&#10;&lt;p&gt;In a similar fashion, points can slip through another object, especially sharp corners or thin features (the spacing between points is roughly 1.7cm in these demos). It is made worse by the fact the drag handles are implemented as a &lt;em&gt;tether&lt;/em&gt;, where other points of the cloth are connected directly to it and move in the direction of the pull. The &amp;ldquo;right&amp;rdquo; way to do it, allowing the force to cascade through each of the points, unfortunately makes everything feel like jello; more accurate cloth sims avoid this by calculating the entire thing on every frame, which is unfeasible in real-time.&lt;/p&gt;&#10;&lt;p&gt;&lt;img src="/images/cloth-sim-rip.jpg" alt="Cloth ripping through the corner of a table"&gt;&lt;/p&gt;&#10;&lt;p&gt;I am currently experiment with using SDFs (Signed Distance Fields - &lt;a href="https://www.rustadventure.dev/introduction-to-signed-distance-fields"&gt;short intro here&lt;/a&gt;) to replace the collision meshes, as they naturally round out all the corners, but it&amp;rsquo;s still an unresolved issue - try dragging the cloth across the small table in the furniture demo and you&amp;rsquo;ll see it.&lt;/p&gt;&#10;&lt;h2 id="demos"&gt;Demos&lt;/h2&gt;&#10;&lt;p&gt;These require a WebGPU enabled browser. Should work fine in Firefox, Chrome, Helium, Safari on Mac and iPhone. Otherwise click the &amp;ldquo;Video&amp;rdquo; option to see a short capture.&lt;/p&gt;&#10;&lt;h3 id="handkerchiefs"&gt;Handkerchiefs&lt;/h3&gt;&#10;&lt;p&gt;A good test for the self-collision. Also shows some of the engine&amp;rsquo;s features: sunlight, volumetric fog, dynamic shadows, contact shadows, ambient occlusion, displacement maps, and more.&lt;/p&gt;&#10;&lt;p&gt;Use &lt;code&gt;WASD + Q/E&lt;/code&gt; to move the camera, or click-and-hold with the right mouse button. Drag a cloth to pick it up.&lt;/p&gt;&#10;&lt;blockquote&gt;&#10;&lt;p&gt;or open this in &lt;a href="/cloth-sim/"&gt;fullscreen&lt;/a&gt;&lt;/p&gt;&#10;&lt;/blockquote&gt;&#10;&lt;p&gt;&lt;iframe-loader&#10;src="https://sites-drie-handkerchiefs-pvu87l.b-cdn.net/"&#10;title="Handkerchiefs cloth simulation"&#10;allow="fullscreen; keyboard-map; clipboard-read; clipboard-write; gamepad; pointer-lock"&#10;width="580"&gt;&#10;&lt;video src="https://sites-drie-handkerchiefs-pvu87l.b-cdn.net/handkerchiefs.mp4"&#10;poster="https://sites-drie-handkerchiefs-pvu87l.b-cdn.net/handkerchiefs.jpg"&#10;controls loop playsinline preload="none"&gt;&lt;/video&gt;&#10;&lt;/iframe-loader&gt;&lt;/p&gt;&#10;&lt;h3 id="furniture-drape"&gt;Furniture drape&lt;/h3&gt;&#10;&lt;p&gt;&lt;code&gt;R&lt;/code&gt; to restart, &lt;code&gt;B&lt;/code&gt; to switch between two bend models for the cloth. &amp;ldquo;Signed&amp;rdquo; mode holds up better (check the creases around the sofa), but is a bit more expensive to compute.&lt;/p&gt;&#10;&lt;p&gt;&lt;iframe-loader&#10;src="https://sites-drie-handkerchiefs-pvu87l.b-cdn.net/furniture.html"&#10;title="Furniture cloth simulation"&#10;allow="fullscreen; keyboard-map; clipboard-read; clipboard-write; gamepad; pointer-lock"&#10;width="580"&gt;&#10;&lt;video src="https://sites-drie-handkerchiefs-pvu87l.b-cdn.net/furniture.mp4"&#10;poster="https://sites-drie-handkerchiefs-pvu87l.b-cdn.net/furniture.jpg"&#10;controls loop playsinline preload="none"&gt;&lt;/video&gt;&#10;&lt;/iframe-loader&gt;&lt;/p&gt;&#10;&lt;h3 id="banners"&gt;Banners&lt;/h3&gt;&#10;&lt;p&gt;Click to fire off spheres, &lt;code&gt;C&lt;/code&gt; to cut them off.&lt;/p&gt;&#10;&lt;p&gt;&lt;iframe-loader&#10;src="https://sites-drie-handkerchiefs-pvu87l.b-cdn.net/banners.html"&#10;title="Banners cloth simulation"&#10;allow="fullscreen; keyboard-map; clipboard-read; clipboard-write; gamepad; pointer-lock"&#10;width="580"&gt;&#10;&lt;video src="https://sites-drie-handkerchiefs-pvu87l.b-cdn.net/banners.mp4"&#10;poster="https://sites-drie-handkerchiefs-pvu87l.b-cdn.net/banners.jpg"&#10;controls loop playsinline preload="none"&gt;&lt;/video&gt;&#10;&lt;/iframe-loader&gt;&lt;/p&gt;&#10;&lt;h2 id="tech"&gt;Tech&lt;/h2&gt;&#10;&lt;p&gt;All examples are written in &lt;a href="https://c3-lang.org"&gt;C3&lt;/a&gt;, and compiled for web using sokol&amp;rsquo;s WebGPU backend and the native &lt;code&gt;c3c&lt;/code&gt; compiler emscripten output.&lt;/p&gt;&#10;&lt;p&gt;I used Claude Code to write most of these (but not to write any part of this post 🤮), the engine itself is developed using a melting pot of LLMs. Lazy ideas like &amp;ldquo;can we sample a more coarse grid in a separate pass&amp;rdquo; become a 5 minute experiment instead of a days-long quest.&lt;/p&gt;&#10;</content></entry><entry><title>React Tutorial rewritten in plain javascript</title><link href="https://ricar.do/2013/06/07/react-tutorial-rewritten-in-plain-javascript.html"/><updated>2013-06-07T00:00:00Z</updated><id>https://ricar.do/2013/06/07/react-tutorial-rewritten-in-plain-javascript.html</id><content type="html">&lt;p&gt;Last week Facebook &amp;amp; Instagram launched the &lt;a href=""&gt;React&lt;/a&gt; framework and an accompanying &lt;a href="http://facebook.github.io/react/docs/tutorial.html"&gt;tutorial&lt;/a&gt;.&lt;/p&gt;&#10;&lt;p&gt;Developer Vlad Yazhbin decided to &lt;a href="https://medium.com/make-your-own-apps/e71bcedc36b"&gt;rewrite that using AngularJS&lt;/a&gt;. The end result is pretty neat, but if you&amp;rsquo;re like me you will not actually appreciate the HTML speaking for itself and doing all the hard work. So let&amp;rsquo;s see what that looks like in plain javascript. Here is the template:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-html" data-lang="html"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;comment-box&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Comments&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h1&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;comment-list&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;comment&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt; &lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;comment-author&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; {{author}}&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;h2&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt; &lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;comment-text&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;{{text}}&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;form&lt;/span&gt; &lt;span class="na"&gt;class&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;comment-form&amp;#34;&lt;/span&gt; &lt;span class="na"&gt;method&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;POST&amp;#34;&lt;/span&gt; &lt;span class="na"&gt;action&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;.&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;input&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;text&amp;#34;&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;author&amp;#34;&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;author&amp;#34;&lt;/span&gt; &lt;span class="na"&gt;required&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;input&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;text&amp;#34;&lt;/span&gt; &lt;span class="na"&gt;name&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;text&amp;#34;&lt;/span&gt; &lt;span class="na"&gt;id&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;text&amp;#34;&lt;/span&gt; &lt;span class="na"&gt;required&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;submit&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;Add comment&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;button&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;form&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;div&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;And this is the code rewritten in &lt;em&gt;VanillaJS&lt;/em&gt;. No dependencies, no DOM libraries, no template engines (kinda). It renders the comments, markdown (using &lt;a href="https://github.com/chjj/marked"&gt;marked&lt;/a&gt;), and handles new comments:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-javascript" data-lang="javascript"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;CommentBox&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;root&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;root&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;root&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;list&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;root&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;.comment-list&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;form&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;root&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;.comment-form&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;bindEvents&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;CommentBox&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;template&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nb"&gt;document&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;querySelector&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;.comment&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;innerHTML&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="nx"&gt;nanostache&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;str&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;replace&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="sr"&gt;/\{\{(\w+)\}\}/g&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;a&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;m&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;m&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;trim&lt;/span&gt;&lt;span class="p"&gt;()]&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="s1"&gt;&amp;#39;&amp;#39;&lt;/span&gt; &lt;span class="p"&gt;})&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;CommentBox&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;comment&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;comment&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;},&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nx"&gt;render&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;list&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;innerHTML&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;comment&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nx"&gt;comment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;marked&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;comment&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;nanostache&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;CommentBox&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;template&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;comment&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="nx"&gt;join&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;\n&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;},&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nx"&gt;bindEvents&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;self&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;form&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;form&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nx"&gt;form&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;addEventListener&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;submit&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;preventDefault&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nx"&gt;self&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nx"&gt;author&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;form&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;author&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="nx"&gt;form&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;text&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;value&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;}).&lt;/span&gt;&lt;span class="nx"&gt;render&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="kc"&gt;false&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;(In a real project you&amp;rsquo;d probably want to create separate &lt;code&gt;Comment&lt;/code&gt; and &lt;code&gt;CommentList&lt;/code&gt; models/views, use a real template engine or DOM manipulation, and put the templates in a &amp;lt;script&amp;gt; tag, but this works.)&lt;/p&gt;&#10;&lt;p&gt;It&amp;rsquo;s nearly the same number of lines as the Angular example, despite some extra work you&amp;rsquo;d probably delegate to DOM, events and templating libraries.&lt;/p&gt;&#10;&lt;h2 id="automatic-updates"&gt;Automatic updates&lt;/h2&gt;&#10;&lt;p&gt;The Angular demo finishes with using a &lt;code&gt;post&lt;/code&gt; to save the comment on the server, and a 5s polling interval for live updates, as does the original one. You can do that, but this is the future, let&amp;rsquo;s use websockets!&lt;/p&gt;&#10;&lt;p&gt;First create a connection:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-javascript" data-lang="javascript"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;ws&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;WebSocket&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;ws://&amp;#39;&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;location&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;hostname&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Then send data to the server when adding a new comment:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-javascript" data-lang="javascript"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="kd"&gt;function&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;comment&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;comment&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nx"&gt;ws&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;send&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;newcomment::&amp;#39;&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stringify&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;comment&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;And change the initialization code to fetch data from the server (it should send existing comments on connection), and listen for new items:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-javascript" data-lang="javascript"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="kd"&gt;var&lt;/span&gt; &lt;span class="nx"&gt;comments&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;CommentBox&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s1"&gt;&amp;#39;.comment-box&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;ws&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;onmessage&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kd"&gt;function&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;){&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="c1"&gt;// or use socket.io/sockjs&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nx"&gt;comments&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;add&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;JSON&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;parse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;e&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;})&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;There you go. A real-time, &lt;em&gt;reactive&lt;/em&gt; comment form with real-time updates &lt;em&gt;without any library whatsoever&lt;/em&gt;.&lt;/p&gt;&#10;&lt;h2 id="conclusions"&gt;Conclusions&lt;/h2&gt;&#10;&lt;p&gt;What does it mean?&lt;/p&gt;&#10;&lt;p&gt;I don&amp;rsquo;t really know. Maybe the point is, there is no point in comparing frameworks with small, synthetic examples.&lt;/p&gt;&#10;&lt;p&gt;Code like this, using plain javascript, can get hairy pretty fast. It will usually give birth to encapsulated common idioms and functionality, and will end up becoming it&amp;rsquo;s own little framework. Sure, it&amp;rsquo;s easier to start with because there is no learning curve, but if in the end it will need a considerable amount of knowledge to play with, how is it different from using a framework like Angular/Ember/React/Knockout from the start?&lt;/p&gt;&#10;&lt;p&gt;My opinion: when in doubt, use &lt;a href="http://backbonejs.org"&gt;Backbone&lt;/a&gt;. It&amp;rsquo;s small, focused and not much different from whatever flavor of MV* you were going to cook up by yourself, with the advantage of a huge community of developers, time-tested code and active development.&lt;/p&gt;&#10;</content></entry><entry><title>Debug de uma aplicação em CoffeeScript</title><link href="https://ricar.do/2013/01/05/debug-aplicacao-coffeescript-pt.html"/><updated>2013-01-05T00:00:00Z</updated><id>https://ricar.do/2013/01/05/debug-aplicacao-coffeescript-pt.html</id><content type="html">&lt;p&gt;O tópico de debug surge com frequência em discussões sobre CoffeeScript. Neste post vou dar um exemplo real de erros utilizando CoffeeScript e a dificuldade (dica: nenhuma) em rastrear a origem.&lt;/p&gt;&#10;&lt;p&gt;Este é um projeto de front-end em CoffeeScript, não é dos mais extensos (~1200 linhas) mas suficiente como exemplo. A aplicação está separada em uma dúzia de arquivos:&lt;/p&gt;&#10;&lt;p&gt;&lt;img src="/images/debug-projeto.png" alt="Estrutura de arquivos coffeescript"&gt;&lt;/p&gt;&#10;&lt;p&gt;Estes são compilados via &lt;code&gt;Cakefile&lt;/code&gt; utilizando &lt;a href="http://ricardobeat.github.com/cake-flour"&gt;flour&lt;/a&gt;:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-coffeescript" data-lang="coffeescript"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;task&lt;/span&gt; &lt;span class="s"&gt;&amp;#39;build:dev&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;-&amp;gt;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nv"&gt;flour.minifiers.js = &lt;/span&gt;&lt;span class="kc"&gt;null&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nx"&gt;bundle&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s"&gt;&amp;#39;scripts/util.coffee&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s"&gt;&amp;#39;scripts/api.coffee&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s"&gt;&amp;#39;scripts/modules.coffee&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s"&gt;&amp;#39;scripts/user.coffee&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s"&gt;&amp;#39;scripts/profile.coffee&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s"&gt;&amp;#39;scripts/location.coffee&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s"&gt;&amp;#39;scripts/activity.coffee&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s"&gt;&amp;#39;scripts/stats.coffee&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="s"&gt;&amp;#39;scripts/main.coffee&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="s"&gt;&amp;#39;resources/app.js&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Neste setup todos os arquivos &lt;code&gt;.coffee&lt;/code&gt; são compilados e concatenados em um único arquivo &lt;code&gt;resources/app.js&lt;/code&gt;, que é incluido no &lt;code&gt;&amp;lt;head&amp;gt;&lt;/code&gt; do HTML. Durante o desenvolvimento a task &lt;code&gt;cake watch&lt;/code&gt; se encarrega de re-compilar o JS a cada alteração.&lt;/p&gt;&#10;&lt;h2 id="erros-de-sintaxe"&gt;Erros de sintaxe&lt;/h2&gt;&#10;&lt;p&gt;Sabe aquele erro maldito de js que te faz voltar pro editor pra corrigir &lt;em&gt;um caractere&lt;/em&gt;? Seja um ponto-e-vírgula no lugar errado, chave não fechada, vírgula, etc, esses erros &lt;em&gt;praticamente não existem&lt;/em&gt; em coffeescript.&lt;/p&gt;&#10;&lt;p&gt;Em primeiro, porque a linguagem é muito simples e limpa, sem tantos caracteres de controle; segundo, porque a maioria desses errors são acusados na hora do compile, não da execução. Além disso as mensages de erro do compiler coffeescript são muito claras. Exemplo:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-coffeescript" data-lang="coffeescript"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;AppView&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;Backbone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;View&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="p"&gt;...&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nv"&gt;start: &lt;/span&gt;&lt;span class="nf"&gt;-&amp;gt;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nx"&gt;@loadTemplates&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="vi"&gt;@root = &lt;/span&gt;&lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;#main&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Consegue ver o erro? No momento em que o arquivo for salvo, a task &lt;code&gt;watch&lt;/code&gt; que está rodando no terminal acusa o erro:&lt;/p&gt;&#10;&lt;p&gt;&lt;img src="/images/debug-compile-error.png" alt="Erro de compilação"&gt;&lt;/p&gt;&#10;&lt;p&gt;Basta ir até a linha em questão e remover o parêntese extra.&lt;/p&gt;&#10;&lt;h2 id="erros-de-execução"&gt;Erros de execução&lt;/h2&gt;&#10;&lt;p&gt;Os erros de execução são a questão que mais preocupa quem ainda não usou coffeescript: receio de não conseguir identificar a linha de código que gerou o erro, pelo fato de o stack trace ser indicado em cima do Javascript compilado, não do fonte.&lt;/p&gt;&#10;&lt;p&gt;Vamos ver o quão difícil é na prática. Como exemplo vou usar esta função de inicialização do app:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-coffeescript" data-lang="coffeescript"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nx"&gt;AppView&lt;/span&gt; &lt;span class="k"&gt;extends&lt;/span&gt; &lt;span class="nx"&gt;Backbone&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;View&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nv"&gt;initialize: &lt;/span&gt;&lt;span class="nf"&gt;-&amp;gt;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nx"&gt;@connect&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="vi"&gt;@currentUser = &lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;Model&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nv"&gt;key: &lt;/span&gt;&lt;span class="nx"&gt;session&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;get&lt;/span&gt; &lt;span class="s"&gt;&amp;#39;userkey&amp;#39;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Agora vamos inserir um erro, uma tentativa de acesso a uma propriedade não-existente no objeto &lt;code&gt;User&lt;/code&gt;:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-coffeescript" data-lang="coffeescript"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nv"&gt;initialize: &lt;/span&gt;&lt;span class="nf"&gt;-&amp;gt;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nx"&gt;@connect&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="vi"&gt;@currentUser = &lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;User&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;model&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nv"&gt;key: &lt;/span&gt;&lt;span class="nx"&gt;session&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;get&lt;/span&gt; &lt;span class="s"&gt;&amp;#39;userkey&amp;#39;&lt;/span&gt; &lt;span class="p"&gt;}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;(se você não percebeu o erro: &lt;code&gt;Model&lt;/code&gt; -&amp;gt; &lt;code&gt;model&lt;/code&gt;)&lt;/p&gt;&#10;&lt;p&gt;Nesse caso a compilação ocorre normalmente - para o compiler o código está ok, ele não tem conhecimento sobre os objetos no momento da execução. Ao abrir o browser porém a aplicação não carregou:&lt;/p&gt;&#10;&lt;p&gt;&lt;img src="/images/debug-blank.png" alt="Tela branca"&gt;&lt;/p&gt;&#10;&lt;p&gt;&lt;code&gt;Uncaught TypeError: undefined is not a function [app.js:1450]&lt;/code&gt;.&lt;/p&gt;&#10;&lt;p&gt;&amp;ldquo;&lt;em&gt;Linha 1450?? Jamais vou encontrar esse erro.&lt;/em&gt;&amp;rdquo;. Na prática o fluxo é o mesmo de debugar javascript. Abra o stack:&lt;/p&gt;&#10;&lt;p&gt;&lt;img src="/images/debug-stack.png" alt="Stack trace"&gt;&lt;/p&gt;&#10;&lt;p&gt;A função culpada é &lt;code&gt;App.initialize&lt;/code&gt;. É só abrir o arquivo &lt;code&gt;app.coffee&lt;/code&gt;, encontrar a definição da função (por memória ou &lt;code&gt;Cmd+F initialize:&lt;/code&gt;) e pronto! Se tu está boiando no projeto e não faz a menor idéia de em que arquivo a função está, bom&amp;hellip; vai de &lt;code&gt;Find in project&lt;/code&gt; ou pede ajuda pra um colega.&lt;/p&gt;&#10;&lt;p&gt;&lt;img src="/images/debug-file.png" alt="Tela branca"&gt;&lt;/p&gt;&#10;&lt;h2 id="source-maps"&gt;Source maps&lt;/h2&gt;&#10;&lt;p&gt;Erros de lógica exigem reler/interpretar o trecho de código para encontrar o erro - portanto não há nenhuma necessidade de abrir o arquivo &lt;code&gt;app.js&lt;/code&gt; compilado. Sendo um pré-processador, é por definição impossível que o compiler CoffeeScript gere erros de lógica (&lt;em&gt;impossível&lt;/em&gt; nesse contexto == mais de 5200 linhas de testes).&lt;/p&gt;&#10;&lt;p&gt;No exemplo acima, a única diferença em relação a debugar javascript é não termos a linha exata que gerou o erro. Chrome e Firefox já estão implementando uma feature chamada &lt;strong&gt;source maps&lt;/strong&gt;, que permite ao console mapear logs à linha de código do arquivo fonte, tanto para javascript (coffeescript, typescript, etc) quanto css (LESS, Sass, Stylus, etc).&lt;/p&gt;&#10;&lt;p&gt;Infelizmente a versão atual do coffeescript (1.4.0) não gera source maps, mas um rewrite &lt;a href="https://github.com/michaelficarra/CoffeeScriptRedux"&gt;já está em andamento&lt;/a&gt; e deve se tornar o compiler oficial nos próximos meses.&lt;/p&gt;&#10;&lt;h2 id="nodejs"&gt;Node.js&lt;/h2&gt;&#10;&lt;p&gt;Ao instalar o coffeescript na sua máquina com &lt;code&gt;npm install coffee-script -g&lt;/code&gt;, o executável &lt;code&gt;coffee&lt;/code&gt; é adicionado ao seu path. Ele nada mais é do que uma invocação do &lt;code&gt;node&lt;/code&gt; com o módulo &lt;code&gt;coffee-script&lt;/code&gt; carregado. Ou seja, &lt;code&gt;coffee meuapp.coffee&lt;/code&gt; é o equivalente a executar:&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;require('coffee-script') // registra .coffee no require.extensions&#10;require('./meuapp')&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;Este padrão é comum no deployment de aplicações em serviços como Heroku, dotCloud, nodejitsu, etc - em geral se exige um arquivo &lt;code&gt;server/main/app.js&lt;/code&gt; para inicialização.&lt;/p&gt;&#10;&lt;p&gt;Como exemplo vou usar o código do &lt;a href="http://playjetcraft.net/"&gt;jetcraft&lt;/a&gt;, jogo que desenvolvi junto com o &lt;a href="http://twitter.com/vitor42"&gt;@vitor42&lt;/a&gt; para o Node Knockout 2013:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-coffeescript" data-lang="coffeescript"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;# app.coffee&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nv"&gt;express = &lt;/span&gt;&lt;span class="nx"&gt;require&lt;/span&gt; &lt;span class="s"&gt;&amp;#39;express&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nv"&gt;http = &lt;/span&gt;&lt;span class="nx"&gt;require&lt;/span&gt; &lt;span class="s"&gt;&amp;#39;http&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nv"&gt;app = &lt;/span&gt;&lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;configure&lt;/span&gt; &lt;span class="nf"&gt;-&amp;gt;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;use&lt;/span&gt; &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;router&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;use&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;static&lt;/span&gt; &lt;span class="s"&gt;&amp;#34;&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="nx"&gt;__dirname&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;/public&amp;#34;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;configure&lt;/span&gt; &lt;span class="s"&gt;&amp;#39;development&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;-&amp;gt;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;use&lt;/span&gt; &lt;span class="nx"&gt;express&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;errorHandler&lt;/span&gt; &lt;span class="nv"&gt;dumpExceptions: &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;showStack: &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;app&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;get&lt;/span&gt; &lt;span class="s"&gt;&amp;#39;/&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;(req, res) -&amp;gt;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nx"&gt;res&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;render&lt;/span&gt; &lt;span class="s"&gt;&amp;#39;index&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;nada&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Note que a variável &lt;code&gt;nada&lt;/code&gt; não foi definida. Como lá em cima ativamos o &lt;code&gt;express.errorHandler&lt;/code&gt; com dump e showStack, o erro é enviado para browser além de impresso no console:&lt;/p&gt;&#10;&lt;p&gt;&lt;img src="/images/debug-node-browser.png" alt="Erro do Express no browser"&gt;&lt;/p&gt;&#10;&lt;p&gt;&lt;img src="/images/debug-node-coffee.png" alt="Erro no terminal"&gt;&lt;/p&gt;&#10;&lt;p&gt;Em ambos os casos o erro indica o arquivo &lt;code&gt;.coffee&lt;/code&gt; que originou o erro:&lt;/p&gt;&#10;&lt;pre&gt;&lt;code&gt;ReferenceError: nada is not defined&#10; at /Users/ricardobeat/Projects/jetcraft/app.coffee:42:32&#10;&lt;/code&gt;&lt;/pre&gt;&#10;&lt;p&gt;Assim, não é estritamente necessário ter um script de build ou tarefas de compile/watch. É util no caso de escrever código para ser compartilhado, como uma biblioteca ou módulo do NPM, para distribuição sem a dependência do módulo coffee-script.&lt;/p&gt;&#10;&lt;h2 id="conclusões"&gt;Conclusões&lt;/h2&gt;&#10;&lt;p&gt;Não tenha medo de utilizar CoffeeScript pela questão de debug. As vantagens da linguagem compensam e muito esse detalhe, que está por ser corrigido em breve. Ainda mais considerando o uso de TDD, build scripts também para o front-end (reduzindo o tamanho de cada source), e os padrões atuais de código, não ter a linha exata de um runtime error é a menor das preocupações.&lt;/p&gt;&#10;&lt;p&gt;Aprenda mais sobre CoffeeScript no &lt;a href="http://coffeescript.org"&gt;site oficial&lt;/a&gt; ou com os livros (grátis):&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;a href="http://arcturo.github.com/library/coffeescript/"&gt;The Little Book on CoffeeScript&lt;/a&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="http://autotelicum.github.com/Smooth-CoffeeScript/interactive/interactive-coffeescript.html"&gt;Smooth CoffeeScript&lt;/a&gt;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;&lt;a href="http://twitter.com/ricardobeat"&gt;Siga no twitter: @ricardobeat&lt;/a&gt;&lt;/p&gt;&#10;</content></entry><entry><title>Implementing the chirp protocol using WebAudio</title><link href="https://ricar.do/2012/12/30/implementing-the-chirp-protocol-using-webaudio.html"/><updated>2012-12-30T00:00:00Z</updated><id>https://ricar.do/2012/12/30/implementing-the-chirp-protocol-using-webaudio.html</id><content type="html">&lt;p&gt;Yesterday I went to check out &lt;a href="http://chirp.io"&gt;chirp.io&lt;/a&gt; to see if they had any news on their&#10;API. &lt;strong&gt;Chirp&lt;/strong&gt; is an app/protocol to transmit small bits of data using sound. It works&#10;remarkably well, and is as easy and magical to use as &lt;strong&gt;Bu.mp&lt;/strong&gt;, with much simpler&#10;technology underneath.&lt;/p&gt;&#10;&lt;p&gt;&lt;img src="/images/chirp_home.jpg" alt="Chirp.io homepage"&gt;&lt;/p&gt;&#10;&lt;p&gt;Seeing that they still don&amp;rsquo;t have an API, I decided to find out how hard it was&#10;to generate a chirp myself. Fortunately they have a detailed enough description of their&#10;protocol at &lt;a href="http://chirp.io/tech"&gt;chirp.io/tech/&lt;/a&gt;.&lt;/p&gt;&#10;&lt;p&gt;So here is how you can reverse-engineer (not quite) a chirp.&lt;/p&gt;&#10;&lt;h2 id="the-protocol"&gt;The protocol&lt;/h2&gt;&#10;&lt;p&gt;The chirp protocol is very simple. A message consists of 20 characters: 2 for a&#10;&amp;ldquo;front door&amp;rdquo; code (a chirp identifier), 10 characters of actual payload, plus 8&#10;characters or error-correction codes. The character table has 32 entries, from 0-9 to a-v.&lt;/p&gt;&#10;&lt;p&gt;First thing was to figure out all the frequencies. Each character corresponds to a note/frequency,&#10;separated by a &lt;em&gt;semitone&lt;/em&gt;, that go from 1760Hz (A6) to 10548Hz (E9). You can see&#10;a chart with notes and corresponding frequencies &lt;a href="http://people.virginia.edu/~pdr4h/pitch-freq.html"&gt;here&lt;/a&gt;.&lt;/p&gt;&#10;&lt;p&gt;A semitone is defined by &lt;code&gt;s = 1.05946311&lt;/code&gt;. This results in an exponential&#10;distribution of frequencies. To go up a semitone you multiply the previous frequency&#10;by &lt;code&gt;s&lt;/code&gt;, or do &lt;code&gt;Math.pow(s, n_semitones)&lt;/code&gt;.&lt;/p&gt;&#10;&lt;p&gt;With that in mind, we can generate a map of the corresponding tones:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-coffeescript" data-lang="coffeescript"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nv"&gt;semitone = &lt;/span&gt;&lt;span class="mf"&gt;1.05946311&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nv"&gt;baseFrequency = &lt;/span&gt;&lt;span class="mi"&gt;1760&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nv"&gt;characters = &lt;/span&gt;&lt;span class="s"&gt;&amp;#39;0123456789abcdefghijklmnopqrstuv&amp;#39;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nv"&gt;freqCodes = &lt;/span&gt;&lt;span class="p"&gt;{}&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nv"&gt;frequencies = &lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="c1"&gt;# Generate the frequencies that correspond to each code point.&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="nx"&gt;char&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;characters&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nv"&gt;freq = &lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;baseFrequency&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;pow&lt;/span&gt; &lt;span class="nx"&gt;semitone&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;toFixed&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nx"&gt;freqCodes&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;char&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;freq&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nx"&gt;frequencies&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="nx"&gt;freq&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cm"&gt;###&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cm"&gt; 0: 1760&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cm"&gt; 1: 1864.655&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cm"&gt; 2: 1975.533&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cm"&gt; 3: 2093.005&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cm"&gt; 4: 2217.461&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cm"&gt; 5: ...&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="cm"&gt;###&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;These frequencies do not match exactly to the chart, I&amp;rsquo;m not sure if due to floating&#10;point errors or bad charts, but they are close enough. For better precision just&#10;create a map of the values.&lt;/p&gt;&#10;&lt;h2 id="audio-generation"&gt;Audio generation&lt;/h2&gt;&#10;&lt;p&gt;To generate the audio, we&amp;rsquo;re just going to use a simple oscillator pumping out&#10;sine waves. The official chirps have a bit more going on that makes them sound more&#10;pleasant and bird-like, but this doesn&amp;rsquo;t seem to be necessary for the transmission&#10;to work.&lt;/p&gt;&#10;&lt;p&gt;First, create a new &lt;code&gt;AudioContext&lt;/code&gt;:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-coffeescript" data-lang="coffeescript"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nv"&gt;context = &lt;/span&gt;&lt;span class="k"&gt;new&lt;/span&gt; &lt;span class="nx"&gt;AudioContext&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Then an oscillator and a gain node:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-coffeescript" data-lang="coffeescript"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nv"&gt;oscillator = &lt;/span&gt;&lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createOscillator&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nv"&gt;oscillator.type = &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="c1"&gt;# sine wave&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nv"&gt;gainNode = &lt;/span&gt;&lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;createGainNode&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nv"&gt;gainNode.gain.value = &lt;/span&gt;&lt;span class="mf"&gt;0.5&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;And finally connect everything:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-coffeescript" data-lang="coffeescript"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;oscillator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;connect&lt;/span&gt; &lt;span class="nx"&gt;gainNode&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;gainNode&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;connect&lt;/span&gt; &lt;span class="nx"&gt;context&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;destination&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;Now we have our audio pipeline setup. If you call &lt;code&gt;oscillator.start(0)&lt;/code&gt; you should get&#10;a steady 440Hz tone.&lt;/p&gt;&#10;&lt;p&gt;A naive approach to playing out the tones would be trying to change the &lt;code&gt;oscillator.frequency&lt;/code&gt;&#10;value on the fly, maybe like this:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-coffeescript" data-lang="coffeescript"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nv"&gt;i = &lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nv"&gt;chirp = &lt;/span&gt;&lt;span class="nx"&gt;do&lt;/span&gt; &lt;span class="nf"&gt;-&amp;gt;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nv"&gt;oscillator.frequency.value = &lt;/span&gt;&lt;span class="nx"&gt;frequencies&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;++&lt;/span&gt;&lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;frequencies&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nx"&gt;setTimeout&lt;/span&gt; &lt;span class="nx"&gt;chirp&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;100&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;But that will actually generate a sweeping tone - the oscillator has a default&#10;ramp value that you can&amp;rsquo;t change. What you can do is pre-program it using the &lt;a href="https://dvcs.w3.org/hg/audio/raw-file/tip/webaudio/specification.html#AudioParam"&gt;AudioParam&lt;/a&gt; interface; it&amp;rsquo;s the standard way&#10;to automate parameters in WebAudio. For simplicity we can use the &lt;code&gt;setValueAtTime&lt;/code&gt; method,&#10;which sets a parameter to an exact value at a defined point in time.&lt;/p&gt;&#10;&lt;p&gt;Assuming &lt;code&gt;chirp&lt;/code&gt; contains the 20-char long string to be transmitted, we can loop over&#10;the characters and define the 20 tone sequence:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-coffeescript" data-lang="coffeescript"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="nx"&gt;char&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;chirp&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="nx"&gt;oscillator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;frequency&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;setValueAtTime&lt;/span&gt; &lt;span class="nx"&gt;freqCodes&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;char&lt;/span&gt;&lt;span class="p"&gt;],&lt;/span&gt; &lt;span class="nx"&gt;beepLength&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;And stop the oscillator after the full chirp length, otherwise it keeps going with&#10;the last frequency:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-coffeescript" data-lang="coffeescript"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;oscillator&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;stop&lt;/span&gt; &lt;span class="nx"&gt;beepLength&lt;/span&gt; &lt;span class="o"&gt;/&lt;/span&gt; &lt;span class="mi"&gt;1000&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;chirp&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;h2 id="payload"&gt;Payload&lt;/h2&gt;&#10;&lt;p&gt;In theory the 10-character payload could be anything (within the provided table),&#10;providing up to 50 bits. The Chirp app just generates a unique ID, which points to the&#10;actual resource online. Even if you increase the chirp length by orders of magnitude you&amp;rsquo;re&#10;still not going to get enough bandwith to send pictures over soundwaves :(&lt;/p&gt;&#10;&lt;p&gt;The final 8 characters of error-correction are generated by a &lt;a href="http://en.wikipedia.org/wiki/Reed%E2%80%93Solomon_error_correction"&gt;Reed-Solomon&lt;/a&gt; algorithm. It&amp;rsquo;s the same&#10;error-correction used in CDs, DVDs, barcodes, satellites and space probes. That means it is complex&#10;enough that there are very few implementations around, none of them in javascript.&lt;/p&gt;&#10;&lt;p&gt;The RS scheme depends on something called a &lt;a href="http://en.wikipedia.org/wiki/Finite_field"&gt;Galois Field&lt;/a&gt;. According to my very&#10;limited math exploration, these are fields that have the curious and useful property that every operation between two elements results in a value that is also an element of the field. They come in powers of two; probably the reason why the Chirp guys chose a table size 32 - 16 is too low density, 256 would result in notes way too close for reliable detection.&lt;/p&gt;&#10;&lt;p&gt;Most implementations of these work with 8-bit symbols, using a GF(2^8), but we need a GF(2^5). I found a &lt;a href="https://github.com/brownan/Reed-Solomon"&gt;python implementation of Reed-Solomon&lt;/a&gt;, but failed to convert it to 5-bit space. An easier way might be to compile a C implementation using Emscripten to run in the browser.&lt;/p&gt;&#10;&lt;p&gt;For now, we can simply grab a ready-made chirp code for testing. This is what one looks like:&lt;/p&gt;&#10;&lt;p&gt;&lt;img src="/images/chirp_soundbooth.png" alt="A chirp&amp;rsquo;s spectrum"&gt;&lt;/p&gt;&#10;&lt;p&gt;Since I didn&amp;rsquo;t have time to write a pitch-detector + decoder (maybe tomorrow before breakfast?) I used a slightly&#10;low-tech approach to decode a message &lt;a href="http://chirp.io/made-of-sound-1/"&gt;from this blog post&lt;/a&gt;:&lt;/p&gt;&#10;&lt;p&gt;&lt;img src="/images/chirp_decoding.png" alt="Decoding a chirp"&gt;&lt;/p&gt;&#10;&lt;p&gt;The code is &lt;code&gt;hjsrg00lgbif4c6u07sq&lt;/code&gt;. It can error-correct up to 5 characters, so feel free to experiment with it - for instance &lt;code&gt;hjsrg00lgbif4c600000&lt;/code&gt; seems to work fine in a low noise environment.&lt;/p&gt;&#10;&lt;p&gt;So now we just need to feed that code and set the frequencies in the oscillator. Here is the working chirp player using the WebAudio API - open the Chirp app on your phone and press the yellow button:&lt;/p&gt;&#10;&lt;div id="chirp-demo" style="width:300px; margin:0 auto;" class="block"&gt;&#10; &lt;script src="http://ricardobeat.github.io/chirpweb/assets/chirp.js"&gt;&lt;/script&gt;&#10; &lt;style scoped&gt;&#10; .chirp {&#10; display: block;&#10; height: 40px;&#10; width: 300px;&#10; font-size: 20px;&#10; background: #ff2;&#10; background-image: -webkit-linear-gradient(#ff1, #aa0);&#10; border: 0;&#10; border-radius: 4px;&#10; cursor: pointer;&#10; }&#10; img {&#10; margin-bottom: 0;&#10; }&#10; .chirp:hover {&#10; background: #ee2;&#10; }&#10; &lt;/style&gt;&#10; &lt;img src="http://chirp.io/assets/made_of_sound_2" width="300" /&gt;&#10; &lt;button class="chirp" onclick="chirp('srg00lgbif', '4c6u07sq')"&gt;♮&lt;/button&gt;&#10;&lt;/div&gt;&#10;&lt;p&gt;And the original audio for comparison:&lt;/p&gt;&#10;&lt;div class="block"&gt; &#10; &lt;audio controls&gt;&#10; &lt;source src="http://chirp.io/chirps/made_of_sound_2.mp3" /&gt;&#10; &lt;source src="http://chirp.io/chirps/made_of_sound_2.ogg" /&gt;&#10; &lt;/audio&gt;&#10;&lt;/div&gt;&#10;&lt;p&gt;If you have any suggestions on how to implement the error correction, I&amp;rsquo;d love to hear them. Goals for the future:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;implement Reed-Solomon error-correcting codes in javascript&lt;/li&gt;&#10;&lt;li&gt;detector + decoder (wireless p2p data using open web APIs!)&lt;/li&gt;&#10;&lt;li&gt;get closer to chirp&amp;rsquo;s sound signature&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;And don&amp;rsquo;t forget to check out &lt;a href="http://chirp.io"&gt;chirp.io&lt;/a&gt;!&lt;/p&gt;&#10;&lt;p&gt;&lt;a href="@ricardobeat"&gt;Follow me on twitter&lt;/a&gt;.&lt;/p&gt;&#10;</content></entry><entry><title>It's time for a native EventEmitter</title><link href="https://ricar.do/2012/08/09/its-time-for-a-native-eventemitter.html"/><updated>2012-08-09T00:00:00Z</updated><id>https://ricar.do/2012/08/09/its-time-for-a-native-eventemitter.html</id><content type="html">&lt;p&gt;Every developer working with javascript has used events hundreds of times. More like thousands. Or millions. Events are the base for UI interaction on the web, and with the popularity of Node.JS and evented frameworks before it, are now a staple in the backend too. If you have a written a decently-sized web site or app in the past years, chances are you&amp;rsquo;ve used a custom event emitter or the observer pattern in your code many times.&lt;/p&gt;&#10;&lt;p&gt;Event-driven architectures are, in my opinion, the most natural way to deal with async code. They free you from cross-referencing objects and methods all over, allowing for really decoupled modules, that don&amp;rsquo;t need to know details about other parts of the application. As soon as your code starts to grow and you have to handle UI events, XHR requests, dynamic asset loading, you either&lt;/p&gt;&#10;&lt;pre style="font-family:sans-serif; font-size:14px; margin-top:-1em;"&gt;&#10; enter&#10; callback&#10; hell,&#10; make an entangled chain of method references, or do the sane thing: resort to events.&#10;&lt;/pre&gt;&#10;&lt;p&gt;The proof is in the massive number of event emitter implementations; Backbone has its own, as does Spine, jQuery (it can handle object events too) and most other client frameworks/libraries:&lt;/p&gt;&#10;&lt;p&gt;(links point to code)&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;a href="https://github.com/angular/angular.js/blob/master/src/ng/rootScope.js"&gt;AngularJS&lt;/a&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://github.com/maccman/spine/blob/master/src/spine.coffee"&gt;SpineJS&lt;/a&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://github.com/documentcloud/backbone/blob/master/backbone.js#L61"&gt;Backbone&lt;/a&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://github.com/jquery/jquery/blob/master/src/event.js"&gt;jQuery&lt;/a&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://github.com/Shopify/batman/blob/master/src/event_emitter/event_emitter.coffee"&gt;BatmanJS&lt;/a&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://github.com/madrobby/zepto/blob/master/src/event.js"&gt;Zepto&lt;/a&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://github.com/flosse/scaleApp/blob/master/src/Mediator.coffee"&gt;ScaleApp&lt;/a&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://github.com/hay/stapes/blob/master/stapes.js#L186"&gt;Stapes&lt;/a&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://github.com/kbjr/Events.js/blob/master/events.js#L850"&gt;Events-js&lt;/a&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://github.com/fat/bean/blob/master/src/bean.js#L378"&gt;Bean/Ender&amp;rsquo;s Jeesh&lt;/a&gt;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;And of course, a million stand-alone implementations:&lt;/p&gt;&#10;&lt;ul&gt;&#10;&lt;li&gt;&lt;a href="https://github.com/Wolfy87/EventEmitter/blob/master/src/EventEmitter.js"&gt;EventEmitter&lt;/a&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://github.com/hij1nx/EventEmitter2/blob/master/lib/eventemitter2.js"&gt;EventEmitter2&lt;/a&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://github.com/daniellmb/MinPubSub/blob/master/minpubsub.src.js"&gt;MinPubSub&lt;/a&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://github.com/jgnewman/shotgun/blob/master/shotgun.js"&gt;ShotGun&lt;/a&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://github.com/federico-lox/pubsub.js/blob/master/src/pubsub.js"&gt;pubsub.js&lt;/a&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://github.com/azer/observer/blob/master/lib/observer.js"&gt;observer&lt;/a&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://github.com/rpflorence/snack/blob/master/src/event.js"&gt;snackJS&lt;/a&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://github.com/pazguille/jvent/blob/master/jvent.js"&gt;jvent&lt;/a&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://github.com/dperrymorrow/callbacks.js/blob/master/callbacks.coffee"&gt;callbacks.js&lt;/a&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://github.com/millermedeiros/js-signals/blob/master/dev/src/Signal.js"&gt;SignalJS&lt;/a&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://github.com/mkuklis/asEvented/blob/master/asevented.js"&gt;asEvented&lt;/a&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://bitbucket.org/killdream/ekho/src/ebfc23d7ed02/src/ekho.js"&gt;ekho.js&lt;/a&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://github.com/jeromeetienne/microevent.js/blob/master/microevent.js"&gt;microevent.js&lt;/a&gt;&lt;/li&gt;&#10;&lt;li&gt;&lt;a href="https://github.com/uxder/Radio/blob/master/radio.js"&gt;radio.js&lt;/a&gt;&lt;/li&gt;&#10;&lt;/ul&gt;&#10;&lt;p&gt;Each of these has a slightly different opinion on how things should work, but EventEmitter2 is arguably the canonical implementation - it&amp;rsquo;s the most complete, battle-hardened and the base for node.js&amp;rsquo;s EventEmitter.&lt;/p&gt;&#10;&lt;p&gt;In the end, they all implement a simple three-method API: &lt;code&gt;addListener&lt;/code&gt;, &lt;code&gt;removeListener&lt;/code&gt; and &lt;code&gt;trigger&lt;/code&gt;. In fact, most of them are functionally equivalent and just differ on method naming: &lt;em&gt;bind|add|listen|on&lt;/em&gt;, &lt;em&gt;unbind|remove|off&lt;/em&gt; and &lt;em&gt;trigger|fire|dispatch|emit&lt;/em&gt;.&lt;/p&gt;&#10;&lt;p&gt;(I have to say I&amp;rsquo;m not very fond of the &lt;em&gt;on/off&lt;/em&gt; naming convention. &amp;ldquo;on&amp;rdquo; was used as a preposition in the original DOM events like &lt;code&gt;onclick&lt;/code&gt;, not as an adverb (being &amp;ldquo;on/off&amp;rdquo;). That&amp;rsquo;s annoying.)&lt;/p&gt;&#10;&lt;p&gt;We should end this mess and have &lt;code&gt;EventEmitter&lt;/code&gt; as a native object. What do you think?&lt;/p&gt;&#10;&lt;iframe src="http://thatsjustlikeyouropinion.info/on/nativeEventEmitter/Agreed/You're stupid" frameborder="0" width="190" height="30" style="vertical-align:bottom;"&gt;&lt;/iframe&gt;</content></entry><entry><title>What should I listen to?</title><link href="https://ricar.do/2011/06/03/what-should-i-listen-to.html"/><updated>2011-06-03T00:00:00Z</updated><id>https://ricar.do/2011/06/03/what-should-i-listen-to.html</id><content type="html">&lt;blockquote&gt;&#10;&lt;p&gt;Do you frequently open Grooveshark/Pandora/iTunes/etc and just stare at it while trying to decide what to listen next?&lt;/p&gt;&#10;&lt;/blockquote&gt;&#10;&lt;p&gt;I do it all the time. I probably visit my own &lt;a href="http://last.fm/user/superbife"&gt;last.fm profile&lt;/a&gt; a dozen times a week, just so I can take a look at my library and pick a familiar name to put into the search field (yeah, I dumped Last.FM radio for Grooveshark - I like to hear whole albums).&lt;/p&gt;&#10;&lt;p&gt;Enter &lt;a href="http://listen.to.ricardo.cc/"&gt;What should I listen to?&lt;/a&gt;, &lt;em&gt;the dumbest web service ever&lt;/em&gt;. It takes a last.fm username and a click, and gives back lots of love in the form of good music recommendations (actually these recommendations come from yourself, but you gotta give it some credit&amp;hellip;)&lt;/p&gt;&#10;&lt;p&gt;I had a lot of fun building it:&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;runs on node.js, courtesy of &lt;a href="http://nodester.com"&gt;nodester&lt;/a&gt;&lt;/li&gt;&#10;&lt;li&gt;IDs are saved in localStorage, and your library&amp;rsquo;s size in your cookies (we need to know the range to shoot at), so no database.&lt;/li&gt;&#10;&lt;li&gt;It has no images &lt;em&gt;per se&lt;/em&gt;, the noisy background is generated via &lt;del&gt;Javascript&lt;/del&gt; CoffeeScript and the rest is CSS.&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;p&gt;It&amp;rsquo;s not mobile-ready, though that should be easy to accomplish. Source code is &lt;a href="http://github.com/ricardobeat/what-should-i-listen-to"&gt;on github&lt;/a&gt;.&lt;/p&gt;&#10;&lt;p&gt;I hope it&amp;rsquo;s useful for a few people out there. If you have any suggestions you can find me &lt;a href="http://twitter.com/ricardobeat"&gt;on Twitter&lt;/a&gt;.&lt;/p&gt;&#10;</content></entry><entry><title>10 CoffeeScript One Liners to Impress Your Friends</title><link href="https://ricar.do/2011/06/02/10-coffeescript-one-liners-to-impress-your-friends.html"/><updated>2011-06-02T00:00:00Z</updated><id>https://ricar.do/2011/06/02/10-coffeescript-one-liners-to-impress-your-friends.html</id><content type="html">&lt;p&gt;You may have read &lt;a href="http://mkaz.com/solog/10-scala-one-liners-to-impress-your-friends"&gt;&amp;ldquo;10 Scala One Liners to Impress Your Friends&amp;rdquo;&lt;/a&gt; at &lt;a href="http://solog.co"&gt;Marcus Kazmierczak&amp;rsquo;s blog&lt;/a&gt; recently featured on &lt;a href="http://news.ycombinator.com"&gt;HN&lt;/a&gt;. Although I don&amp;rsquo;t know Scala (or Java), it all looks quite nice, so I decided to impress my friends too - folks go from Java to Scala, we go from Javascript to CoffeeScript. Assume &lt;a href="http://nodejs.org"&gt;node.js&lt;/a&gt; as the environment for all examples.&lt;/p&gt;&#10;&lt;ol&gt;&#10;&lt;li&gt;Multiply each item in a list by 2&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;hr&gt;&#10;&lt;p&gt;Marcus starts by showing off the &lt;code&gt;map&lt;/code&gt; function. We can do exactly the same using a &lt;em&gt;range literal&lt;/em&gt; and an anonymous function:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-coffeescript" data-lang="coffeescript"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;..&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt; &lt;span class="nf"&gt;(i) -&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;but we can also write the more expressive form&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-coffeescript" data-lang="coffeescript"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt; &lt;span class="mi"&gt;2&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;..&lt;/span&gt;&lt;span class="mi"&gt;10&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;ol start="2"&gt;&#10;&lt;li&gt;Sum a list of numbers&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;hr&gt;&#10;&lt;p&gt;Javascript (and CoffeeScript by extension) also has native &lt;a href="http://en.wikipedia.org/wiki/Map_%28higher-order_function%29"&gt;map&lt;/a&gt; and &lt;a href="http://en.wikipedia.org/wiki/Fold_%28higher-order_function%29"&gt;reduce&lt;/a&gt; functions:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-coffeescript" data-lang="coffeescript"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;..&lt;/span&gt;&lt;span class="mi"&gt;1000&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;reduce&lt;/span&gt; &lt;span class="nf"&gt;(t, s) -&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;t&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="nx"&gt;s&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;(&lt;code&gt;reduce&lt;/code&gt; == &lt;code&gt;reduceLeft&lt;/code&gt;, &lt;code&gt;reduceRight&lt;/code&gt; is also available)&lt;/p&gt;&#10;&lt;ol start="3"&gt;&#10;&lt;li&gt;Verify if word exists in string&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;hr&gt;&#10;&lt;p&gt;Too easy since we have the &lt;code&gt;some&lt;/code&gt; method. It returns true if any of the elements in the array satisfies the function:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-coffeescript" data-lang="coffeescript"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nv"&gt;wordList = &lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;coffeescript&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;&amp;#34;eko&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;&amp;#34;play framework&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;&amp;#34;and stuff&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="s"&gt;&amp;#34;falsy&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nv"&gt;tweet = &lt;/span&gt;&lt;span class="s"&gt;&amp;#34;This is an example tweet talking about javascript and stuff.&amp;#34;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;wordList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;some&lt;/span&gt; &lt;span class="nf"&gt;(word) -&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;~&lt;/span&gt;&lt;span class="nx"&gt;tweet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;indexOf&lt;/span&gt; &lt;span class="nx"&gt;word&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;This will return the matched words instead:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-coffeescript" data-lang="coffeescript"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;wordList&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;filter&lt;/span&gt; &lt;span class="nf"&gt;(word) -&amp;gt;&lt;/span&gt; &lt;span class="o"&gt;~&lt;/span&gt;&lt;span class="nx"&gt;tweet&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;indexOf&lt;/span&gt; &lt;span class="nx"&gt;word&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;code&gt;~&lt;/code&gt; is not a special operator in CoffeeScript, just a dirty trick. It is the &lt;a href="https://developer.mozilla.org/en/JavaScript/Reference/Operators/Bitwise_Operators"&gt;bitwise NOT&lt;/a&gt; operator, which inverts the bits of it&amp;rsquo;s operand. In practice it equates to &lt;code&gt;-x-1&lt;/code&gt;. Here it works on the basis that we want to check for an index greater than &lt;code&gt;-1&lt;/code&gt;, and &lt;code&gt;-(-1)-1 == 0&lt;/code&gt; evaluates to false.&lt;/p&gt;&#10;&lt;ol start="4"&gt;&#10;&lt;li&gt;Read in a File&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;hr&gt;&#10;&lt;p&gt;Users of client-side javascript frameworks will be familiar with this idea:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-coffeescript" data-lang="coffeescript"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;readFile&lt;/span&gt; &lt;span class="s"&gt;&amp;#39;data.txt&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;(err, data) -&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;fileText = &lt;/span&gt;&lt;span class="nx"&gt;data&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;You could also use the synchronous version:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-coffeescript" data-lang="coffeescript"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nv"&gt;fileText = &lt;/span&gt;&lt;span class="nx"&gt;fs&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;readFileSync&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;data.txt&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;toString&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;In node.js land this is &lt;strong&gt;only acceptable for application start-up&lt;/strong&gt; routines. You should use the async version in your code.&lt;/p&gt;&#10;&lt;ol start="5"&gt;&#10;&lt;li&gt;Happy Birthday&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;hr&gt;&#10;&lt;p&gt;First, a 1 to 1 mapping to the Scala version with a bit of string interpolation thrown in the mix:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-coffeescript" data-lang="coffeescript"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;..&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;map&lt;/span&gt; &lt;span class="nf"&gt;(i) -&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt; &lt;span class="s"&gt;&amp;#34;Happy Birthday &amp;#34;&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;is&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt; &lt;span class="s"&gt;&amp;#34;dear Robert&amp;#34;&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="s"&gt;&amp;#34;to You&amp;#34;&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;But it get&amp;rsquo;s better. This reads almost like pseudo-code:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-coffeescript" data-lang="coffeescript"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;console&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;log&lt;/span&gt; &lt;span class="s"&gt;&amp;#34;Happy Birthday &lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;is&lt;/span&gt; &lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt; &lt;span class="s"&gt;&amp;#34;dear Robert&amp;#34;&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="s"&gt;&amp;#34;to You&amp;#34;&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;..&lt;/span&gt;&lt;span class="mi"&gt;4&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;ol start="6"&gt;&#10;&lt;li&gt;Filter list of numbers&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;hr&gt;&#10;&lt;p&gt;Filter a list of numbers into two categories. The literate way:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-coffeescript" data-lang="coffeescript"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nx"&gt;score&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;passed&lt;/span&gt; &lt;span class="o"&gt;or&lt;/span&gt; &lt;span class="nv"&gt;passed = &lt;/span&gt;&lt;span class="p"&gt;[])&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;failed&lt;/span&gt; &lt;span class="o"&gt;or&lt;/span&gt; &lt;span class="nv"&gt;failed = &lt;/span&gt;&lt;span class="p"&gt;[])).&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt; &lt;span class="nx"&gt;score&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="nx"&gt;score&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;49&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;58&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;76&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;82&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;88&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;90&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;(thanks &lt;a href="http://twitter.com/giacecco"&gt;@giacecco&lt;/a&gt; for shortening this)&lt;/p&gt;&#10;&lt;p&gt;And the functional way:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-coffeescript" data-lang="coffeescript"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nx"&gt;passed&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nx"&gt;failed&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;49&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;58&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;76&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;82&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;88&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;90&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nx"&gt;reduce&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;(p,c,i) -&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="o"&gt;+&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;c&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="mi"&gt;60&lt;/span&gt;&lt;span class="p"&gt;)].&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt; &lt;span class="nx"&gt;c&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="p"&gt;[[],[]]&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;ol start="7"&gt;&#10;&lt;li&gt;Fetch and Parse a XML web service&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;hr&gt;&#10;&lt;p&gt;XML what? Haven&amp;rsquo;t heard of it. Let&amp;rsquo;s fetch a JSON instead, using the &lt;a href="http://github.com/mikeal/request"&gt;request&lt;/a&gt; library:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-coffeescript" data-lang="coffeescript"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;request&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;get&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="nx"&gt;uri&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;path/to/api.json&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nv"&gt;json: &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt; &lt;span class="p"&gt;},&lt;/span&gt; &lt;span class="nf"&gt;(err, r, body) -&amp;gt;&lt;/span&gt; &lt;span class="nv"&gt;results = &lt;/span&gt;&lt;span class="nx"&gt;body&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;ol start="8"&gt;&#10;&lt;li&gt;Find minimum (or maximum) in a List&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;hr&gt;&#10;&lt;p&gt;The &lt;code&gt;apply&lt;/code&gt; function comes handy here. It allows you to call a function passing an array as the list of arguments: &lt;code&gt;Math.max&lt;/code&gt; and &lt;code&gt;Math.min&lt;/code&gt; both receive a variable number of arguments, i.e. &lt;code&gt;Math.max 30, 10, 20&lt;/code&gt; returns &lt;code&gt;30&lt;/code&gt;. Let&amp;rsquo;s put it to work with an array:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-coffeescript" data-lang="coffeescript"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;max&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;apply&lt;/span&gt; &lt;span class="nx"&gt;@&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;14&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;35&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;46&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;98&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c1"&gt;# 98&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nb"&gt;Math&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;min&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;apply&lt;/span&gt; &lt;span class="nx"&gt;@&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;14&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;35&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;7&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;46&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="mi"&gt;98&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="c1"&gt;# -7&#10;&lt;/span&gt;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;ol start="9"&gt;&#10;&lt;li&gt;Paralell Processing&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;hr&gt;&#10;&lt;p&gt;Not there yet. You can create &lt;a href="http://nodejs.org/docs/v0.4.8/api/child_processes.html"&gt;child processes&lt;/a&gt; on your own and communicate with them, or use the &lt;a href="https://github.com/pgriess/node-webworker"&gt;WebWorkers API implementation&lt;/a&gt;. Skipping over.&lt;/p&gt;&#10;&lt;ol start="10"&gt;&#10;&lt;li&gt;Sieve of Eratosthenes&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;hr&gt;&#10;&lt;p&gt;&lt;strong&gt;update&lt;/strong&gt;: &lt;a href="http://twitter.com/dionyziz"&gt;@dionyziz&lt;/a&gt; sent me this compact version:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-coffeescript" data-lang="coffeescript"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nv"&gt;primes = &lt;/span&gt;&lt;span class="p"&gt;[]&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nx"&gt;primes&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;..&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="o"&gt;not&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;primes&lt;/span&gt; &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="o"&gt;%&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;length&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;which we can then use for a real one-liner like the original:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-coffeescript" data-lang="coffeescript"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nf"&gt;(n) -&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;..&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="o"&gt;not&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt; &lt;span class="o"&gt;or&lt;/span&gt; &lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[])&lt;/span&gt; &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;)[&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;])&lt;/span&gt; &lt;span class="o"&gt;and&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;p&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;or the somewhat more efficient&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-coffeescript" data-lang="coffeescript"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nf"&gt;(n) -&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;push&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;2&lt;/span&gt;&lt;span class="p"&gt;..&lt;/span&gt;&lt;span class="nx"&gt;n&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;when&lt;/span&gt; &lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nx"&gt;p&lt;/span&gt; &lt;span class="o"&gt;or&lt;/span&gt; &lt;span class="nx"&gt;p&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="p"&gt;[]).&lt;/span&gt;&lt;span class="nx"&gt;some&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nf"&gt;(j) -&amp;gt;&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="nx"&gt;j&lt;/span&gt; &lt;span class="o"&gt;is&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;))&lt;/span&gt; &lt;span class="o"&gt;and&lt;/span&gt; &lt;span class="nx"&gt;n&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="nx"&gt;p&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;ol start="11"&gt;&#10;&lt;li&gt;Bonus&lt;/li&gt;&#10;&lt;/ol&gt;&#10;&lt;hr&gt;&#10;&lt;p&gt;Most readable fizzbuzz version you&amp;rsquo;ll ever see:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-coffeescript" data-lang="coffeescript"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="s"&gt;&amp;#34;&lt;/span&gt;&lt;span class="si"&gt;#{&lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt; &lt;span class="o"&gt;is&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt; &lt;span class="s"&gt;&amp;#39;fizz&amp;#39;&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="s"&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class="si"&gt;}#{&lt;/span&gt;&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt; &lt;span class="o"&gt;is&lt;/span&gt; &lt;span class="mi"&gt;0&lt;/span&gt; &lt;span class="k"&gt;then&lt;/span&gt; &lt;span class="s"&gt;&amp;#39;buzz&amp;#39;&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="s"&gt;&amp;#39;&amp;#39;&lt;/span&gt;&lt;span class="si"&gt;}&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;&lt;/span&gt; &lt;span class="o"&gt;or&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;..&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;strong&gt;edit:&lt;/strong&gt; even simpler, but trickier, with &lt;a href="https://github.com/jashkenas/coffee-script/issues/1406#issuecomment-1293309"&gt;a little hint by satyr&lt;/a&gt;:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-coffeescript" data-lang="coffeescript"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;fizz&amp;#39;&lt;/span&gt; &lt;span class="k"&gt;unless&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="mi"&gt;3&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;+&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;buzz&amp;#39;&lt;/span&gt; &lt;span class="k"&gt;unless&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt;&lt;span class="o"&gt;%&lt;/span&gt;&lt;span class="mi"&gt;5&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;or&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="nx"&gt;i&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;&lt;span class="p"&gt;..&lt;/span&gt;&lt;span class="mi"&gt;100&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;When you use the &lt;code&gt;+&lt;/code&gt; operator on an Array, it converts it to a string. &lt;code&gt;[].toString()&lt;/code&gt; is the same as &lt;code&gt;[].join(',')&lt;/code&gt;, which gives an empty string in case the array value is &lt;code&gt;undefined&lt;/code&gt; or &lt;code&gt;null&lt;/code&gt;. This also works in Javascript (&lt;code&gt;[undefined] + &amp;quot;b&amp;quot; === &amp;quot;b&amp;quot;&lt;/code&gt;).&lt;/p&gt;&#10;&lt;h2 id="conclusions"&gt;Conclusions&lt;/h2&gt;&#10;&lt;p&gt;Modern languages are amazingly expressive. I&amp;rsquo;m also surprised that some of the syntax in these map so closely to Scala, given they&amp;rsquo;re oceans apart.&lt;/p&gt;&#10;&lt;p&gt;You can &lt;a href="http://jashkenas.github.com/coffee-script/"&gt;learn more about CoffeeScript here&lt;/a&gt;, see a few more CoffeeScript snippets on &lt;a href="http://rosettacode.org/wiki/Category:CoffeeScript"&gt;rosettacode&lt;/a&gt;, and follow me on Twitter &lt;a href="http://twitter.com/ricardobeat"&gt;@ricardobeat&lt;/a&gt;.&lt;/p&gt;&#10;</content></entry><entry><title>Localization on node.js + express + jquery templates</title><link href="https://ricar.do/2011/04/28/localization-on-nodejs-express-jquery-templates.html"/><updated>2011-04-28T00:00:00Z</updated><id>https://ricar.do/2011/04/28/localization-on-nodejs-express-jquery-templates.html</id><content type="html">&lt;p&gt;I just published &lt;code&gt;jqtpl-express-i18n&lt;/code&gt;, a module for translations using &lt;a href="https://github.com/kof/node-jqtpl"&gt;jquery templates&lt;/a&gt; and &lt;a href="http://expressjs.com"&gt;express&lt;/a&gt;. It takes a string from the .html template and looks for a suitable translation according to the received &lt;code&gt;Accept-Language&lt;/code&gt; header.&lt;/p&gt;&#10;&lt;p&gt;My first idea was to add a getter called .i18n to &lt;code&gt;String.prototype&lt;/code&gt; using &lt;strong&gt;defineGetter&lt;/strong&gt;:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-coffeescript" data-lang="coffeescript"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="nb"&gt;String&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;prototype&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nx"&gt;__defineGetter__&lt;/span&gt; &lt;span class="s"&gt;&amp;#39;i18n&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nf"&gt;() -&amp;gt;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt; &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nx"&gt;strings&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="s"&gt;&amp;#39;pt&amp;#39;&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="k"&gt;this&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="k"&gt;this&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;And this in the templates:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-html" data-lang="html"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;${&amp;#34;Look ma I&amp;#39;m being translated&amp;#34;.i18n}&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;p&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;&lt;em&gt;Magic.&lt;/em&gt;&lt;/p&gt;&#10;&lt;p&gt;This didn&amp;rsquo;t work though, because you can&amp;rsquo;t access the request&amp;rsquo;s scope from the getter. I ended up extending jQuery templates after following &lt;a href="https://gist.github.com/726057"&gt;this example&lt;/a&gt;, and it turned out to be much cleaner. Now my templates look like this:&lt;/p&gt;&#10;&lt;div class="highlight"&gt;&lt;pre tabindex="0" class="chroma"&gt;&lt;code class="language-html" data-lang="html"&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;ul&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#9;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;li&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;{[e &amp;#34;Something&amp;#34;}}&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;li&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&#9;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;li&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;{[e &amp;#34;Another something&amp;#34;}}&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;li&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;&amp;lt;/&lt;/span&gt;&lt;span class="nt"&gt;ul&lt;/span&gt;&lt;span class="p"&gt;&amp;gt;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;span class="line"&gt;&lt;span class="cl"&gt;&lt;span class="p"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nt"&gt;input&lt;/span&gt; &lt;span class="na"&gt;type&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;submit&amp;#34;&lt;/span&gt; &lt;span class="na"&gt;value&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s"&gt;&amp;#34;{[e &amp;#39;submit&amp;#39;}}&amp;#34;&lt;/span&gt; &lt;span class="p"&gt;/&amp;gt;&lt;/span&gt;&#10;&lt;/span&gt;&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;&lt;p&gt;(ignore the bracket, &lt;a href="http://daringfireball.net/projects/markdown/"&gt;markdown&lt;/a&gt; can&amp;rsquo;t handle the double braces)&lt;/p&gt;&#10;&lt;p&gt;I wish I could just use &lt;code&gt;{[&amp;quot;text here&amp;quot;}}&lt;/code&gt; - but I&amp;rsquo;d have to break into node-jqtpl&amp;rsquo;s house and mess with it&amp;rsquo;s privates, that wouldn&amp;rsquo;t be nice. Right?&lt;/p&gt;&#10;&lt;p&gt;I&amp;rsquo;m currently rewriting it to use .json files and update strings automagically, hope to get v0.2 up soon. You can get it from npm or &lt;a href="https://github.com/ricardobeat/jqtpl-express-i18n"&gt;github&lt;/a&gt;. Questions or issues go to &lt;a href="https://github.com/ricardobeat/jqtpl-express-i18n"&gt;https://github.com/ricardobeat/jqtpl-express-i18n&lt;/a&gt;.&lt;/p&gt;&#10;</content></entry></feed>