<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>Caffeinated Simpleton&#187; Development</title>
	<atom:link href="http://justin.harmonize.fm/index.php/category/development/feed/" rel="self" type="application/rss+xml" />
	<link>http://justin.harmonize.fm</link>
	<description>A cup of coffee and a soapbox is like a bottle of Jack and a gun.</description>
	<lastBuildDate>Mon, 15 Feb 2010 08:06:40 +0000</lastBuildDate>
	<generator>http://wordpress.org/?v=2.9.2</generator>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
			<item>
		<title>Cobra is Ready to Release</title>
		<link>http://justin.harmonize.fm/index.php/2010/02/cobra-is-ready-to-release/</link>
		<comments>http://justin.harmonize.fm/index.php/2010/02/cobra-is-ready-to-release/#comments</comments>
		<pubDate>Mon, 15 Feb 2010 08:06:40 +0000</pubDate>
		<dc:creator>justin</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Cobra]]></category>
		<category><![CDATA[JavaScript]]></category>

		<guid isPermaLink="false">http://justin.harmonize.fm/?p=346</guid>
		<description><![CDATA[I did a little work on Cobra today and pushed the code around a little bit. Things that changed:

Reorganized into a single closure. This helps keep internal details internal.
Minimize with Closure Compiler
JSLint now passes

I definitely recommend the JSLint VIM plugin if you&#8217;re a vimmer.


Works in node.js (and most likely any server-side JavaScript implementation)
Works in Internet [...]]]></description>
			<content:encoded><![CDATA[<p>I did a little work on Cobra today and pushed the code around a little bit. Things that changed:</p>
<ul>
<li>Reorganized into a single closure. This helps keep internal details internal.</li>
<li>Minimize with <a href="http://code.google.com/closure/compiler/" target="_blank">Closure Compiler</a></li>
<li><a href="http://www.jslint.com/" target="_blank">JSLint</a> now passes
<ul>
<li>I definitely recommend the <a href="http://github.com/hallettj/jslint.vim" target="_blank">JSLint VIM plugin</a> if you&#8217;re a vimmer.</li>
</ul>
</li>
<li>Works in <a href="http://nodejs.org/" target="_blank">node.js</a> (and most likely any server-side JavaScript implementation)</li>
<li>Works in Internet Explorer.
<ul>
<li>I had never really tested this, but after fixing the things that JSLint found, the tests just passed.</li>
<li>Still need to test in IE 6. I don&#8217;t have a machine with IE 6 unfortunately.</li>
</ul>
</li>
<li>Added to documentation to clarify a few things.</li>
</ul>
<p>At this point, I think the library&#8217;s safe for others to use. I tagged a 0.5 version, and I&#8217;ll be calling it 1.0 after it gets into a few more projects. Feel free to try it out!</p>
<p>You can find <a href="http://github.com/jmtulloss/cobra">Cobra on github</a>, or you can <a href="http://github.com/jmtulloss/cobra/zipball/0.5">download version 0.5 now</a> (<a href="http://github.com/jmtulloss/cobra/tarball/0.5">tgz</a>).</p>
]]></content:encoded>
			<wfw:commentRss>http://justin.harmonize.fm/index.php/2010/02/cobra-is-ready-to-release/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>An Introduction to JavaScript&#8217;s &#8220;this&#8221;</title>
		<link>http://justin.harmonize.fm/index.php/2009/09/an-introduction-to-javascripts-this/</link>
		<comments>http://justin.harmonize.fm/index.php/2009/09/an-introduction-to-javascripts-this/#comments</comments>
		<pubDate>Sun, 27 Sep 2009 01:27:43 +0000</pubDate>
		<dc:creator>justin</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[this]]></category>

		<guid isPermaLink="false">http://justin.harmonize.fm/?p=298</guid>
		<description><![CDATA[JavaScript is an amazing little language, but it&#8217;s got some quirks that turn a lot of people off. One of those quirks is this, and how it&#8217;s not necessarily what you expect it to be. this isn&#8217;t that complicated, but there are very few explanations of how it works on the internet. I find myself [...]]]></description>
			<content:encoded><![CDATA[<p>JavaScript is an amazing little language, but it&#8217;s got some quirks that turn a lot of people off. One of those quirks is <code>this</code>, and how it&#8217;s not necessarily what you expect it to be. <code>this</code> isn&#8217;t that complicated, but there are very few explanations of how it works on the internet. I find myself constantly re-explaining the concept to those who are new to JavaScript development. This article is an attempt to explain how <code>this</code> works and how to use it properly.</p>
<h2>What is <code>this</code>?!</h2>
<p><code>this</code> is the current object. People that are used to object oriented languages use objects constantly, and <code>this</code> is how you access your object in JavaScript. This differs from Java and C++. This is best demonstrated by example, so let&#8217;s take a look.</p>

<div class="wp_syntax"><div class="code"><pre class="cpp cpp" style="font-family:monospace;"><span style="color: #0000ff;">class</span> HotDog <span style="color: #008000;">&#123;</span>
<span style="color: #0000ff;">public</span><span style="color: #008080;">:</span>
     string getCondiments<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>;
<span style="color: #0000ff;">private</span><span style="color: #008080;">:</span>
     string condiments;
<span style="color: #008000;">&#125;</span>
&nbsp;
string HotDog<span style="color: #008080;">::</span><span style="color: #007788;">getCondiments</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #0000ff;">return</span> condiments; <span style="color: #666666;">// The compiler knows to find &quot;condiments&quot; in the current HotDog instance</span>
<span style="color: #008000;">&#125;</span></pre></div></div>

<p>Ruby and Python don&#8217;t work this way. Instead, you must say &#8220;look for this variable in the current instance&#8221;</p>

<div class="wp_syntax"><div class="code"><pre class="python python" style="font-family:monospace;"><span style="color: #ff7700;font-weight:bold;">class</span> HotDog:
    condiments
    <span style="color: #ff7700;font-weight:bold;">def</span> getCondiments<span style="color: black;">&#40;</span><span style="color: #008000;">self</span><span style="color: black;">&#41;</span>:
         <span style="color: #ff7700;font-weight:bold;">return</span> <span style="color: #008000;">self</span>.<span style="color: black;">condiments</span> <span style="color: #808080; font-style: italic;"># &quot;self&quot; is a reference to the current instance of HotDog.</span></pre></div></div>


<div class="wp_syntax"><div class="code"><pre class="ruby ruby" style="font-family:monospace;">  <span style="color:#9966CC; font-weight:bold;">class</span> HotDog
         <span style="color:#0066ff; font-weight:bold;">@condiments</span>
         <span style="color:#9966CC; font-weight:bold;">def</span> getCondiments
              <span style="color:#0000FF; font-weight:bold;">return</span> <span style="color:#0066ff; font-weight:bold;">@condiments</span> <span style="color:#008000; font-style:italic;"># @condiments is an instance variable of an instance of HotDog</span>
         <span style="color:#9966CC; font-weight:bold;">end</span>
    <span style="color:#9966CC; font-weight:bold;">end</span></pre></div></div>

<p>JavaScript has more in common with Ruby and Python than with Java and C++. The same thing in JavaScript is:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> HotDog<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">condiments</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;mustard, ketchup&quot;</span>;
    <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">getCondiments</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
         <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">condiments</span>; <span style="color: #006600; font-style: italic;">//this is expected to be a reference to the current instance</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>That&#8217;s what <code>this</code> is expected to be, anyway. It&#8217;s expected to be a reference to the current instance of whatever object it&#8217;s defined within.</p>
<h3><code>this</code> Gets Tricky</h3>
<p>However, let&#8217;s say we wanted to find out what condiments were on our hotdog in 30 seconds. Assuming the HotDog class from above, that code might look like this:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> myHotDog <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> HotDog<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #006600; font-style: italic;">// Call the getCondiments function in 3 seconds</span>
setTimeout<span style="color: #009900;">&#40;</span>myHotDog.<span style="color: #660066;">getCondiments</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">3000</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>Many JavaScript beginners are surprised to learn that this code won&#8217;t work. It&#8217;ll give an error saying that <code>this</code> doesn&#8217;t have a member called <code>condiments</code>, even though it clearly does. What happened?!</p>
<p>As it turns out, <code>this</code> in the <code>getCondiments</code> function is set to the global object, <code>window</code>. This is because there is no binding of functions to instances in JavaScript. Whenever the instance isn&#8217;t explicitly stated, then <code>this</code> becomes <code>window</code> (at least in the browser). Writing this as <code>myHotDog.getCondiments()</code> indicates that you want <code>this</code> to be <code>myHotDog</code>, so it works correctly. The <code>setTimeout</code> function, however, just has a reference to that function. When it calls it, it&#8217;s not aware of <code>myHotDog</code>, so JavaScript sets <code>this</code> to <code>window</code></p>
<h2>&#8220;Fixing&#8221; <code>this</code></h2>
<p>There are several ways of forcing this to be what you want, and many of them touch on some of the more interesting features available in JavaScript. The first is <code>apply</code>. <code>apply</code> is a member of every function. It says &#8220;call this function with this object as this and these arguments. An example might help.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// These two are equivalent</span>
myHotDog.<span style="color: #660066;">getCondiments</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
myHotDog.<span style="color: #660066;">getCondiments</span>.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span>myHotDog<span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #006600; font-style: italic;">// You could also do this</span>
<span style="color: #003366; font-weight: bold;">var</span> yourHotDog <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> HotDog<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
myHotDog.<span style="color: #660066;">getCondiments</span>.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span>yourHotDog<span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #006600; font-style: italic;">// The above line does the same thing as:</span>
yourHotDog.<span style="color: #660066;">getCondiments</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
<span style="color: #006600; font-style: italic;">// The above two calls will return the condiments off of your hot dog, not mine.</span></pre></div></div>

<p>That doesn&#8217;t solve our <code>setTimeout</code> problem, however. <code>apply</code> actually calls the function, and we just want a reference to the function that uses the correct <code>this</code>. Luckily this is easily done, but let&#8217;s talk about some background first.</p>
<h3>Lexical Scoping</h3>
<p>In many languages (all the good ones, if you ask me), <a href="http://en.wikipedia.org/wiki/Scope_%28programming%29#Static_scoping_.28also_known_as_lexical_scoping.29">lexical scoping</a> is supported. Lexical scoping basically allows you to access local variables in a parent function. If a function is defined within another function, it can access its own local variables as well as those of the function it was defined within. Time for another example.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> drinkBeer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> beer <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Big Daddy IPA&quot;</span>;
    <span style="color: #003366; font-weight: bold;">function</span> pour<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
         <span style="color: #003366; font-weight: bold;">var</span> glass <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;Pint Glass&quot;</span>;
         <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">&quot;Poured &quot;</span> <span style="color: #339933;">+</span> beer <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; into a &quot;</span> <span style="color: #339933;">+</span> glass;
   <span style="color: #009900;">&#125;</span>
   <span style="color: #003366; font-weight: bold;">function</span> drink<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
         beer <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">null</span>;
         <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #3366CC;">&quot;Beer is all gone&quot;</span>;
   <span style="color: #009900;">&#125;</span>
   pour<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
   drink<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span>
drinkBeer<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>This works just fine. <code>drinkBeer</code> cannot access <code>glass</code>, but <code>pour</code> and <code>drink</code> can access <code>beer</code>.</p>
<p>Lexical scoping is extremely powerful. If this small example doesn&#8217;t explain it, I encourage you to look around other examples on the internet until it&#8217;s more clear. While writing JavaScript, look out for ways this can make your life easier. Once you&#8217;re looking, they&#8217;re all over the place.</p>
<h3>Solving our <code>setTimeout</code> Problem</h3>
<p>With lexical scoping, we can easily solve our <code>setTimeout</code> problem.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> myHotDog <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> HotDog<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
setTimeout<span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    myHotDog.<span style="color: #660066;">getCondiments</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #CC0000;">3000</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>See what we did there? We created a new function that we passed to <code>setTimeout</code>. Our new function can access <code>myHotDog</code>, so it just applies it to the <code>getCondiments</code> function. Pretty slick.</p>
<h3>Defending against <code>this</code> abuse</h3>
<p>As somebody writing the <code>HotDog</code> class, it might be upsetting to you that people constantly need to keep <code>this</code> in mind when accessing your class. It would be nicer if there was a way to make your class work all the time. Fortunately, there is!</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> HotDog<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> my <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>; <span style="color: #006600; font-style: italic;">// my references the current this, which is correct.</span>
    my.<span style="color: #660066;">condiments</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;mustard, ketchup&quot;</span>;
    my.<span style="color: #660066;">getCondiments</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
         <span style="color: #000066; font-weight: bold;">return</span> my.<span style="color: #660066;">condiments</span>; <span style="color: #006600; font-style: italic;">//my is guaranteed to be a reference to the original &quot;this&quot;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<p><code>HotDog</code> is a constructor. You instantiate a new instance of <code>HotDog</code> by writing &#8220;<code>new HotDog()</code>&#8220;. In constructors, <code>this</code> is always your instance. So we created a new variable, my, that references the <code>HotDog</code> instance. This allows you to always refer to the <code>HotDog</code> instance, no matter how the <code>getCondiments</code> function is called.</p>
<h3>Bind</h3>
<p>Another method of &#8220;fixing&#8221; <code>this</code> is adding a <code>bind</code> attribute to every function. This allows you to pass in an object that will be your <code>this</code>. Many JavaScript libraries, such as <a href="http://www.prototypejs.org/">Prototype</a>, do this.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> boundFunction <span style="color: #339933;">=</span> myHotDog.<span style="color: #660066;">getCondiments</span>.<span style="color: #660066;">bind</span><span style="color: #009900;">&#40;</span>myHotDog<span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>Now <code>boundFunction</code> will always call <code>getCondiments</code> with <code>this</code> set to <code>myHotDog</code>. If you&#8217;ve been paying attention, it should be fairly obvious how bind is written.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">function</span> bind<span style="color: #009900;">&#40;</span>thisObject<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #003366; font-weight: bold;">var</span> my <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">this</span>; <span style="color: #006600; font-style: italic;">// this is the function here</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        my.<span style="color: #660066;">apply</span><span style="color: #009900;">&#40;</span>thisObject<span style="color: #009900;">&#41;</span>;
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></div></div>

<h3>Cobra</h3>
<p>My own class library, <a href="http://justin.harmonize.fm/index.php/2009/01/cobra-a-little-javascript-class-library/">cobra</a>, solves the <code>this</code> problem in a different way. It passes a reference to the original <code>this</code> to every function, which allows you to use some fancy features like prototypal inheritance while still not worrying about binding and the like. If you&#8217;re interested, you can <a href="http://bitbucket.org/jmtulloss/cobra/overview/">find the code on bitbucket</a>.</p>
<h2>Whew</h2>
<p>That&#8217;s pretty much all you need to know about <code>this</code>.</p>
<ul>
<li><code>this</code> will be <code>window</code> whenever your function is indirectly called.</li>
<li>Use <code>apply</code> to force <code>this</code> to be what you want</li>
<li>You can use lexical scoping to make sure <code>this</code> behaves in a predictable manner</li>
</ul>
<p>I hope that&#8217;s clear, comment if it&#8217;s not!</p>
]]></content:encoded>
			<wfw:commentRss>http://justin.harmonize.fm/index.php/2009/09/an-introduction-to-javascripts-this/feed/</wfw:commentRss>
		<slash:comments>49</slash:comments>
		</item>
		<item>
		<title>Meetup.com webOS Client Part 1: Services</title>
		<link>http://justin.harmonize.fm/index.php/2009/07/meetup-com-webos-client-part-1-services/</link>
		<comments>http://justin.harmonize.fm/index.php/2009/07/meetup-com-webos-client-part-1-services/#comments</comments>
		<pubDate>Mon, 20 Jul 2009 04:46:23 +0000</pubDate>
		<dc:creator>justin</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Example-Meetup-Client]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Meetup.com]]></category>
		<category><![CDATA[Mojo]]></category>
		<category><![CDATA[webOS]]></category>

		<guid isPermaLink="false">http://justin.harmonize.fm/?p=283</guid>
		<description><![CDATA[Palm&#8217;s Mojo SDK has just been released to the public this past week. I thought I would take the opportunity to show off some of the awesome things the SDK can do. The Mojo SDK is exceptionally good at tying your life together with the web services you use every day, so my first series [...]]]></description>
			<content:encoded><![CDATA[<p><a href="http://developer.palm.com">Palm&#8217;s Mojo SDK</a> has just been released to the public this past week. I thought I would take the opportunity to show off some of the awesome things the SDK can do. The Mojo SDK is exceptionally good at tying your life together with the web services you use every day, so my first series will be on building a simple Meetup.com client. At first, we&#8217;ll just sync down meetups using the <a href="http://www.meetup.com/meetup_api/key/" target="_blank">api key that&#8217;s associated with every individual account</a>. Another day, we&#8217;ll add OAuth authentication to make it generally useful for anybody.</p>
<p>This article assumes that you have some knowledge of how to create an app, new scenes, and how to debug. If you aren&#8217;t somewhat comfortable with the SDK yet, check out the <a href="http://developer.palm.com/index.php?option=com_content&amp;view=article&amp;id=1758">Hello World tutorial</a>.</p>
<h3>Setup</h3>
<p>At first, we&#8217;ll just create a big button that puts all our meetups into the calendar. Nothing fancy. To create the scene, run:</p>

<div class="wp_syntax"><div class="code"><pre class="bash bash" style="font-family:monospace;">$ palm-generate <span style="color: #660033;">-t</span> new_scene <span style="color: #660033;">-p</span> <span style="color: #ff0000;">&quot;name=sync&quot;</span></pre></div></div>

<p>Then in the stage-assistant.js file, put:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">this</span>.<span style="color: #660066;">controller</span>.<span style="color: #660066;">pushScene</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;sync&quot;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<h3>Creating a Calendar</h3>
<p>The first thing that we will do is create a new calender for Meetup.com. This calendar will appear in the calendar app right next to your Google Calendar or Exchange calendar using the Palm Synergy APIs. This is great because it allows you to deliver new data to your users without having to write yet another way of presenting it. All contacts and calendars can be plugged straight into the core webOS applications.</p>
<p>To create a calendar, you first need to <a href="http://developer.palm.com/index.php?option=com_content&#038;view=article&#038;id=1737">make an account</a>:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">self.<span style="color: #660066;">accountServiceId</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;palm://com.palm.accounts/crud/&quot;</span>;
&nbsp;
 <span style="color: #009966; font-style: italic;">/* Retrieves account if it exists, otherwise creates it */</span>
setupAccount<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>self<span style="color: #339933;">,</span> k<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    self.<span style="color: #660066;">controller</span>.<span style="color: #660066;">serviceRequest</span><span style="color: #009900;">&#40;</span>self.<span style="color: #660066;">accountServiceId</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
        method<span style="color: #339933;">:</span> <span style="color: #3366CC;">'listAccounts'</span><span style="color: #339933;">,</span>
        parameters<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
        onSuccess<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>list<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            Mojo.<span style="color: #660066;">Log</span>.<span style="color: #660066;">info</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Got account list: %j&quot;</span><span style="color: #339933;">,</span> list<span style="color: #009900;">&#41;</span>;
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>list.<span style="color: #660066;">list</span> <span style="color: #339933;">&amp;&amp;</span> list.<span style="color: #660066;">list</span>.<span style="color: #660066;">length</span> <span style="color: #339933;">&gt;</span> 0<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                self.<span style="color: #660066;">account</span> <span style="color: #339933;">=</span> list.<span style="color: #660066;">list</span><span style="color: #009900;">&#91;</span>0<span style="color: #009900;">&#93;</span>;
                k<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
            <span style="color: #009900;">&#125;</span>
            <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                self.<span style="color: #660066;">account</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
                    username<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;justin&quot;</span><span style="color: #339933;">,</span>
                    domain<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;meetup.com&quot;</span><span style="color: #339933;">,</span>
                    displayName<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Meetup.com&quot;</span><span style="color: #339933;">,</span>
                    dataTypes<span style="color: #339933;">:</span> <span style="color: #009900;">&#91;</span><span style="color: #3366CC;">&quot;CALENDAR&quot;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">,</span>
                    isDataReadOnly<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">,</span>
                    icons<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>largeIcon<span style="color: #339933;">:</span> <span style="color: #3366CC;">''</span><span style="color: #339933;">,</span> smallIcon<span style="color: #339933;">:</span> <span style="color: #3366CC;">''</span><span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span>;
                self.<span style="color: #660066;">controller</span>.<span style="color: #660066;">serviceRequest</span><span style="color: #009900;">&#40;</span>self.<span style="color: #660066;">accountServiceId</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
                    method<span style="color: #339933;">:</span> <span style="color: #3366CC;">'createAccount'</span><span style="color: #339933;">,</span>
                    parameters<span style="color: #339933;">:</span> self.<span style="color: #660066;">account</span><span style="color: #339933;">,</span>
                    onSuccess<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>response<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        Mojo.<span style="color: #660066;">Log</span>.<span style="color: #660066;">info</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Got %j for %j&quot;</span><span style="color: #339933;">,</span> response<span style="color: #339933;">,</span> self.<span style="color: #660066;">account</span><span style="color: #009900;">&#41;</span>;
                        self.<span style="color: #660066;">account</span>.<span style="color: #660066;">accountId</span> <span style="color: #339933;">=</span> response.<span style="color: #660066;">accountId</span>;
                        k<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
                    <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
        onFailure<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            Mojo.<span style="color: #660066;">Controller</span>.<span style="color: #660066;">errorDialog</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Failed to create account&quot;</span><span style="color: #009900;">&#41;</span>;
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
        <span style="color: #000066;">onError</span><span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>error<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            Mojo.<span style="color: #660066;">Controller</span>.<span style="color: #660066;">errorDialog</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Error creating account&quot;</span><span style="color: #009900;">&#41;</span>;
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span></pre></div></div>

<p>You&#8217;ll notice a few different things about this code, the odd things which are just my style. Using <code>self</code> instead of <code>this</code> is an idiom from <a href="http://bitbucket.org/jmtulloss/cobra/overview/">Cobra</a>, a JavaScript class library I wrote. <code>k</code> is a continuation, or the function that should be called after the account is created. <code>k</code> I believe is an idiom for <a href="http://en.wikipedia.org/wiki/Continuation-passing_style">Continuation Passing Style</a> in <a href="http://en.wikipedia.org/wiki/Scheme_(programming_language)">Scheme</a>.</p>
<p>Beyond that, this is very standard Mojo code. Services are identified as &#8220;palm://com.palm.<service>&#8221; and all methods support <code>onSuccess</code>, <code>onFailure</code>, and <code>onError</code> callbacks. This code checks to see if there is already an account associated with this appId, and if there is, calls its continuation. If there is not, it creates the account and calls its continuation.</p>
<p>After the account is created, we can <a href="http://developer.palm.com/index.php?option=com_content&#038;view=article&#038;id=1751">create the calendar</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">self.<span style="color: #660066;">calendarServiceId</span> <span style="color: #339933;">=</span> <span style="color: #3366CC;">&quot;palm://com.palm.calendar/crud/&quot;</span>;
&nbsp;
<span style="color: #009966; font-style: italic;">/* Retrieves calendar if it exists, otherwise creates it */</span>
setupCalendar<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>self<span style="color: #339933;">,</span> k<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    self.<span style="color: #660066;">controller</span>.<span style="color: #660066;">serviceRequest</span><span style="color: #009900;">&#40;</span>self.<span style="color: #660066;">calendarServiceId</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
        method<span style="color: #339933;">:</span> <span style="color: #3366CC;">'listCalendars'</span><span style="color: #339933;">,</span>
        parameters<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
            accountId<span style="color: #339933;">:</span> self.<span style="color: #660066;">account</span>.<span style="color: #660066;">accountId</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
        onSuccess<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>calList<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            Mojo.<span style="color: #660066;">Log</span>.<span style="color: #660066;">info</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Got calendar list&quot;</span><span style="color: #009900;">&#41;</span>;
            <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>calList.<span style="color: #660066;">calendars</span>.<span style="color: #660066;">length</span> <span style="color: #339933;">&gt;</span> 0<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                self.<span style="color: #660066;">calendar</span> <span style="color: #339933;">=</span> calList.<span style="color: #660066;">calendars</span><span style="color: #009900;">&#91;</span>0<span style="color: #009900;">&#93;</span>;
                k<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
            <span style="color: #009900;">&#125;</span>
            <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
                self.<span style="color: #660066;">calendar</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #000066;">name</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;Meetup.com&quot;</span>
                <span style="color: #009900;">&#125;</span>
                self.<span style="color: #660066;">controller</span>.<span style="color: #660066;">serviceRequest</span><span style="color: #009900;">&#40;</span>self.<span style="color: #660066;">calendarServiceId</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
                    method<span style="color: #339933;">:</span> <span style="color: #3366CC;">'createCalendar'</span><span style="color: #339933;">,</span>
                    parameters<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
                        accountId<span style="color: #339933;">:</span> self.<span style="color: #660066;">account</span>.<span style="color: #660066;">accountId</span><span style="color: #339933;">,</span>
                        calendar<span style="color: #339933;">:</span> self.<span style="color: #660066;">calendar</span>
                    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
                    onSuccess<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>response<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        self.<span style="color: #660066;">calendar</span>.<span style="color: #660066;">calendarId</span> <span style="color: #339933;">=</span> response.<span style="color: #660066;">calendarId</span>
                        k<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
                    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
                    onFailure<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>error<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        Mojo.<span style="color: #660066;">Log</span>.<span style="color: #660066;">error</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Creating calendar failed: %j&quot;</span><span style="color: #339933;">,</span> error<span style="color: #009900;">&#41;</span>;
                        Mojo.<span style="color: #660066;">Controller</span>.<span style="color: #660066;">errorDialog</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Failed to create calendar&quot;</span><span style="color: #009900;">&#41;</span>;
                    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
                    <span style="color: #000066;">onError</span><span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>error<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                        Mojo.<span style="color: #660066;">Log</span>.<span style="color: #660066;">error</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Creating calendar failed: %j&quot;</span><span style="color: #339933;">,</span> error<span style="color: #009900;">&#41;</span>;
                        Mojo.<span style="color: #660066;">Controller</span>.<span style="color: #660066;">errorDialog</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Error creating calendar&quot;</span><span style="color: #009900;">&#41;</span>;
                    <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
        onFailure<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            Mojo.<span style="color: #660066;">Controller</span>.<span style="color: #660066;">errorDialog</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Failed to create calendar&quot;</span><span style="color: #009900;">&#41;</span>;
        <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
        <span style="color: #000066;">onError</span><span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>error<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            Mojo.<span style="color: #660066;">Controller</span>.<span style="color: #660066;">errorDialog</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Error creating calendar&quot;</span><span style="color: #009900;">&#41;</span>;
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span></pre></div></div>

<p>This is almost identical to the account creation code, except it&#8217;s creating a calendar.</p>
<p>Now calling these functions is easy:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">self.<span style="color: #660066;">setupAccount</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    self.<span style="color: #660066;">setupCalendar</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        self.<span style="color: #660066;">buttonModel</span>.<span style="color: #660066;">disabled</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span>;
        self.<span style="color: #660066;">controller</span>.<span style="color: #660066;">modelChanged</span><span style="color: #009900;">&#40;</span>self.<span style="color: #660066;">buttonModel</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<h3>Pulling down events</h3>
<p>To get the events that are associated with the account, I am using the <a href="http://www.meetup.com/meetup_api/clients/">Meetup.com JavaScript client</a>. The client requires jQuery, but since it only requires jQuery to do JSONP, we replace that line with a Mojo call. There is no reason that we couldn&#8217;t use jQuery, but jQuery is overkill for just doing JSONP.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">jQuery.<span style="color: #660066;">getJSON</span><span style="color: #009900;">&#40;</span>urlprefix <span style="color: #339933;">+</span> call_type <span style="color: #339933;">+</span> url<span style="color: #339933;">,</span> params<span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>json<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>callback<span style="color: #009900;">&#40;</span>json<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></pre></div></div>

<p>Becomes:</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;"><span style="color: #003366; font-weight: bold;">var</span> query <span style="color: #339933;">=</span> $H<span style="color: #009900;">&#40;</span>params<span style="color: #009900;">&#41;</span>.<span style="color: #660066;">toQueryString</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
url <span style="color: #339933;">=</span> urlprefix <span style="color: #339933;">+</span> call_type <span style="color: #339933;">+</span> url <span style="color: #339933;">+</span> query;
Mojo.<span style="color: #660066;">loadScriptWithCallback</span><span style="color: #009900;">&#40;</span>url<span style="color: #339933;">,</span> Mojo.<span style="color: #660066;">doNothing</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>After we have the Meetup client library, getting the events is easy, but a little indirect. It takes three API calls, one to get the member id, one to get every group associated with the member id, and one to get every event in all those groups. You can see the details on <a href="http://www.meetup.com/meetup_api/docs/#readmethods">Meetup&#8217;s API page</a>.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">self.<span style="color: #660066;">client</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> MeetupApiClient<span style="color: #009900;">&#40;</span>Meet.<span style="color: #660066;">Auth</span>.<span style="color: #660066;">apiKey</span><span style="color: #009900;">&#41;</span>;
syncCalendar<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>self<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// Gets my member id</span>
    Mojo.<span style="color: #660066;">Log</span>.<span style="color: #660066;">info</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Syncing calendar&quot;</span><span style="color: #009900;">&#41;</span>;
    self.<span style="color: #660066;">client</span>.<span style="color: #660066;">get_members</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
        relation<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;self&quot;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> self._getGroups<span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
_getGroups<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>self<span style="color: #339933;">,</span> members<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    Mojo.<span style="color: #660066;">Log</span>.<span style="color: #660066;">info</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Got members&quot;</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #003366; font-weight: bold;">var</span> memberId <span style="color: #339933;">=</span> members.<span style="color: #660066;">results</span><span style="color: #009900;">&#91;</span>0<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">id</span>;
    self.<span style="color: #660066;">client</span>.<span style="color: #660066;">get_groups</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
        member_id<span style="color: #339933;">:</span> memberId
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> self._getEvents<span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
_getEvents<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>self<span style="color: #339933;">,</span> groups<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    Mojo.<span style="color: #660066;">Log</span>.<span style="color: #660066;">info</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Got events&quot;</span><span style="color: #009900;">&#41;</span>;
    groups <span style="color: #339933;">=</span> groups.<span style="color: #660066;">results</span>;
    <span style="color: #003366; font-weight: bold;">var</span> groupString <span style="color: #339933;">=</span> groups<span style="color: #009900;">&#91;</span>0<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">id</span>;
    <span style="color: #003366; font-weight: bold;">var</span> today <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
    <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">var</span> i <span style="color: #339933;">=</span> <span style="color: #CC0000;">1</span>; i <span style="color: #339933;">&lt;</span> groups.<span style="color: #660066;">length</span>; i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        groupString <span style="color: #339933;">+=</span> <span style="color: #3366CC;">&quot;,&quot;</span> <span style="color: #339933;">+</span> groups<span style="color: #009900;">&#91;</span>i<span style="color: #009900;">&#93;</span>.<span style="color: #660066;">id</span>;
    <span style="color: #009900;">&#125;</span>
    self.<span style="color: #660066;">client</span>.<span style="color: #660066;">get_events</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
        group_id<span style="color: #339933;">:</span> groupString<span style="color: #339933;">,</span>
        after<span style="color: #339933;">:</span> today.<span style="color: #660066;">getMonth</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> today.<span style="color: #660066;">getDay</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> today.<span style="color: #660066;">getFullYear</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> self._saveEvents<span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span></pre></div></div>

<p>Now <code>self._saveEvents</code> will receive a list of events that are coming up after today. All we need to do is loop over the list, format them as Palm calendar events, and pass them to the calendar service.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">_saveEvents<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>self<span style="color: #339933;">,</span> events<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    Mojo.<span style="color: #660066;">Log</span>.<span style="color: #660066;">info</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Saving events&quot;</span><span style="color: #009900;">&#41;</span>;
&nbsp;
    self.<span style="color: #660066;">numEventsProcessed</span> <span style="color: #339933;">=</span> 0;
    self.<span style="color: #660066;">events</span> <span style="color: #339933;">=</span> events;
&nbsp;
    events.<span style="color: #660066;">results</span>.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span><span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>meetupEvent<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #003366; font-weight: bold;">var</span> time <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">new</span> Date<span style="color: #009900;">&#40;</span>meetupEvent.<span style="color: #660066;">time</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">getTime</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>meetupEvent.<span style="color: #660066;">myrsvp</span> <span style="color: #339933;">!=</span> <span style="color: #3366CC;">&quot;no&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            self.<span style="color: #660066;">controller</span>.<span style="color: #660066;">serviceRequest</span><span style="color: #009900;">&#40;</span>self.<span style="color: #660066;">calendarServiceId</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
                method<span style="color: #339933;">:</span> <span style="color: #3366CC;">'createEvent'</span><span style="color: #339933;">,</span>
                parameters<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
                    calendarId<span style="color: #339933;">:</span> self.<span style="color: #660066;">calendar</span>.<span style="color: #660066;">calendarId</span><span style="color: #339933;">,</span>
                    event<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
                        eventId<span style="color: #339933;">:</span> meetupEvent.<span style="color: #660066;">id</span><span style="color: #339933;">,</span>
                        subject<span style="color: #339933;">:</span> meetupEvent.<span style="color: #000066;">name</span><span style="color: #339933;">,</span>
                        startTimestamp<span style="color: #339933;">:</span> time<span style="color: #339933;">,</span>
                        endTimestamp<span style="color: #339933;">:</span> time <span style="color: #339933;">+</span> <span style="color: #CC0000;">3600000</span><span style="color: #339933;">,</span> <span style="color: #006600; font-style: italic;">// 1 hour in ms</span>
                        allDay<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">,</span>
                        note<span style="color: #339933;">:</span> self._formatNote<span style="color: #009900;">&#40;</span>meetupEvent<span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
                        location<span style="color: #339933;">:</span> meetupEvent.<span style="color: #660066;">lat</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;, &quot;</span> <span style="color: #339933;">+</span> meetupEvent.<span style="color: #660066;">lon</span><span style="color: #339933;">,</span>
                        alarm<span style="color: #339933;">:</span> <span style="color: #3366CC;">'none'</span><span style="color: #339933;">,</span>
                    <span style="color: #009900;">&#125;</span>
                <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
                onSuccess<span style="color: #339933;">:</span> self._createdEvent<span style="color: #339933;">,</span>
                <span style="color: #000066;">onError</span><span style="color: #339933;">:</span> self._errorCreatingEvent<span style="color: #339933;">,</span>
                onFailure<span style="color: #339933;">:</span> self._failureCreatingEvent
            <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
_createdEvent<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>self<span style="color: #339933;">,</span> response<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    self._checkIfSyncFinished<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
_errorCreatingEvent<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>self<span style="color: #339933;">,</span> response<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    Mojo.<span style="color: #660066;">Log</span>.<span style="color: #660066;">error</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Could not create event: %j&quot;</span><span style="color: #339933;">,</span> response<span style="color: #009900;">&#41;</span>;
    self._checkIfSyncFinished<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
_failureCreatingEvent<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>self<span style="color: #339933;">,</span> response<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    Mojo.<span style="color: #660066;">Log</span>.<span style="color: #660066;">error</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Failed to create event: %d, %j&quot;</span><span style="color: #339933;">,</span> self._eventsReturned<span style="color: #339933;">,</span> response<span style="color: #009900;">&#41;</span>;
    self._checkIfSyncFinished<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
&nbsp;
_checkIfSyncFinished<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>self<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    self.<span style="color: #660066;">numEventsProcessed</span>++;
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>self.<span style="color: #660066;">numEventsProcessed</span> <span style="color: #339933;">==</span> self.<span style="color: #660066;">events</span>.<span style="color: #660066;">meta</span>.<span style="color: #660066;">count</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>self.<span style="color: #660066;">events</span>.<span style="color: #660066;">meta</span>.<span style="color: #660066;">next</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            Mojo.<span style="color: #660066;">Log</span>.<span style="color: #660066;">info</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Fetching the next page of results...&quot;</span><span style="color: #009900;">&#41;</span>;
            self.<span style="color: #660066;">client</span>.<span style="color: #660066;">nextPage</span><span style="color: #009900;">&#40;</span>self._saveEvents<span style="color: #009900;">&#41;</span>;
        <span style="color: #009900;">&#125;</span>
        <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
            Mojo.<span style="color: #660066;">Log</span>.<span style="color: #660066;">info</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;Fetched all the events&quot;</span><span style="color: #009900;">&#41;</span>;
            self.<span style="color: #660066;">buttonModel</span>.<span style="color: #660066;">disabled</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">false</span>;
            self.<span style="color: #660066;">controller</span>.<span style="color: #660066;">modelChanged</span><span style="color: #009900;">&#40;</span>self.<span style="color: #660066;">buttonModel</span><span style="color: #009900;">&#41;</span>;
            self.<span style="color: #660066;">controller</span>.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;sync-button&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">mojo</span>.<span style="color: #660066;">deactivate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span></pre></div></div>

<p>And that&#8217;s basically it! With the code above, all your Meetup.com events can be inserted into your webOS calendar. All you need is an event handler for your button.</p>

<div class="wp_syntax"><div class="code"><pre class="javascript javascript" style="font-family:monospace;">self.<span style="color: #660066;">buttonModel</span> <span style="color: #339933;">=</span> <span style="color: #009900;">&#123;</span>
    buttonLabel<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Sync'</span><span style="color: #339933;">,</span>
    disabled<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">true</span>
<span style="color: #009900;">&#125;</span>;
&nbsp;
self.<span style="color: #660066;">controller</span>.<span style="color: #660066;">setupWidget</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'sync-button'</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
    type<span style="color: #339933;">:</span> Mojo.<span style="color: #660066;">Widget</span>.<span style="color: #660066;">activityButton</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> self.<span style="color: #660066;">buttonModel</span><span style="color: #009900;">&#41;</span>;
&nbsp;
Mojo.<span style="color: #660066;">Event</span>.<span style="color: #660066;">listen</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">'sync-button'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span> Mojo.<span style="color: #660066;">Event</span>.<span style="color: #660066;">tap</span><span style="color: #339933;">,</span> <span style="color: #003366; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    self.<span style="color: #660066;">buttonModel</span>.<span style="color: #660066;">disabled</span> <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">true</span>;
    self.<span style="color: #660066;">controller</span>.<span style="color: #660066;">modelChanged</span><span style="color: #009900;">&#40;</span>self.<span style="color: #660066;">buttonModel</span><span style="color: #009900;">&#41;</span>
    self.<span style="color: #660066;">controller</span>.<span style="color: #660066;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;sync-button&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">mojo</span>.<span style="color: #660066;">activate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
    self.<span style="color: #660066;">syncCalendar</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>;</pre></div></div>

<p>This works assuming a view containing:</p>

<div class="wp_syntax"><div class="code"><pre class="html" style="font-family:monospace;">&lt;div x-mojo-element=&quot;Button&quot; id=&quot;sync-button&quot;&gt;&lt;/div&gt;</pre></div></div>

<h3>Debugging Tips</h3>
<p>Creating a file called <code>framework_config.json</code> allows you to change the logging level. That will permit JavaScript messages to be output into /var/log/messages on the device. This is especially valuable if you&#8217;re working on a 64-bit linux machine where the inspector is currently not supported.</p>

<div class="wp_syntax"><div class="code"><pre class="json" style="font-family:monospace;">{
    &quot;logLevel&quot;: 99 // 0 means no logging, 99 will max it out
}</pre></div></div>

<p>Removing /var/luna/data/dbdata/PalmDatabase.db3 removes all data you may have inserted. This allows you to start over fresh, but I wouldn&#8217;t recommend doing this on your actual device. You&#8217;ll lose all your data!</p>
<h3>Todo</h3>
<p>This program isn&#8217;t really complete, but it&#8217;s a good start. A couple of things we still have to do are:</p>
<ul>
<li>Add Authentication with OAuth</li>
<li>Keep track of already inserted appointments so we don&#8217;t insert duplicates</li>
<li>Add automatic synchronization in the background</li>
<li>Add push updates (this is impossible without server side support)</li>
<li>Make things look nice</li>
</ul>
<p>Stay tuned for articles on some or all of these exciting new features! Until then, the <a href="http://github.com/jmtulloss/Meetup.com-Mojo-Client/tree/Services">code is available on github</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://justin.harmonize.fm/index.php/2009/07/meetup-com-webos-client-part-1-services/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Java, please stop ruining my fun.</title>
		<link>http://justin.harmonize.fm/index.php/2009/07/java-please-stop-ruining-my-fun/</link>
		<comments>http://justin.harmonize.fm/index.php/2009/07/java-please-stop-ruining-my-fun/#comments</comments>
		<pubDate>Tue, 14 Jul 2009 06:49:08 +0000</pubDate>
		<dc:creator>justin</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[Clojure]]></category>
		<category><![CDATA[Environment]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Learning-Clojure]]></category>

		<guid isPermaLink="false">http://justin.harmonize.fm/?p=263</guid>
		<description><![CDATA[I don&#8217;t like Java. I haven&#8217;t learned Java well because I don&#8217;t enjoy using it. I don&#8217;t enjoy using it because it&#8217;s verbose, for one, but mostly because it&#8217;s constantly making things hard for me to do. I know there are ways to do what I want, after all, millions of people use Java successfully [...]]]></description>
			<content:encoded><![CDATA[<p>I don&#8217;t like Java. I haven&#8217;t learned Java well because I don&#8217;t enjoy using it. I don&#8217;t enjoy using it because it&#8217;s verbose, for one, but mostly because it&#8217;s constantly making things hard for me to do. I know there are ways to do what I want, after all, millions of people use Java successfully every day, but I don&#8217;t know what they are. Furthermore, finding out what they are is excruciatingly painful.</p>
<p>I recently did a <a href="http://justin.harmonize.fm/index.php/tag/learning-clojure/" mce_href="http://justin.harmonize.fm/index.php/tag/learning-clojure/">series of articles</a> on a project I was doing to learn Clojure. It kind of petered out for a number of reasons, but one constant annoyance in learning Clojure was dealing with the Java-isms. Java has given Clojure a vast library of high quality software essentially for free, but it&#8217;s also brought on a lot of the pain, much of which I think needs to be fixed before Clojure can have the nice feel of my favorite dynamic languages.</p>
<h3>Installing Clojure</h3>
<p>The first thing one has to do is install Clojure. It&#8217;s not a package in Ubuntu yet, but it&#8217;s young, so that&#8217;s ok cause we&#8217;re veterans and don&#8217;t need no stinking packages. To compile, we just download the source and type &#8220;ant&#8221;.</p>
<p>And that&#8217;s it. There&#8217;s no install process that makes a nice pretty &#8220;clojure&#8221; command that takes us to the REPL or executes scripts that are passed to it. To run clojure, you need to run it using Java:</p>

<div class="wp_syntax"><div class="code"><pre class="sh" style="font-family:monospace;">$ java -cp clojure.jar clojure.lang.Repl</pre></div></div>

<p>That is a lot to type just to get a Repl, and getting a usable command line is even harder. After installing JLine ConsoleRunner, you need to get the library into your classpath (a rant on which is upcoming) and then run</p>

<div class="wp_syntax"><div class="code"><pre class="sh" style="font-family:monospace;">$ java -cp jline-0.9.91.jar:clojure.jar jline.ConsoleRunner clojure.lang.Repl</pre></div></div>

<p>Not exactly intuitive, but whatever. We put it in a bash script, put it in our path, and head off to the races. After a while, we have a few lines of a quality script we would like to save and run. How do we do that? </p>
<p>Obviously, it&#8217;s:</p>

<div class="wp_syntax"><div class="code"><pre class="sh" style="font-family:monospace;">$ java -cp clojure.jar clojure.lang.Script my-script.clj</pre></div></div>

<p>This assumes that clojure.jar is in the same directory as the script you want to run. If you don&#8217;t have clojure.jar there, you must provide a specific path to the jar file. There is no idea of a default directory where Java will look for jar files. You must provide every single jar file to Java at runtime.</p>
<p>Contrast this with the Python install process:</p>

<div class="wp_syntax"><div class="code"><pre class="sh" style="font-family:monospace;">$ sudo apt-get install python
$ python
... Have fun in the interpreter
... Write a script
$ python my_script.py</pre></div></div>

<p>Simple.</p>
<h3>The Classpath</h3>
<p>First of all, I&#8217;m no expert on the classpath, but it seems like an unholy abomination thrust upon us by invisible powers that must be extinguished at all costs. It would appear, and again, I am no expert, but it would appear that every single dependency of a program must be explicitly passed to Java at the time you run your program. I wrote a bash script to automate the process, but viewing the command line for running my simple Compojure-based webapp is apalling:</p>

<div class="wp_syntax"><div class="code"><pre class="sh" style="font-family:monospace;">java -Djava.library.path=/usr/local/lib -cp :/mnt/data/Users/justin/bin/compojure/compojure.jar:/mnt/data/Users/justin/bin/compojure/deps/clojure-contrib.jar:/mnt/data/Users/justin/bin/compojure/deps/clojure.jar:/mnt/data/Users/justin/bin/compojure/deps/fact.jar:/mnt/data/Users/justin/bin/compojure/deps/jetty-6.1.14.jar:/mnt/data/Users/justin/bin/compojure/deps/jetty-util-6.1.14.jar:/mnt/data/Users/justin/bin/compojure/deps/re-rand.jar:/mnt/data/Users/justin/bin/compojure/deps/servlet-api-2.5-6.1.14.jar:/mnt/data/Users/justin/lib/clj-http-client.jar:/mnt/data/Users/justin/lib/clojure-contrib.jar:/mnt/data/Users/justin/lib/clojure.jar:/mnt/data/Users/justin/lib/commons-codec-1.3.jar:/mnt/data/Users/justin/lib/commons-httpclient-3.1.jar:/mnt/data/Users/justin/lib/commons-io-1.4-javadoc.jar:/mnt/data/Users/justin/lib/commons-io-1.4-sources.jar:/mnt/data/Users/justin/lib/commons-io-1.4.jar:/mnt/data/Users/justin/lib/commons-logging-1.1.1-javadoc.jar:/mnt/data/Users/justin/lib/commons-logging-1.1.1-sources.jar:/mnt/data/Users/justin/lib/commons-logging-1.1.1.jar:/mnt/data/Users/justin/lib/commons-logging-adapters-1.1.1.jar:/mnt/data/Users/justin/lib/commons-logging-api-1.1.1.jar:/mnt/data/Users/justin/lib/commons-logging-tests.jar:/mnt/data/Users/justin/lib/compojure.jar:/mnt/data/Users/justin/lib/jline-0.9.94.jar:/mnt/data/Users/justin/lib/tokyo-cabinet-clj.jar:/mnt/data/Users/justin/lib/tokyo-cabinet.jar:/mnt/data/Users/justin/lib/tokyocabinet.jar:/mnt/data/Users/justin/lib/tokyotyrant-0.6.jar clojure.lang.Script index.clj</pre></div></div>

<p>That is bad. That is not correct, that is not how software should be designed, I object. Every other language I can think of off the top of my head (except JavaScript) has some structured way of finding its dependencies, and most have a way of adding additional rules to that search should the defaults not be adequate. While this can lead to &#8220;DLL hell&#8221;, I do not see how the Java situation is any better when everybody just ends up with scripts to automate the process and then those scripts pick up the wrong things and you can&#8217;t figure out why.</p>
<p>The classpath makes me very upset. If Clojure can find a way to mask it, I would appreciate it very much.</p>
<h3>Maven</h3>
<p>First of all, what the hell is Maven? A quick trip to their site reveals a huge chunk of text with hundreds of links and an initial sentence that describes it as:</p>
<blockquote><p>Maven, a&nbsp;<a href="http://en.wikipedia.org/wiki/Maven" mce_href="http://en.wikipedia.org/wiki/Maven">Yiddish word</a> meaning&nbsp;<span mce_name="em" mce_style="font-style: italic;" style="font-style: italic;" class="Apple-style-span" mce_fixed="1">accumulator of knowledge</span>, was originally started as an attempt to simplify the build processes in the Jakarta Turbine project.</p>
</blockquote>
<p>I went to the site with some hope that it would provide some relief to my dependency issues (All I want is &#8220;pip install&#8221;, or &#8220;gem install&#8221;), and I get greeted with a dense paragraph of history combined with some mumbo-jumbo about &#8220;best practices&#8221;.</p>
<p>After reading a bit I find that Maven downloads and builds dependencies and installs them in a local repository, along with the library you are trying to compile. Perfect! Sounds like exactly what I want. However, it doesn&#8217;t mention anything about the classpath. Am I still responsible for dealing with all that muck, even though it&#8217;s tucking my libraries in a hidden directory (implying that it&#8217;s responsible for managing them)?</p>
<p>To answer that question I need to wade through dozens of other pages that alternately describe how to accomplish basic tasks and lecture me on software engineering. Finally I come to the conclusion that while Maven does indeed find dependencies for you, it does not actually help you execute programs with those dependencies in place. This means you either need a script that automatically passes your entire maven local repository to Java, or you need to know the dependencies that Maven was conveniently supposed to hide from you. To top it off, it doesn&#8217;t play well with Clojure. Completely useless.</p>
<p>(For the record, there is a <a href="http://mojo.codehaus.org/exec-maven-plugin/" mce_href="http://mojo.codehaus.org/exec-maven-plugin/">Maven extension</a> that does exactly this.)</p>
<h3>The Last Word</h3>
<p>Dependency management is a hard problem that all languages must learn to deal with. Higher level languages have an even harder time in that they must not only deal with whatever dependencies they have written in their own language, but also with extensions written in other languages. Clojure, which is still very young, suffers tremendously from the godawful environment that Java has ensconsed itself in. I am largely a veteran of the *nix world, which seems quite different from the world Java developers have built around themselves. They have their own tools, their own build systems, their own set of &#8220;best practices&#8221;, and the Apache foundation. What I have seen in my brief saunter over the wall has appalled me. It has appalled me far more than similar saunters into the somewhat exciting world of Microsoft and .NET. It strikes me very much as a world in need of fixing, and I hope that Clojure (or Scala) can do it. Heck, I may even do my part to help.</p>
<p>But probably I&#8217;ll just run back to Python.</p>
]]></content:encoded>
			<wfw:commentRss>http://justin.harmonize.fm/index.php/2009/07/java-please-stop-ruining-my-fun/feed/</wfw:commentRss>
		<slash:comments>9</slash:comments>
		</item>
		<item>
		<title>Exploring memcmp</title>
		<link>http://justin.harmonize.fm/index.php/2009/05/exploring-memcmp/</link>
		<comments>http://justin.harmonize.fm/index.php/2009/05/exploring-memcmp/#comments</comments>
		<pubDate>Wed, 13 May 2009 19:38:05 +0000</pubDate>
		<dc:creator>justin</dc:creator>
				<category><![CDATA[Development]]></category>
		<category><![CDATA[memcmp]]></category>
		<category><![CDATA[SIMD]]></category>
		<category><![CDATA[virtualization]]></category>

		<guid isPermaLink="false">http://justin.harmonize.fm/?p=231</guid>
		<description><![CDATA[Yesterday I had a problem. I had a chunk of binary data saved in a file that I had extracted from another file. I needed to find where this chunk of data had come from. I wanted to know if it appeared in the original file more than once, and if so, where. I also [...]]]></description>
			<content:encoded><![CDATA[<p>Yesterday I had a problem. I had a chunk of binary data saved in a file that I had extracted from another file. I needed to find where this chunk of data had come from. I wanted to know if it appeared in the original file more than once, and if so, where. I also wanted to see if it appeared in a number of other files.</p>
<p>There is no command in Linux (that I know of) that does this, so I set out to write my own. I used the obvious and naive implementation. I memory mapped the file containing the chunk I was looking for (the needle) and the file containing all the data I was looking through (the haystack). Then I walked through the haystack byte by byte and ran memcmp to see if it was the same.</p>

<div class="wp_syntax"><div class="code"><pre class="c c" style="font-family:monospace;"><span style="color: #993333;">int</span>
compare<span style="color: #009900;">&#40;</span><span style="color: #993333;">char</span> <span style="color: #339933;">*</span>needle_pa<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> needle_size<span style="color: #339933;">,</span> <span style="color: #993333;">char</span> <span style="color: #339933;">*</span>haystack_pa<span style="color: #339933;">,</span> <span style="color: #993333;">int</span> haystack_size<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #993333;">int</span> i;
    <span style="color: #993333;">int</span> found <span style="color: #339933;">=</span> <span style="color:#800080;">0</span>;
    <span style="color: #b1b100;">for</span> <span style="color: #009900;">&#40;</span>i <span style="color: #339933;">=</span> <span style="color:#800080;">0</span>; i <span style="color: #339933;">&lt;</span> haystack_size<span style="color: #339933;">-</span>needle_size; i<span style="color: #339933;">++</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span>memcmp<span style="color: #009900;">&#40;</span>needle_pa<span style="color: #339933;">,</span> haystack_pa <span style="color: #339933;">+</span> i<span style="color: #339933;">,</span> needle_size<span style="color: #009900;">&#41;</span> <span style="color: #339933;">==</span> <span style="color:#800080;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000066;">printf</span><span style="color: #009900;">&#40;</span><span style="color: #ff0000;">&quot;Found needle at byte offset %x<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> i<span style="color: #009900;">&#41;</span>;
            found++;
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> found;
<span style="color: #009900;">&#125;</span></pre></div></div>

<p>This was fast enough for my purposes, but intellectual curiosity lead me to try and optimize this function. There are a number of ways to speed this up, but the one that seemed most fun was to compare more bits at once. This lead me to explore in internals of memcmp.</p>
<h3>glibc&#8217;s memcmp</h3>
<p>Glibc comes with a memcmp implementation, of course. You can <a href="http://gist.github.com/111199">view the source here</a>. It&#8217;s quite simple. If the passed buffer is not aligned on a word boundary, iterate byte by byte doing comparison via subtraction until you get to a word boundary. Then loop through word by word comparing with the equality operator (==). Once you get to the end of all the full words, compare the final few bytes by subtraction again.</p>
<p>This implementation is pretty good. It&#8217;s fast on most platforms as it maximizes aligned comparisons. It&#8217;s very portable as it does not take advantage of any processor specific features. However, the fact that it does not take advantage of processor features means that it could be faster.</p>
<h3>SIMD (Single Instruction Multiple Data)</h3>
<p>Many platforms have SIMD instructions. SIMD instructions are very specific vector instructions that allow you to manipulate large chunks of data with a single instruction. A common operation found in SIMD instruction sets is string comparison, which is what essentially what memcmp does. For instance, in the x86 architecture, there are instructions to compare strings 16 bytes at a time. That&#8217;s 4x the throughput of the glibc implementation.</p>
<p>Taking advantage of SIMD instructions is a bit tricky. They&#8217;re not supported on all platforms, even in the same processor family. Intel, for instance, has SSE1, SSE2, and SSE3 instruction sets, which all add new instructions not found in the previous iteration. Generally there&#8217;s no reason to go through the pain of using these instructions unless you absolutely know you need the speed boost. Today, we&#8217;re just doing it for fun.</p>
<h3>GCC built-in functions</h3>
<p>GCC comes with a number of SIMD optimized replacements for common libc functions. Memcmp is one of those functions that GCC optimizes. However, turning on this optimization is a bit tricky. Again, since every platform supports different instructions, you have to be very specific about what features are supported in order to get the maximum optimization. GCC will always create the most compatible binary it can unless specifically told otherwise, which is the right thing to do.</p>
<p>To compile with GCC&#8217;s optimized memcmp, we run the following:</p>

<div class="wp_syntax"><div class="code"><pre class="none" style="font-family:monospace;">gcc -Wall findneedle.c -O3 -march=opteron -o findneedle</pre></div></div>

<p>This tells gcc to optimize as much as it can (-O3) and that it can expect to be running on a processor with the same features as the Opteron (x86-64, SSE1-3). I&#8217;m running this on an Intel Core Duo, which supports the same feature set. This gives us a version of the findneedle program that does not use the standard memcmp implementation, but instead uses GCC&#8217;s SIMD optimized version.</p>
<h3>The Results</h3>
<p>My test was simple: I timed how long it took to a 16K needle in a 328MB haystack.</p>
<p>At first I was doing this on a virtual machine. The most interesting result of any of this is that the program running in a vmware image slowed down significantly. It went from an average of ~4 seconds to an average of ~10 seconds. Thinking that the virtual machine might be emulating the SIMD instructions, I compiled on my mac itself. In that situation I saw the expected speedup. The average search time went from ~4.4 seconds to ~4.1 seconds. That&#8217;s a 7% speedup!</p>
<p>Clearly, these results show that optimizing your program to take advantage of processor features does not always help very much. However, if you&#8217;re compiling your own programs (I&#8217;m looking at you, gentoo fanatics), it does help to make sure that your processor options are set correctly. Just switching a few flags can cause core functionality to be improved, and that makes the whole experience that much better.</p>
<p>Also, assume your virtual machine is a 486. It&#8217;s way faster that way.</p>
<p><strong>Update:</strong><br />
There was a <a href=" http://news.ycombinator.com/item?id=607489">good debate</a> about optimizing the algorithm itself over on hacker news. <a href="http://news.ycombinator.com/user?id=jws">jws</a> was nice enough to <a href="http://news.ycombinator.com/item?id=607954">do a rundown on a number of different algorithms</a>. Not surprisingly, the one demonstrated above is indeed the most naive.</p>
]]></content:encoded>
			<wfw:commentRss>http://justin.harmonize.fm/index.php/2009/05/exploring-memcmp/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
