<?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>Cameron McKay</title>
	<atom:link href="http://cdmckay.org/blog/feed/" rel="self" type="application/rss+xml" />
	<link>http://cdmckay.org/blog</link>
	<description>Freelance Programmer and Language Polyglot</description>
	<lastBuildDate>Fri, 10 May 2013 05:16:26 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.5.1</generator>
		<item>
		<title>Process Street is looking for a UI/UX Co-Founder</title>
		<link>http://cdmckay.org/blog/2013/05/10/process-street-is-looking-for-a-uiux-co-founder/</link>
		<comments>http://cdmckay.org/blog/2013/05/10/process-street-is-looking-for-a-uiux-co-founder/#comments</comments>
		<pubDate>Fri, 10 May 2013 04:23:43 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[Process Street]]></category>

		<guid isPermaLink="false">http://cdmckay.org/blog/?p=2247</guid>
		<description><![CDATA[We are looking for a team member to join as a co-founder for a new startup called Process Street. Process Street is building a collaborative process management system targeted at the SME market. We are a brand new startup, working to build our MVP. We currently have 2 of our desired 3 &#8220;Dream Team&#8221;. We [...]]]></description>
				<content:encoded><![CDATA[<p>We are looking for a team member to join as a co-founder for a new startup called <a href="http://process.st" target="_blank">Process Street</a>. Process Street is building a collaborative process management system targeted at the SME market.</p>
<p>We are a brand new startup, working to build our <a href="https://en.wikipedia.org/wiki/Minimum_viable_product" target="_blank">MVP</a>. We currently have 2 of our desired 3 &#8220;Dream Team&#8221;. We have our Hustler (business/marketing) and our Hacker (coding/sys-admin), we just need our Hipster (UI/UX) co-founder to help make our dream a reality.</p>
<p>We are Australian and Canadian and the company is based in Delaware, USA, but we are living and working in Buenos Aires, where we plan to build out the initial product and work on doing market testing and gaining traction. If everything goes well, we plan to move to San Francisco (Silicon Valley) to scale out the business.</p>
<p><span id="more-2247"></span></p>
<h3>The Role</h3>
<p>You will be part of a 3 man team, working to build a highly needed, super useful product that helps businesses make more money. Easy sell.</p>
<p>Because you are coming in at the ground level, you will be entitled to founders equity &#8211; which we all know is going to be worth BILLION$ in just a few short months <img src='http://cdmckay.org/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </p>
<h3>Responsibilities</h3>
<ul>
<li>User EXPERIENCE. Yes, that&#8217;s experience, not design, or interface, or graphics, or logos&#8230; EXPERIENCE.<br />
We are building a B2B product, and our first and foremost focus is to make sure our product is and simple, quick and painless to use. Because time is money for businesses, and unless we can make our product as easy to use as possible, we will fail.</li>
<li>User Interface.<br />
Yes, you will still be doing design work and will be responsible for the look and feel of all of the areas of the startup. This includes product interface design, marketing site design, logos, color schemes etc&#8230; BUT WAIT. If you are a kick ass user experience person who hates thinking about colors, we still want to talk to you <img src='http://cdmckay.org/blog/wp-includes/images/smilies/icon_smile.gif' alt=':)' class='wp-smiley' /> </li>
<li>Front End HTML/CSS. Putting your UI/UX designs into action. You know the deal.</li>
<li>Countless other startup hats including content, customer support, testing, marketing, etc.</li>
</ul>
<h3>Requirements</h3>
<ul>
<li>UX Freak.<br />
We are looking for someone who lives and breathes UX. UX is your shit, you know what I mean. Websites, web apps, mobile, TV, you are interested in all interfaces. You are on a mission to optimize design and streamline the way humans interact with technology. You have a focus on functionality, simplicity, customer conversion and retention as your key experience design principals. Not pretty colors (not that we don&#8217;t like pretty colors).</li>
<li>Outstanding Overall Communicator (English).<br />
You gotsa know how to read, write, talk, yell, sing, shout, etc.</li>
<li>A flexible life situation.</li>
<li>An understanding of how startups operate (understanding this job description is a good start).</li>
<li>A burning desire to do something BIG.</li>
</ul>
<p>Contact <a href="mailto:vinay@vskill.com">vinay@vskill.com</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2013/05/10/process-street-is-looking-for-a-uiux-co-founder/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Pajama: Client and Server Validation Simplified</title>
		<link>http://cdmckay.org/blog/2012/12/06/pajama-client-and-server-validation-simplified/</link>
		<comments>http://cdmckay.org/blog/2012/12/06/pajama-client-and-server-validation-simplified/#comments</comments>
		<pubDate>Thu, 06 Dec 2012 16:02:50 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://cdmckay.org/blog/?p=2185</guid>
		<description><![CDATA[When implementing an HTML form that requires validation, a developer must make a decision: should I use client-side validation, server-side validation, or both? In this article I will give a quick overview of the pros and cons of these validation techniques, as well as introduce my solution to the problem: Pajama. Client-side Validation Performing validation [...]]]></description>
				<content:encoded><![CDATA[<p>When implementing an HTML form that requires validation, a developer must make a decision: should I use client-side validation, server-side validation, or both?</p>
<p>In this article I will give a quick overview of the pros and cons of these validation techniques, as well as introduce my solution to the problem: <a href="https://github.com/cdmckay/pajama" target="_blank">Pajama</a>.</p>
<p><span id="more-2185"></span></p>
<h3>Client-side Validation</h3>
<p>Performing validation on the client has the advantage of being responsive to the user, as the data can be validated without communicating with the server.</p>
<p>On the other hand, client-side validation has the enormous disadvantage in that it cannot be trusted by the server. The client is in total control of what is being sent, meaning there is no way for a developer to guarantee that the browser is sending valid data.  As a result, client-side validation must be combined with some sort of server-side validation in order to ensure the client isn&#8217;t tampering with the data.</p>
<h3>Server-side Validation</h3>
<p>With that in mind, server-side validation seems like a much better solution. Since there are no trust issues with server-side validation, a developer has the option of foregoing client-side validation altogether.</p>
<p>Unfortunately, server-side validation has the downside that every time the client attempts validation, the entire form must be sent to the server. Worse yet, with some server-side validation schemes, all that data must then be sent back to the client and re-rendered if validation fails. This can happen several times for a single form leading to a significant amount of wasted bandwidth, especially for high-traffic sites.</p>
<h3>Hybrid Validation</h3>
<p>This brings us to hybrid validation. Under this validation scheme, data is first validated by the client. Once the client is satisfied the data is valid, it is then sent to the server and re-validated. This allows a form to have the responsiveness and efficiency of client-side validation with the trust of server-side validation. If a client tampers with the data before it is sent, the server-side validation will catch it. </p>
<p>Hybrid validation isn&#8217;t without it&#8217;s own drawbacks, however. Unlike client- or server-side validation, hybrid validation takes place in two different spots: the browser and the server.  This adds to the implementation complexity. Complicating matters further, the client and server typically do not use the same programming language. This leads to a lot of questions: How do we validate the same way on client and server? Do we need to define our rules twice, once in JavaScript and once in the other language? What happens if the rules need to change? Do we have to remember to update the rules in both languages each time? What if each language has a different validation engine? Do I need to learn both? </p>
<p>This is where Pajama comes in.</p>
<h3>Pajama</h3>
<p>Pajama is a model validator written in PHP. It uses the same rule system as the popular <a href="http://bassistance.de/jquery-plugins/jquery-plugin-validation/" target="_blank">jQuery Validation plugin</a> and has a similar API.  For example, here is how a validator is setup with the jQuery Validation plugin:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;form&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">validate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
    rules<span style="color: #339933;">:</span> <span style="color: #009900;">&#123;</span>
        first_name<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;required&quot;</span><span style="color: #339933;">,</span>
        last_name<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;required&quot;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    submitHandler<span style="color: #339933;">:</span> <span style="color: #000066; 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: #660066;">post</span><span style="color: #009900;">&#40;</span>...<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>&#8230;and here&#8217;s how it&#8217;s setup in Pajama:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;">\Pajama\Validator<span style="color: #339933;">::</span><span style="color: #004000;">validate</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
     <span style="color: #0000ff;">'model'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$_POST</span><span style="color: #339933;">,</span>
     <span style="color: #0000ff;">'rules'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
          <span style="color: #0000ff;">'first_name'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'required'</span><span style="color: #339933;">,</span>
          <span style="color: #0000ff;">'last_name'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #0000ff;">'required'</span><span style="color: #339933;">,</span>
     <span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
     <span style="color: #0000ff;">'validHandler'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000000; 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: #666666; font-style: italic;">// Store in database.</span>
     <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>    
<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Notice the similarities in the API. If someone knows how to use the jQuery Validation plugin, they&#8217;ll have a good idea of how to use Pajama. </p>
<p>Also notice how the rules are defined. Except for the differences in language syntax (i.e. <code>{ foo: "bar" }</code> versus <code>array('foo' => 'bar')</code>), they&#8217;re identical. This is where the power of Pajama lies. Pajama implements all the standard jQuery Validation methods. As a result, most rulesets used by the jQuery Validation plugin can also be used with Pajama with minimal modification to account for syntax. But wouldn&#8217;t it be nice if we didn&#8217;t have to do any modifications?</p>
<h3>JSON To The Rescue</h3>
<p>One of the biggest issues with hybrid validation is figuring out a way to share the rules between two different programming languages. Pajama solves this issue by using a common JSON rules file. Why does Pajama use a JSON rules file? Well, first off, the jQuery Validation plugin already defines its rules as JavaScript literals, so converting them to JSON is a cinche. Better yet, PHP can easily convert to and from JSON using the <a href="http://php.net/manual/en/function.json-encode.php" target="_blank">json_encode</a> and <a href="http://php.net/manual/en/function.json-decode.php" target="_blank">json_decode</a> functions. Thus, since both JavaScript and PHP can speak JSON, sharing rules between the languages in that format is straightforward and convenient.</p>
<p>To see how simple it is, here&#8217;s how the rules from the above example would look as a standalone JSON file:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#123;</span>
    <span style="color: #3366CC;">&quot;first_name&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;required&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #3366CC;">&quot;last_name&quot;</span><span style="color: #339933;">:</span><span style="color: #3366CC;">&quot;required&quot;</span>    
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Next, to use them with client-side JavaScript, we&#8217;d modify the previous example to:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> form <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;form&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">on</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;submit&quot;</span><span style="color: #339933;">,</span> <span style="color: #000066; 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: #006600; font-style: italic;">// Disable submit while loading rules.</span>
    <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span> 
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
$.<span style="color: #660066;">getJSON</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;rules.json&quot;</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>rules<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #006600; font-style: italic;">// Enable submit now that rules are loaded.</span>
    form.<span style="color: #660066;">off</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;submit&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    form.<span style="color: #660066;">validate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>
        rules<span style="color: #339933;">:</span> rules<span style="color: #339933;">,</span>
        submitHandler<span style="color: #339933;">:</span> <span style="color: #000066; 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: #660066;">post</span><span style="color: #009900;">&#40;</span>...<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>&#8230;and modify the PHP slightly to:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;">\Pajama\Validator<span style="color: #339933;">::</span><span style="color: #004000;">validate</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
     <span style="color: #0000ff;">'model'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000088;">$_POST</span><span style="color: #339933;">,</span>
     <span style="color: #0000ff;">'rules'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #990000;">json_decode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'rules.json'</span><span style="color: #339933;">,</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
     <span style="color: #0000ff;">'validHandler'</span> <span style="color: #339933;">=&gt;</span> <span style="color: #000000; 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: #666666; font-style: italic;">// Store in database.</span>
     <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>    
<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Presto! We now have a single rules file being used by both the client and the server to validate. If any of the rules need to change, we just need to edit that single file.</p>
<h3>And That&#8217;s Not All</h3>
<p>In addition to the basic functionality outlined in this post, Pajama also <a href="https://github.com/cdmckay/pajama#custom-validators" target="_blank">supports custom validation methods</a> and <a href="https://github.com/cdmckay/pajama#limitations" target="_blank">a small subset of CSS selectors</a> that can be used for conditional rules.  For more information on these subjects, <a href="https://github.com/cdmckay/pajama" target="_blank">refer to the GitHub page</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2012/12/06/pajama-client-and-server-validation-simplified/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Counting Syllables and Detecting Rhyme in PHP</title>
		<link>http://cdmckay.org/blog/2012/08/15/counting-syllables-and-detecting-rhyme-in-php/</link>
		<comments>http://cdmckay.org/blog/2012/08/15/counting-syllables-and-detecting-rhyme-in-php/#comments</comments>
		<pubDate>Wed, 15 Aug 2012 16:27:37 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://cdmckay.org/blog/?p=2067</guid>
		<description><![CDATA[I was looking at a Software Developer posting on the FreshBooks careers page the other day, and near the end of the &#8220;How to apply&#8221; instructions there was a curious sentence: &#8220;If you want to prove you&#8217;re really paying attention, include a verse of ottava rima and a link to your GitHub profile with your [...]]]></description>
				<content:encoded><![CDATA[<p>I was looking at a <a href="http://www.freshbooks.com/careers/software-developer.php" target="_blank">Software Developer posting</a> on the FreshBooks careers page the other day, and near the end of the &#8220;How to apply&#8221; instructions there was a curious sentence:</p>
<blockquote><p>&#8220;If you want to prove you&#8217;re really paying attention, <strong>include a verse of ottava rima</strong> and a link to your GitHub profile with your application and you&#8217;re guaranteed to have your application reviewed by our Software Development Manager.&#8221;</p></blockquote>
<p>I&#8217;d never heard of the <a href="http://en.wikipedia.org/wiki/Ottava_rima" target="_blank">ottava rima</a> rhyming stanza form before, but a quick trip to Wikipedia remedied that.  Essentially, an ottava rima stanza must satisfy three rules:</p>
<ol>
<li>Each stanza must have 8 lines,</li>
<li>The lines must be iambic pentameter,</li>
<li>The stanza must have the rhyming format a-b-a-b-a-b-c-c.</li>
</ol>
<p>Here is an example of an ottava rima stanza by <a href="http://en.wikipedia.org/wiki/John_Hookham_Frere" target="_blank">Frere</a> (as given in the Wikipedia article):</p>
<blockquote><p>But chiefly, when the shadowy moon had shed<br />
O&#8217;er woods and waters her mysterious hue,<br />
Their passive hearts and vacant fancies fed<br />
With thoughts and aspirations strange and new,<br />
Till their brute souls with inward working bred<br />
Dark hints that in the depths of instinct grew<br />
Subjection not from Locke&#8217;s associations,<br />
Nor David Hartley&#8217;s doctrine of vibrations.</p></blockquote>
<p>The simplicity of the rules got me thinking: <strong>how hard would it be to write a program to check if a poem stanza is ottava rima?</strong></p>
<p>In this article, we will write a simple ottava rima detector in PHP.  </p>
<p><em>(If you&#8217;re not interested in the details, you can skip to <a href="https://github.com/cdmckay/is-ottava-rima" target="_blank">the code</a> at <a href="https://github.com/" target="_blank">GitHub</a>.)</em></p>
<p><span id="more-2067"></span></p>
<h3>The Detector Function</h3>
<p>In our PHP program, the ottava rima detector will be a function named <code>is_ottava_rima</code> that will return true or false depending on whether the stanza string passed to it satisfies the 3 ottava rima rules.  The general structure looks something like this:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> is_ottava_rima<span style="color: #009900;">&#40;</span>
    <span style="color: #000088;">$stanza</span><span style="color: #339933;">,</span> 
    <span style="color: #000088;">$delimiter</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> 
    <span style="color: #000088;">$syllable_tolerance</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span>
<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$lines</span> <span style="color: #339933;">=</span> extract_lines_from<span style="color: #009900;">&#40;</span><span style="color: #000088;">$stanza</span><span style="color: #339933;">,</span> <span style="color: #000088;">$delimiter</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Rule 1.</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$lines</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!==</span> <span style="color: #cc66cc;">8</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Rule 2.</span>
    <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$lines</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$line</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><span style="color: #339933;">!</span>is_iambic_pentameter<span style="color: #009900;">&#40;</span><span style="color: #000088;">$line</span><span style="color: #339933;">,</span> <span style="color: #000088;">$syllable_tolerance</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Rule 3.</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>is_abababcc_rhyme<span style="color: #009900;">&#40;</span><span style="color: #000088;">$lines</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>For the remainder of this article, we will explore how to implement each of the <code>is_ottava_rima</code> sub-functions in detail.</p>
<h3>Extracting Lines</h3>
<p>The second rule of our ottava rima detector states:</p>
<blockquote><p>1. Each stanza must have 8 lines.</p></blockquote>
<p>This is the most straightforward part of the detector.  The <code>extract_lines_from</code> function separates the stanza into lines which are then counted. Here&#8217;s what the code looks like:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> is_ottava_rima<span style="color: #009900;">&#40;</span>
    <span style="color: #000088;">$stanza</span><span style="color: #339933;">,</span> 
    <span style="color: #000088;">$delimiter</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span> 
    <span style="color: #000088;">$syllable_tolerance</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span>
<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$lines</span> <span style="color: #339933;">=</span> extract_lines_from<span style="color: #009900;">&#40;</span><span style="color: #000088;">$stanza</span><span style="color: #339933;">,</span> <span style="color: #000088;">$delimiter</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Rule 1.</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$lines</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!==</span> <span style="color: #cc66cc;">8</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #339933;">...</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> extract_lines_from<span style="color: #009900;">&#40;</span><span style="color: #000088;">$stanza</span><span style="color: #339933;">,</span> <span style="color: #000088;">$delimiter</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>    
    <span style="color: #666666; font-style: italic;">// Separate the stanza into lines.</span>
    <span style="color: #b1b100;">return</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$delimiter</span><span style="color: #339933;">,</span> <span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$stanza</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h3>Phonetic Transcription</h3>
<p>Before we move on to the metre and rhyme rules, we need to briefly discuss the concept phonetic transcription.</p>
<p>In order for a program to be able to accurately count how many syllables a word has, or compare two words for rhyme, it needs to first transform the words into a <a href="https://en.wikipedia.org/wiki/Phonetic_transcription" target="_blank">phonetic transcription</a>.  This transform is necessary because, in English, the written form of a word, the <a href="https://en.wikipedia.org/wiki/Orthography" target="_blank">orthography</a>, can differ from the pronunciation of it.  For example, the words &#8220;bough&#8221; and &#8220;trough&#8221; do not rhyme in English, even though their spellings might suggest they do.</p>
<p>Our detector will use a combination of two phonetic transcriptions: Arpabet and Metaphone code, although Metaphone code will only be used as a last resort in rhyme detection.</p>
<p><a href="https://en.wikipedia.org/wiki/Arpabet" target="_blank">Arpabet</a>, developed by Advanced Research Projects Agency (ARPA), represents each phoneme of General American English with a distinct sequence of ASCII characters.  Here is an example of <a href="https://en.wikipedia.org/wiki/Arpabet#Symbols" target="_blank">some of the phonemes</a>:</p>
<table>
<tr>
<th>Arpabet</th>
<th>Examples</th>
</tr>
<tr>
<td>AO</td>
<td>off (AO1 F); fall (F AO1 L); frost (F R AO1 S T)</td>
</tr>
<tr>
<td>EY</td>
<td>say (S EY1); eight (EY1 T)</td>
</tr>
<tr>
<td>P</td>
<td>pay (P EY1)</td>
</tr>
</table>
<p>Unfortunately, there is no algorithm for taking an English word and converting it to an Arpabet representation.  There is, however, a dictionary called the <a href="http://en.wikipedia.org/wiki/CMU_Pronouncing_Dictionary" target="_blank">CMU Pronouncing Dictionary</a> (CMUDict), that allows a program to convert ~120,000 English words to their Arpabet equivalents.</p>
<p><a href="https://en.wikipedia.org/wiki/Metaphone" target="_blank">Metaphone</a> is a phonetic algorithm that can transcribe any English word into Metaphone code.  Unfortunately, the resulting transcriptions are not as accurate Arpabet.  There are 3 versions of Metaphone (Metaphone, Double Metaphone and Metaphone 3).  In our detector, we will be using the original Metaphone algorithm that is included in PHP.</p>
<h3>Detecting Iambic Pentameter</h3>
<p>The second rule of our ottava rima detector states:</p>
<blockquote><p>2. The lines must be iambic pentameter.</p></blockquote>
<p>What does it mean for something to be <a href="https://en.wikipedia.org/wiki/Iamb_(foot)" target="_blank">iambic</a>?  In English, it refers to a <a href="https://en.wikipedia.org/wiki/Metrical_foot" target="_blank">metrical foot</a> comprising of an unstressed syllable followed by a stressed syllable (i.e. &#8220;da-DUM&#8221;).  Thus, in the case of <a href="https://en.wikipedia.org/wiki/Iambic_pentameter" target="_blank">iambic pentameter</a>, there are 5 iambs: &#8220;da-DUM da-DUM da-DUM da-DUM da-DUM&#8221;.</p>
<p>To simplify our detector, we will be looking for a weaker version of iambic pentameter: we won&#8217;t be worrying about the stresses (&#8220;da-DUM&#8221;) and instead just be looking for 2 syllables per iamb (&#8220;da-da&#8221;) for a total of 10 syllables per line.  With this in mind, we can sketch out some code for an <code>is_iambic_pentameter</code> function:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> is_iambic_pentameter<span style="color: #009900;">&#40;</span><span style="color: #000088;">$line</span><span style="color: #339933;">,</span> <span style="color: #000088;">$syllable_tolerance</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$syllables</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$words</span> <span style="color: #339933;">=</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">' '</span><span style="color: #339933;">,</span> <span style="color: #000088;">$line</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$words</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$word</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$syllables</span> <span style="color: #339933;">+=</span> estimate_syllables<span style="color: #009900;">&#40;</span><span style="color: #000088;">$word</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$syllables</span> <span style="color: #339933;">===</span> <span style="color: #cc66cc;">10</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Which brings us to the next question: how do we count the number of syllables in a word?  Well, in order to answer that question, we need ask a more elemental question: what is a syllable?</p>
<p>A <a href="https://en.wikipedia.org/wiki/Syllable" target="_blank">syllable</a> consists of two parts: a syllable nucleus (most often a vowel) with optional initial and final margins (typically, consonants).  So, we can think of a syllable as a vowel with some optional consonant garnishes.  That means that we can roughly estimate the number of syllables in an English word by counting the number of vowels in it.  Let&#8217;s try that:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> estimate_syllables<span style="color: #009900;">&#40;</span><span style="color: #000088;">$word</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #b1b100;">return</span> count_english_vowels<span style="color: #009900;">&#40;</span><span style="color: #000088;">$word</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> count_english_vowels<span style="color: #009900;">&#40;</span><span style="color: #000088;">$word</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    static <span style="color: #000088;">$english_vowels</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'A'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'E'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'I'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'O'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'U'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$vowel_count</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$letters</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_split</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strtoupper</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$word</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$letters</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$letter</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><span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$letter</span><span style="color: #339933;">,</span> <span style="color: #000088;">$english_vowels</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$vowel_count</span><span style="color: #339933;">++;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$vowel_count</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>This heuristic works, but there are cases where it can give incorrect results due to the peculiarities of the orthography.  For example: &#8220;here&#8221; would yield 2 syllables instead of 1 and &#8220;my&#8221; would yield 0 syllables instead of 1.  How can we improve on that?</p>
<p>The answer lies in using the Arpabet phonetic transcription.  Since a phonetic transcription provides a one-to-one mapping between symbols and sound, we can avoid the over- and under-counting we get with words like &#8220;here&#8221; and &#8220;my&#8221;.  To demonstrate, here are the Arpabet transcriptions of &#8220;here&#8221; and &#8220;my&#8221; (with the vowels bolded):</p>
<table>
<tr>
<th>English</th>
<th>Arpabet</th>
<tr>
<tr>
<td>here</td>
<td>HH <strong>IY1</strong> R</td>
<tr>
<td>my</td>
<td>M <strong>AY1</strong></td>
</tr>
</table>
<p>In this case, both words are correctly shown as having a single Arpabet vowel each and therefore a single syllable.  From this somewhat contrived example, we can see that Arpabet more accurately reflect vowel sounds as compared to English orthography.  However, using Arpabet is not without its pitfalls.  First off, it requires more time and memory, as the CMUDict database needs to be loaded into memory.  Second, there are only ~120,000 words in the CMUDict, whereas the English language has ~600,000 words.  That means there are a lot of words that CMUDict cannot transcribe to Arpabet.</p>
<p>Thus, in order to be able to handle any English word, we need to take a hybrid approach:  If the word is in the CMUDict, we use the Arpabet transcription to get an accurate syllable count.  If it&#8217;s not, we use the English vowel counting heuristic.  Here&#8217;s what the code looks like:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> estimate_syllables<span style="color: #009900;">&#40;</span><span style="color: #000088;">$word</span><span style="color: #339933;">,</span> <span style="color: #000088;">$syllable_tolerance</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$syllable_count</span> <span style="color: #339933;">=</span> count_arpabet_vowels<span style="color: #009900;">&#40;</span><span style="color: #000088;">$word</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$syllable_count</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$syllable_count</span> <span style="color: #339933;">=</span> count_english_vowels<span style="color: #009900;">&#40;</span><span style="color: #000088;">$word</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$syllable_count</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> count_arpabet_vowels<span style="color: #009900;">&#40;</span><span style="color: #000088;">$word</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    static <span style="color: #000088;">$arpabet_vowels</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'AO'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'AA'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'IY'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'UW'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'EH'</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">// Monophthongs</span>
        <span style="color: #0000ff;">'IH'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'UH'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'AH'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'AX'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'AE'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'EY'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'AY'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'OW'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'AW'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'OY'</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">// Diphthongs</span>
        <span style="color: #0000ff;">'ER'</span> <span style="color: #666666; font-style: italic;">// R-colored vowels</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$cmu_dict</span> <span style="color: #339933;">=</span> CMUDict<span style="color: #339933;">::</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$phonemes</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$cmu_dict</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPhonemes</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$word</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$phonemes</span> <span style="color: #339933;">!==</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$vowel_count</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$phonemes</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$phoneme</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><span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$phoneme</span><span style="color: #339933;">,</span> <span style="color: #000088;">$arpabet_vowels</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$vowel_count</span><span style="color: #339933;">++;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$vowel_count</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> count_english_vowels<span style="color: #009900;">&#40;</span><span style="color: #000088;">$word</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    static <span style="color: #000088;">$english_vowels</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'A'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'E'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'I'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'O'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'U'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$vowel_count</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$letters</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_split</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strtoupper</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$word</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$letters</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$letter</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><span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$letter</span><span style="color: #339933;">,</span> <span style="color: #000088;">$english_vowels</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$vowel_count</span><span style="color: #339933;">++;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$vowel_count</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>As you might have noticed, there&#8217;s a <code>$syllable_tolerance</code> argument for the <code>is_iambic_pentameter</code> function.  This is to compensate for two facts: </p>
<ul>
<li>the English vowel counting heuristic can under- or over-estimate the number of syllables,</li>
<li>some poems don&#8217;t match iambic pentameter perfectly.</li>
</ul>
<p>Thus, to give the detector a bit of flexibility in detecting ottava rima, there is a <code>$syllable_tolerance</code> variable.  A value of 2 means that the detector will accept lines that have between 8 and 12 syllables.  Here&#8217;s the final code for <code>is_iambic_pentameter</code>:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> is_iambic_pentameter<span style="color: #009900;">&#40;</span><span style="color: #000088;">$line</span><span style="color: #339933;">,</span> <span style="color: #000088;">$syllable_tolerance</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$syllable_count</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$words</span> <span style="color: #339933;">=</span> get_words_from<span style="color: #009900;">&#40;</span><span style="color: #000088;">$line</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$words</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$word</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$syllable_count</span> <span style="color: #339933;">+=</span> estimate_syllables<span style="color: #009900;">&#40;</span><span style="color: #000088;">$word</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000088;">$min_syllable_count</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$syllable_tolerance</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$max_syllable_count</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$syllable_tolerance</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$syllable_count</span> <span style="color: #339933;">&gt;=</span> <span style="color: #000088;">$min_syllable_count</span> <span style="color: #339933;">&amp;&amp;</span> 
           <span style="color: #000088;">$syllable_count</span> <span style="color: #339933;">&lt;=</span> <span style="color: #000088;">$max_syllable_count</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> get_words_from<span style="color: #009900;">&#40;</span><span style="color: #000088;">$line</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$cleaned_line</span> <span style="color: #339933;">=</span> <span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/[^A-Za-z' ]/&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">' '</span><span style="color: #339933;">,</span> <span style="color: #000088;">$line</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #990000;">preg_split</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/\s+/'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$cleaned_line</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>You might have noticed the <code>get_words_from</code> function.  This function removes all superfluous punctuation that many poets are so fond of, and then separates the line string into an array of word strings.  The reason this logic has been given its own function is because it is used again when detecting rhyme.</p>
<h3>Detecting Rhyme</h3>
<p>The third rule of our ottava rima detector states:</p>
<blockquote><p>3. The stanza must have the rhyming format a-b-a-b-a-b-c-c.</p></blockquote>
<p><a href="http://en.wikipedia.org/wiki/Rhyme" target="_blank">Rhyme</a>, according to Wikipedia, is:</p>
<blockquote><p>&#8220;a repetition of similar sounds in two or more words and is most often used in poetry and songs.&#8221;</p></blockquote>
<p>There are several different types of rhymes, including perfect rhymes, general rhymes, and mirror rhymes, each with their own subcategories.  In the case of ottava rima, we will be specifically looking for a type of general rhyme called <a href="http://en.wikipedia.org/wiki/Syllable_rhyme" target="_blank">syllabic rhyme</a>, where the last syllable of the words sounds the same.</p>
<p>What this means from a programming perspective is that we need only look at the last syllable of the last word of every line.  Since we already have the <code>get_words_from</code> function for breaking lines into an array of words, all we need to do is grab the last word of that array, like so:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> does_rhyme<span style="color: #009900;">&#40;</span><span style="color: #000088;">$line1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$line2</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$words1</span> <span style="color: #339933;">=</span> get_words_from<span style="color: #009900;">&#40;</span><span style="color: #000088;">$line1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$last_word1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$words1</span><span style="color: #009900;">&#91;</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$words1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$words2</span> <span style="color: #339933;">=</span> get_words_from<span style="color: #009900;">&#40;</span><span style="color: #000088;">$line2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$last_word2</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$words2</span><span style="color: #009900;">&#91;</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$words2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #339933;">...</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Next, we need to check if the words are in the CMUDict and, if they are, we retrieve their corresponding Arpabet phonemes.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> does_rhyme<span style="color: #009900;">&#40;</span><span style="color: #000088;">$line1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$line2</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #339933;">...</span>
&nbsp;
    <span style="color: #000088;">$words_found</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$cmu_dict</span> <span style="color: #339933;">=</span> CMUDict<span style="color: #339933;">::</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$phonemes1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$cmu_dict</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPhonemes</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$last_word1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$phonemes1</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$words_found</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000088;">$phonemes2</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$cmu_dict</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPhonemes</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$last_word2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$phonemes2</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$words_found</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #339933;">...</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Once we know whether or not the CMUDict contains the words, we can check whether or not they rhyme by comparing the last syllable of each word. If the CMUDict did not contain either of the words then we instead compare the last Metaphone code symbol of each word.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> does_rhyme<span style="color: #009900;">&#40;</span><span style="color: #000088;">$line1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$line2</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #339933;">...</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$words_found</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$last_syllable1</span> <span style="color: #339933;">=</span> get_last_syllable_of<span style="color: #009900;">&#40;</span><span style="color: #000088;">$phonemes1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$last_syllable2</span> <span style="color: #339933;">=</span> get_last_syllable_of<span style="color: #009900;">&#40;</span><span style="color: #000088;">$phonemes2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$rhymes</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$last_syllable1</span> <span style="color: #339933;">===</span> <span style="color: #000088;">$last_syllable2</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$metaphone1</span> <span style="color: #339933;">=</span> <span style="color: #990000;">metaphone</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$last_word1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$metaphone2</span> <span style="color: #339933;">=</span> <span style="color: #990000;">metaphone</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$last_word2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$rhymes</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$metaphone1</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$metaphone2</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$rhymes</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #990000;">error_log</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$last_word1</span> and <span style="color: #006699; font-weight: bold;">$last_word2</span> don't rhyme.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$rhymes</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>How do we get the last syllable of an Arpabet transcribed word?  Well, as we know from earlier, the vowel is the nucleus of a syllable.  So, if we scan an Arpabet transcription backwards for the last vowel then chop off everything before it, we can approximately get the last syllable.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> get_last_syllable_of<span style="color: #009900;">&#40;</span><span style="color: #000088;">$phonemes</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    static <span style="color: #000088;">$arpabet_vowels</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'AO'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'AA'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'IY'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'UW'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'EH'</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">// Monophthongs</span>
        <span style="color: #0000ff;">'IH'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'UH'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'AH'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'AX'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'AE'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'EY'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'AY'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'OW'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'AW'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'OY'</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">// Diphthongs</span>
        <span style="color: #0000ff;">'ER'</span> <span style="color: #666666; font-style: italic;">// R-colored vowels</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$reversed_syllable_phonemes</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">array_reverse</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$phonemes</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$phoneme</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$reversed_syllable_phonemes</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$phoneme</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$phoneme</span><span style="color: #339933;">,</span> <span style="color: #000088;">$arpabet_vowels</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #990000;">implode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #990000;">array_reverse</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$reversed_syllable_phonemes</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>That completes the <code>does_rhyme</code> function for determining if two lines rhyme.  Now, in order to determine if an array of lines has the a-b-a-b-a-b-c-c rhyming scheme, we simply need to apply that function to the appropriate lines:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> is_abababcc_rhyme<span style="color: #009900;">&#40;</span><span style="color: #000088;">$lines</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$a1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$b1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$a2</span><span style="color: #339933;">,</span> <span style="color: #000088;">$b2</span><span style="color: #339933;">,</span> <span style="color: #000088;">$a3</span><span style="color: #339933;">,</span> <span style="color: #000088;">$b3</span><span style="color: #339933;">,</span> <span style="color: #000088;">$c1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$c2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$lines</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$a_rhyme</span> <span style="color: #339933;">=</span> does_rhyme<span style="color: #009900;">&#40;</span><span style="color: #000088;">$a1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$a2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> 
               does_rhyme<span style="color: #009900;">&#40;</span><span style="color: #000088;">$a2</span><span style="color: #339933;">,</span> <span style="color: #000088;">$a3</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> 
               does_rhyme<span style="color: #009900;">&#40;</span><span style="color: #000088;">$a1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$a3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$b_rhyme</span> <span style="color: #339933;">=</span> does_rhyme<span style="color: #009900;">&#40;</span><span style="color: #000088;">$b1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$b2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> 
               does_rhyme<span style="color: #009900;">&#40;</span><span style="color: #000088;">$b2</span><span style="color: #339933;">,</span> <span style="color: #000088;">$b3</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> 
               does_rhyme<span style="color: #009900;">&#40;</span><span style="color: #000088;">$b1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$b3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$c_rhyme</span> <span style="color: #339933;">=</span> does_rhyme<span style="color: #009900;">&#40;</span><span style="color: #000088;">$c1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$c2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$a_rhyme</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$b_rhyme</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$c_rhyme</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h3>Bringing It All Together</h3>
<p>That completes the simplified ottava rima detector.  Here is what <a href="https://github.com/cdmckay/is-ottava-rima/blob/master/is_ottava_rima.php" target="_blank">the final code</a> looks like:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span>
<span style="color: #b1b100;">require_once</span> __DIR__ <span style="color: #339933;">.</span> <span style="color: #0000ff;">'/classes/CMUDict.class.php'</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> is_ottava_rima<span style="color: #009900;">&#40;</span>
    <span style="color: #000088;">$stanza</span><span style="color: #339933;">,</span>
    <span style="color: #000088;">$delimiter</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #339933;">,</span>
    <span style="color: #000088;">$syllable_tolerance</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">2</span>
<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$lines</span> <span style="color: #339933;">=</span> extract_lines_from<span style="color: #009900;">&#40;</span><span style="color: #000088;">$stanza</span><span style="color: #339933;">,</span> <span style="color: #000088;">$delimiter</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Rule 1.</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$lines</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!==</span> <span style="color: #cc66cc;">8</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Rule 2.</span>
    <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$lines</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$line</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><span style="color: #339933;">!</span>is_iambic_pentameter<span style="color: #009900;">&#40;</span><span style="color: #000088;">$line</span><span style="color: #339933;">,</span> <span style="color: #000088;">$syllable_tolerance</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// Rule 3.</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span>is_abababcc_rhyme<span style="color: #009900;">&#40;</span><span style="color: #000088;">$lines</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> extract_lines_from<span style="color: #009900;">&#40;</span><span style="color: #000088;">$stanza</span><span style="color: #339933;">,</span> <span style="color: #000088;">$delimiter</span> <span style="color: #339933;">=</span> <span style="color: #0000ff;">&quot;<span style="color: #000099; font-weight: bold;">\n</span>&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// Separate the stanza into lines.</span>
    <span style="color: #b1b100;">return</span> <span style="color: #990000;">explode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$delimiter</span><span style="color: #339933;">,</span> <span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$stanza</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> is_iambic_pentameter<span style="color: #009900;">&#40;</span><span style="color: #000088;">$line</span><span style="color: #339933;">,</span> <span style="color: #000088;">$syllable_tolerance</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$syllable_count</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$words</span> <span style="color: #339933;">=</span> get_words_from<span style="color: #009900;">&#40;</span><span style="color: #000088;">$line</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$words</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$word</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$syllable_count</span> <span style="color: #339933;">+=</span> estimate_syllables<span style="color: #009900;">&#40;</span><span style="color: #000088;">$word</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000088;">$min_syllable_count</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10</span> <span style="color: #339933;">-</span> <span style="color: #000088;">$syllable_tolerance</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$max_syllable_count</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">10</span> <span style="color: #339933;">+</span> <span style="color: #000088;">$syllable_tolerance</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$syllable_count</span> <span style="color: #339933;">&gt;=</span> <span style="color: #000088;">$min_syllable_count</span> <span style="color: #339933;">&amp;&amp;</span> 
           <span style="color: #000088;">$syllable_count</span> <span style="color: #339933;">&lt;=</span> <span style="color: #000088;">$max_syllable_count</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> get_words_from<span style="color: #009900;">&#40;</span><span style="color: #000088;">$line</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$cleaned_line</span> <span style="color: #339933;">=</span> <span style="color: #990000;">trim</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">preg_replace</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;/[^A-Za-z' ]/&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">' '</span><span style="color: #339933;">,</span> <span style="color: #000088;">$line</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #990000;">preg_split</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'/\s+/'</span><span style="color: #339933;">,</span> <span style="color: #000088;">$cleaned_line</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> estimate_syllables<span style="color: #009900;">&#40;</span><span style="color: #000088;">$word</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$syllable_count</span> <span style="color: #339933;">=</span> count_arpabet_vowels<span style="color: #009900;">&#40;</span><span style="color: #000088;">$word</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$syllable_count</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$syllable_count</span> <span style="color: #339933;">=</span> count_english_vowels<span style="color: #009900;">&#40;</span><span style="color: #000088;">$word</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$syllable_count</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> count_arpabet_vowels<span style="color: #009900;">&#40;</span><span style="color: #000088;">$word</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    static <span style="color: #000088;">$arpabet_vowels</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'AO'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'AA'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'IY'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'UW'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'EH'</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">// Monophthongs</span>
        <span style="color: #0000ff;">'IH'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'UH'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'AH'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'AX'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'AE'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'EY'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'AY'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'OW'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'AW'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'OY'</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">// Diphthongs</span>
        <span style="color: #0000ff;">'ER'</span> <span style="color: #666666; font-style: italic;">// R-colored vowels</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$cmu_dict</span> <span style="color: #339933;">=</span> CMUDict<span style="color: #339933;">::</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$phonemes</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$cmu_dict</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPhonemes</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$word</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$phonemes</span> <span style="color: #339933;">!==</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$vowel_count</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$phonemes</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$phoneme</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><span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$phoneme</span><span style="color: #339933;">,</span> <span style="color: #000088;">$arpabet_vowels</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                <span style="color: #000088;">$vowel_count</span><span style="color: #339933;">++;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#125;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$vowel_count</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> count_english_vowels<span style="color: #009900;">&#40;</span><span style="color: #000088;">$word</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    static <span style="color: #000088;">$english_vowels</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'A'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'E'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'I'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'O'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'U'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$vowel_count</span> <span style="color: #339933;">=</span> <span style="color: #cc66cc;">0</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$letters</span> <span style="color: #339933;">=</span> <span style="color: #990000;">str_split</span><span style="color: #009900;">&#40;</span><span style="color: #990000;">strtoupper</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$word</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$letters</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$letter</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><span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$letter</span><span style="color: #339933;">,</span> <span style="color: #000088;">$english_vowels</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #000088;">$vowel_count</span><span style="color: #339933;">++;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$vowel_count</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> is_abababcc_rhyme<span style="color: #009900;">&#40;</span><span style="color: #000088;">$lines</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #990000;">list</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$a1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$b1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$a2</span><span style="color: #339933;">,</span> <span style="color: #000088;">$b2</span><span style="color: #339933;">,</span> <span style="color: #000088;">$a3</span><span style="color: #339933;">,</span> <span style="color: #000088;">$b3</span><span style="color: #339933;">,</span> <span style="color: #000088;">$c1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$c2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$lines</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$a_rhymes</span> <span style="color: #339933;">=</span> does_rhyme<span style="color: #009900;">&#40;</span><span style="color: #000088;">$a1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$a2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span>
                does_rhyme<span style="color: #009900;">&#40;</span><span style="color: #000088;">$a2</span><span style="color: #339933;">,</span> <span style="color: #000088;">$a3</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span>
                does_rhyme<span style="color: #009900;">&#40;</span><span style="color: #000088;">$a1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$a3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$b_rhymes</span> <span style="color: #339933;">=</span> does_rhyme<span style="color: #009900;">&#40;</span><span style="color: #000088;">$b1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$b2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span>
                does_rhyme<span style="color: #009900;">&#40;</span><span style="color: #000088;">$b2</span><span style="color: #339933;">,</span> <span style="color: #000088;">$b3</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span>
                does_rhyme<span style="color: #009900;">&#40;</span><span style="color: #000088;">$b1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$b3</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$c_rhymes</span> <span style="color: #339933;">=</span> does_rhyme<span style="color: #009900;">&#40;</span><span style="color: #000088;">$c1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$c2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$a_rhymes</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$b_rhymes</span> <span style="color: #339933;">&amp;&amp;</span> <span style="color: #000088;">$c_rhymes</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> does_rhyme<span style="color: #009900;">&#40;</span><span style="color: #000088;">$line1</span><span style="color: #339933;">,</span> <span style="color: #000088;">$line2</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000088;">$words1</span> <span style="color: #339933;">=</span> get_words_from<span style="color: #009900;">&#40;</span><span style="color: #000088;">$line1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$last_word1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$words1</span><span style="color: #009900;">&#91;</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$words1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$words2</span> <span style="color: #339933;">=</span> get_words_from<span style="color: #009900;">&#40;</span><span style="color: #000088;">$line2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$last_word2</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$words2</span><span style="color: #009900;">&#91;</span><span style="color: #990000;">count</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$words2</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">-</span> <span style="color: #cc66cc;">1</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$words_found</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">true</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$cmu_dict</span> <span style="color: #339933;">=</span> CMUDict<span style="color: #339933;">::</span><span style="color: #004000;">get</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000088;">$phonemes1</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$cmu_dict</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPhonemes</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$last_word1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$phonemes1</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$words_found</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000088;">$phonemes2</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$cmu_dict</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">getPhonemes</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$last_word2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$phonemes2</span> <span style="color: #339933;">===</span> <span style="color: #009900; font-weight: bold;">null</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$words_found</span> <span style="color: #339933;">=</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$words_found</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$last_syllable1</span> <span style="color: #339933;">=</span> get_last_syllable_of<span style="color: #009900;">&#40;</span><span style="color: #000088;">$phonemes1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$last_syllable2</span> <span style="color: #339933;">=</span> get_last_syllable_of<span style="color: #009900;">&#40;</span><span style="color: #000088;">$phonemes2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$rhymes</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$last_syllable1</span> <span style="color: #339933;">===</span> <span style="color: #000088;">$last_syllable2</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #b1b100;">else</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$metaphone1</span> <span style="color: #339933;">=</span> <span style="color: #990000;">metaphone</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$last_word1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$metaphone2</span> <span style="color: #339933;">=</span> <span style="color: #990000;">metaphone</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$last_word2</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$rhymes</span> <span style="color: #339933;">=</span> <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$metaphone1</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> 
                  <span style="color: #990000;">substr</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$metaphone2</span><span style="color: #339933;">,</span> <span style="color: #339933;">-</span><span style="color: #cc66cc;">1</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #339933;">!</span><span style="color: #000088;">$rhymes</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #990000;">error_log</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;<span style="color: #006699; font-weight: bold;">$last_word1</span> and <span style="color: #006699; font-weight: bold;">$last_word2</span> don't rhyme.&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
&nbsp;
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$rhymes</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">function</span> get_last_syllable_of<span style="color: #009900;">&#40;</span><span style="color: #000088;">$phonemes</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    static <span style="color: #000088;">$arpabet_vowels</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
        <span style="color: #0000ff;">'AO'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'AA'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'IY'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'UW'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'EH'</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">// Monophthongs</span>
        <span style="color: #0000ff;">'IH'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'UH'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'AH'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'AX'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'AE'</span><span style="color: #339933;">,</span>
        <span style="color: #0000ff;">'EY'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'AY'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'OW'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'AW'</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">'OY'</span><span style="color: #339933;">,</span> <span style="color: #666666; font-style: italic;">// Diphthongs</span>
        <span style="color: #0000ff;">'ER'</span> <span style="color: #666666; font-style: italic;">// R-colored vowels</span>
    <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$reversed_syllable_phonemes</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">array_reverse</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$phonemes</span><span style="color: #009900;">&#41;</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$phoneme</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$reversed_syllable_phonemes</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$phoneme</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #990000;">in_array</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$phoneme</span><span style="color: #339933;">,</span> <span style="color: #000088;">$arpabet_vowels</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #b1b100;">break</span><span style="color: #339933;">;</span>
        <span style="color: #009900;">&#125;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #990000;">implode</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">''</span><span style="color: #339933;">,</span> <span style="color: #990000;">array_reverse</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$reversed_syllable_phonemes</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h3>Does It Work?</h3>
<p>Of course, there&#8217;s no point in writing all this code unless we test it.  To that end, a <a href="https://github.com/cdmckay/is-ottava-rima/blob/master/is_ottava_rima_test.php" target="_blank">test program</a> was written that analyzed a smattering of ottava rima poems found on <a href="http://rpo.library.utoronto.ca/" target="_blank">the University of Toronto Representative Poetry Online site</a>.  The test program analyzed ottava rima stanzas (for false negatives) and non ottava rima stanzas (for false positives).  These were the results:</p>
<table>
<tr>
<th>Rhyme</th>
<th>Stanzas</th>
<th>Detected</th>
</tr>
<tr>
<td>Ottava rima</td>
<td>23</td>
<td>52.17%</td>
</tr>
<tr>
<td>Garbage</td>
<td>15</td>
<td>0%</td>
</tr>
</table>
<p>From this rather limited testing, we can can conclude that the detector has a 47.83% chance of false negatives and 0% chance of false positives.</p>
<p>So the detector works, but there is clearly room for improvement.</p>
<h3>Conclusion</h3>
<p>In this article we implemented a simple ottava rima detector in PHP.  This was done by analyzing a string of text representing a poem stanza for the three characteristic properties of an ottava rima poem.  The resulting program detected ottava rima stanzas correctly 52% of the time and never detected an non-ottava rima stanzas as ottava rima.</p>
<p>The final code, including the test program and test poetry, is available for download at <a href="https://github.com/cdmckay/is-ottava-rima" target="_blank">GitHub</a>.</p>
]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2012/08/15/counting-syllables-and-detecting-rhyme-in-php/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Creating ASP.NET HttpCookie-compatible multi-valued cookies in JavaScript</title>
		<link>http://cdmckay.org/blog/2012/02/12/creating-asp-net-httpcookie-compatible-multi-valued-cookies-in-javascript/</link>
		<comments>http://cdmckay.org/blog/2012/02/12/creating-asp-net-httpcookie-compatible-multi-valued-cookies-in-javascript/#comments</comments>
		<pubDate>Sun, 12 Feb 2012 21:40:31 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://cdmckay.org/blog/?p=1969</guid>
		<description><![CDATA[Cookies are ubiquitous on the web. They&#8217;re used to store usernames, login tokens, shopping cart contents, and so on. In libraries, cookies are typically modeled as name-value pairs. An API consumer requests a cookie using a known name (e.g. $.cookie('user') in jQuery with the Cookie plugin) and the library returns a string (e.g. alice). Sometimes, [...]]]></description>
				<content:encoded><![CDATA[<p>Cookies are ubiquitous on the web.  They&#8217;re used to store usernames, login tokens, shopping cart contents, and so on.  In libraries, cookies are typically modeled as name-value pairs.  An API consumer requests a cookie using a known name (e.g. <code>$.cookie('user')</code> in jQuery with the Cookie plugin) and the library returns a string (e.g. <code>alice</code>).  Sometimes, however, just plain old name-value pairs aren&#8217;t enough.  In this article, we will take a look at how to bake multi-valued cookies in JavaScript that are compatible with the <a href="http://msdn.microsoft.com/en-us/library/system.web.httpcookie.values.aspx">Values</a> property of ASP.NET&#8217;s <a href="http://msdn.microsoft.com/en-us/library/system.web.httpcookie.aspx">HttpCookie</a> class.</p>
<p><span id="more-1969"></span></p>
<h3>Serialization</h3>
<p>The .NET HttpCookie class stores multiple values in a single cookie by <a href="http://en.wikipedia.org/wiki/Serialization">serializing</a> them into a single string.  For example, this code:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;">HttpCookie cookie <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> HttpCookie<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Cookie1&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
cookie<span style="color: #008000;">.</span><span style="color: #0000FF;">Values</span><span style="color: #008000;">&#91;</span><span style="color: #666666;">&quot;Val1&quot;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;1&quot;</span><span style="color: #008000;">;</span>
cookie<span style="color: #008000;">.</span><span style="color: #0000FF;">Values</span><span style="color: #008000;">&#91;</span><span style="color: #666666;">&quot;Val2&quot;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;2&quot;</span><span style="color: #008000;">;</span>
cookie<span style="color: #008000;">.</span><span style="color: #0000FF;">Values</span><span style="color: #008000;">&#91;</span><span style="color: #666666;">&quot;Val3&quot;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;3&quot;</span><span style="color: #008000;">;</span>
Response<span style="color: #008000;">.</span><span style="color: #0000FF;">Cookies</span><span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Add</span><span style="color: #008000;">&#40;</span>cookie<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>&#8230;will result in a cookie that looks like this:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;">Cookie1<span style="color: #008000;">=</span>Val1<span style="color: #008000;">=</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&amp;</span>Val2<span style="color: #008000;">=</span><span style="color: #FF0000;">2</span><span style="color: #008000;">&amp;</span>Val3<span style="color: #008000;">=</span><span style="color: #FF0000;">3</span></pre></td></tr></table></div>

<p>Thus, we can see the HttpCookie serializes the Values property in a similar way to how parameters are encoded in query strings.</p>
<h3>The JavaScript (for MS .NET and Mono)</h3>
<p>Based on the example output <code>Cookie1=Val1=1&#038;Val2=2&#038;Val3=3</code>, we can work out some JavaScript code to generate it:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Works with Microsoft .NET and Mono runtimes, with reservations.</span>
<span style="color: #000066; font-weight: bold;">var</span> setMultiValuedCookie <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>name<span style="color: #339933;">,</span> values<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">var</span> valuePairs <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">var</span> n <span style="color: #000066; font-weight: bold;">in</span> values<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        valuePairs.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>n <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;=&quot;</span> <span style="color: #339933;">+</span> values<span style="color: #009900;">&#91;</span>n<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">var</span> cookieValue <span style="color: #339933;">=</span> valuePairs.<span style="color: #660066;">join</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&amp;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    document.<span style="color: #660066;">cookie</span> <span style="color: #339933;">=</span> name <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;=&quot;</span> <span style="color: #339933;">+</span> cookieValue<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This is more or less how the Microsoft .NET implementation of HttpCookie works.  It also happens to be compatible with the Mono implementation (more on that below). The problem with this code is that it does not take into account edge cases, such as when a name or value contain the &#8220;=&#8221; or &#8220;&amp;&#8221; characters.  In those cases, things can get weird.  For example, consider this code:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;">HttpCookie cookie <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> HttpCookie<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Cookie1&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
cookie<span style="color: #008000;">.</span><span style="color: #0000FF;">Values</span><span style="color: #008000;">&#91;</span><span style="color: #666666;">&quot;Val1&quot;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;1&amp;Val2=2&quot;</span><span style="color: #008000;">;</span>
cookie<span style="color: #008000;">.</span><span style="color: #0000FF;">Values</span><span style="color: #008000;">&#91;</span><span style="color: #666666;">&quot;Val3&quot;</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">=</span> <span style="color: #666666;">&quot;3&quot;</span><span style="color: #008000;">;</span>
Response<span style="color: #008000;">.</span><span style="color: #0000FF;">Cookies</span><span style="color: #008000;">.</span><span style="color: #0600FF; font-weight: bold;">Add</span><span style="color: #008000;">&#40;</span>cookie<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>Notice how only two values are set.  Yet, when encoded by Microsoft&#8217;s HttpCookie class the resulting output is <code>Cookie1=Val1=1&#038;Val2=2&#038;Val3=3</code>, the exact same output that results the previous three value example in article.  However, the cookie, when parsed by HttpCookie, will always be interpreted as having 3 values.  This means that Microsoft&#8217;s .NET HttpCookie serialization scheme is ambiguous and care must be taken when using it with non-alphanumeric characters.</p>
<h3>The JavaScript (just for Mono)</h3>
<p>The <a href="https://github.com/mono" title="Mono Project" target="_blank">Mono Project</a>&#8216;s <a href="https://github.com/mono/mono/blob/master/mcs/class/System.Web/System.Web/HttpCookie.cs" title="HttpCookie.cs" target="_blank">HttpCookie class</a> fares a lot better than its Microsoft counterpart.  In the HttpCookie source, there is a nested CookieNVC class (NVC stands for NameValueCollection) with the serialization code in the overridden ToString method:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #0600FF; font-weight: bold;">override</span> <span style="color: #6666cc; font-weight: bold;">string</span> ToString<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
    StringBuilder builder <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> StringBuilder<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
    <span style="color: #6666cc; font-weight: bold;">bool</span> first_key <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
    <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> key <span style="color: #0600FF; font-weight: bold;">in</span> Keys<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>first_key<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span> 
            builder<span style="color: #008000;">.</span><span style="color: #0000FF;">Append</span><span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;&amp;&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #008000;">&#93;</span> vals <span style="color: #008000;">=</span> GetValues<span style="color: #008000;">&#40;</span>key<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>        
        <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>vals <span style="color: #008000;">==</span> <span style="color: #0600FF; font-weight: bold;">null</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
           vals <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> <span style="color: #6666cc; font-weight: bold;">string</span><span style="color: #008000;">&#91;</span><span style="color: #FF0000;">1</span><span style="color: #008000;">&#93;</span> <span style="color: #008000;">&#123;</span><span style="color: #6666cc; font-weight: bold;">String</span><span style="color: #008000;">.</span><span style="color: #0000FF;">Empty</span><span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>
        <span style="color: #6666cc; font-weight: bold;">bool</span> first_val <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">true</span><span style="color: #008000;">;</span>
        <span style="color: #0600FF; font-weight: bold;">foreach</span> <span style="color: #008000;">&#40;</span><span style="color: #6666cc; font-weight: bold;">string</span> v <span style="color: #0600FF; font-weight: bold;">in</span> vals<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span><span style="color: #008000;">!</span>first_val<span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
               builder<span style="color: #008000;">.</span><span style="color: #0000FF;">Append</span> <span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;&amp;&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>key <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">&amp;&amp;</span> key<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span> <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                builder<span style="color: #008000;">.</span><span style="color: #0000FF;">Append</span> <span style="color: #008000;">&#40;</span>HttpUtility<span style="color: #008000;">.</span><span style="color: #0000FF;">UrlEncode</span><span style="color: #008000;">&#40;</span>key<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
                builder<span style="color: #008000;">.</span><span style="color: #0000FF;">Append</span> <span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;=&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>
            <span style="color: #0600FF; font-weight: bold;">if</span> <span style="color: #008000;">&#40;</span>v <span style="color: #008000;">!=</span> <span style="color: #0600FF; font-weight: bold;">null</span> <span style="color: #008000;">&amp;&amp;</span> v<span style="color: #008000;">.</span><span style="color: #0000FF;">Length</span> <span style="color: #008000;">&gt;</span> <span style="color: #FF0000;">0</span><span style="color: #008000;">&#41;</span> <span style="color: #008000;">&#123;</span>
                builder<span style="color: #008000;">.</span><span style="color: #0000FF;">Append</span> <span style="color: #008000;">&#40;</span>HttpUtility<span style="color: #008000;">.</span><span style="color: #0000FF;">UrlEncode</span><span style="color: #008000;">&#40;</span>v<span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
            <span style="color: #008000;">&#125;</span>    
            first_val <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
        <span style="color: #008000;">&#125;</span>        
        first_key <span style="color: #008000;">=</span> <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">;</span>
    <span style="color: #008000;">&#125;</span>
    <span style="color: #0600FF; font-weight: bold;">return</span> builder<span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p>Notice that in the Mono HttpCookie, the names and values are URL encoded in order to escape the &#8220;=&#8221; and &#8220;&amp;&#8221; characters.  This means that we can use &#8220;=&#8221; and &#8220;&amp;&#8221; characters in the name and value of the Values property and they will be serialized unambiguously.  To make our JavaScript work with the Mono HttpCookie class improvements, the name and value strings must be URL encoded using <code>encodeURIComponent</code>:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #006600; font-style: italic;">// Warning: Does not work well with the Microsoft .NET runtime.</span>
<span style="color: #000066; font-weight: bold;">var</span> setMonoMultiValuedCookie <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>name<span style="color: #339933;">,</span> values<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">var</span> valuePairs <span style="color: #339933;">=</span> <span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
    <span style="color: #000066; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">var</span> n <span style="color: #000066; font-weight: bold;">in</span> values<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        valuePairs.<span style="color: #660066;">push</span><span style="color: #009900;">&#40;</span>encodeURIComponent<span style="color: #009900;">&#40;</span>n<span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;=&quot;</span> <span style="color: #339933;">+</span> encodeURIComponent<span style="color: #009900;">&#40;</span>values<span style="color: #009900;">&#91;</span>n<span style="color: #009900;">&#93;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #000066; font-weight: bold;">var</span> cookieValue <span style="color: #339933;">=</span> valuePairs.<span style="color: #660066;">join</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&amp;&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    document.<span style="color: #660066;">cookie</span> <span style="color: #339933;">=</span> name <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot;=&quot;</span> <span style="color: #339933;">+</span> cookieValue<span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>And presto, we have a Mono-compatible verison of <code>setMultiValuedCookie</code>.</p>
<h3>Conclusion</h3>
<p>In this article we examined how the Microsoft and Mono versions HttpCookie class serialize multiple values into a single cookie.  By looking at a sample cookies encoded by HttpCookie, as well as perusing the Mono HttpCookie source code, two JavaScript functions were written: <code>setMultiValuedCookie</code>, a function that serializes multi-valued cookies in manner compatible with both Microsoft and Mono .NET runtimes and <code>setMonoMultiValuedCookie</code>, a function that more robustly serializes multi-valued cookies but is only compatible with the Mono .NET runtime.</p>
]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2012/02/12/creating-asp-net-httpcookie-compatible-multi-valued-cookies-in-javascript/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Introducing CoffeeDOM, a JDOM fork for Java 5</title>
		<link>http://cdmckay.org/blog/2011/05/20/introducing-coffeedom-a-jdom-fork-for-java-5/</link>
		<comments>http://cdmckay.org/blog/2011/05/20/introducing-coffeedom-a-jdom-fork-for-java-5/#comments</comments>
		<pubDate>Fri, 20 May 2011 16:44:26 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[CoffeeDOM]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>

		<guid isPermaLink="false">http://cdmckay.org/blog/?p=1881</guid>
		<description><![CDATA[When it comes time to work with XML in Java, the first thing I usually do is go to the JDOM website to check for a Java 5 update. Unfortunately, I am always disappointed. There has not been a major JDOM release in over 6 years and, if the JDOM mailing list is to be [...]]]></description>
				<content:encoded><![CDATA[<p>When it comes time to work with XML in Java, the first thing I usually do is go to <a href="http://jdom.org">the JDOM website</a> to check for a Java 5 update.  Unfortunately, I am always disappointed.  There has not been a major JDOM release in over 6 years and, <a href="http://www.jdom.org/pipermail/jdom-interest/2005-August/014982.html">if the JDOM mailing list is to be believed</a>, no Java 5 version is planned.  As a result, I have decided to take my own initiative and make <a href="http://coffeedom.googlecode.com">CoffeeDOM</a>, a JDOM fork with Java 5 support.</p>
<p>CoffeeDOM is intended as a natural evolution for JDOM developers.  As such, there have been minimal changes to the API.  CoffeeDOM adds support for Java 5 features like generics, enums, and covariant method return types, and reduces the amount of boilerplate required by making previously checked exceptions (like JDOMException) unchecked.  In this article, I will briefly go over these changes.</p>
<p><em>(If you don&#8217;t want to bother with the article, you can skip right to the <a href="http://coffeedom.googlecode.com/">Google Code page</a> or <a href="http://cdmckay.org/coffeedom/apidocs/">browse the Javadoc API documentation</a>.)</em></p>
<p><span id="more-1881"></span></p>
<h3>Generic collections</h3>
<p>JDOM makes heavy use of the <a href="http://download.oracle.com/javase/tutorial/collections/index.html">Java Collections Framework</a>, principally Lists and Iterators.  Thus, in JDOM, to get a list of all child elements of an element, we do the following:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">Element</span> element <span style="color: #339933;">=</span> ...<span style="color: #339933;">;</span>
List<span style="color: #339933;">&lt;?&gt;</span> children <span style="color: #339933;">=</span> element.<span style="color: #006633;">getChildren</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Object</span> childObject <span style="color: #339933;">:</span> children<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// Cast :(</span>
    <span style="color: #003399;">Element</span> child <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Element</span><span style="color: #009900;">&#41;</span> childObject<span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #666666; font-style: italic;">// ...</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>See that pesky cast?  How inconvenient.  Another approach is to cast the List:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">Element</span> element <span style="color: #339933;">=</span> ...<span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// Cast :(</span>
List<span style="color: #339933;">&lt;</span>Element<span style="color: #339933;">&gt;</span> children <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>List<span style="color: #339933;">&lt;</span>Element<span style="color: #339933;">&gt;</span><span style="color: #009900;">&#41;</span> element.<span style="color: #006633;">getChildren</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Element</span> child <span style="color: #339933;">:</span> children<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// ...</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>&#8230; but then we would need to either ignore a warning (not recommended) or add a <code>@SuppressWarnings("unchecked")</code> annotation.  Neither is a desirable solution.</p>
<p>Fortuantely, CoffeeDOM comes to the rescue.  In CoffeeDOM, all JCF classes like List, Collection, Iterable, etc. have appropriate generic parameters:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">Element</span> element <span style="color: #339933;">=</span> ...<span style="color: #339933;">;</span>
List<span style="color: #339933;">&lt;</span>Element<span style="color: #339933;">&gt;</span> children <span style="color: #339933;">=</span> element.<span style="color: #006633;">getChildren</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Element</span> child <span style="color: #339933;">:</span> children<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// ...</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Enjoy the comfort of compile time type-checking.</p>
<h3>Covariant method return types</h3>
<p>In JDOM, in order to copy nodes, the built-in <code>Object#clone()</code> method is used:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">Element</span> element <span style="color: #339933;">=</span> ...<span style="color: #339933;">;</span>
<span style="color: #003399;">Element</span> clonedElement <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Element</span><span style="color: #009900;">&#41;</span> element.<span style="color: #006633;">clone</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Oh look, another silly cast.  Fortunately, thanks to covariant method return types, CoffeeDOM eliminates this unneeded cast:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">Element</span> element <span style="color: #339933;">=</span> ...<span style="color: #339933;">;</span>
<span style="color: #003399;">Element</span> clonedElement <span style="color: #339933;">=</span> element.<span style="color: #006633;">clone</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>CoffeeDOM also removes the need for casting when using <code>detach</code>, <code>setParent</code> and <code>getParent</code> (in most cases).</p>
<h3>Enums instead of int constants</h3>
<p>This one is pretty straightforward.  In JDOM, when we make a new Attribute, we can set the attribute type using an int constant like <code>Attribute.CDATA_TYPE</code>, <code>Attribute.ENTITIES_TYPE</code>, etc.  Here&#8217;s a (contrived) example:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">Element</span> element <span style="color: #339933;">=</span> ...<span style="color: #339933;">;</span>
<span style="color: #003399;">Attribute</span> attribute <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Attribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;name&quot;</span>, <span style="color: #0000ff;">&quot;value&quot;</span>, 
        <span style="color: #003399;">Attribute</span>.<span style="color: #006633;">UNDECLARED</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
element.<span style="color: #006633;">setAttribute</span><span style="color: #009900;">&#40;</span>attribute<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #666666; font-style: italic;">// ...or just element.setAttribute(&quot;name&quot;, &quot;value&quot;)</span></pre></td></tr></table></div>

<p>Unfortunately, modern IDEs have issues with int constants (they can&#8217;t tell which ones go with a particular method).  Moreover, operations like iterating through the enum or converting from a string to a constant, are more difficult and messy.</p>
<p>CoffeeDOM remedies this by using Java 5&#8242;s enum structure.  The above code turns into something rather similar:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">Element</span> element <span style="color: #339933;">=</span> ...<span style="color: #339933;">;</span>
<span style="color: #003399;">Attribute</span> attribute <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> <span style="color: #003399;">Attribute</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;name&quot;</span>, <span style="color: #0000ff;">&quot;value&quot;</span>, 
        <span style="color: #003399;">Attribute</span>.<span style="color: #006633;">Type</span>.<span style="color: #006633;">UNDECLARED</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
element.<span style="color: #006633;">setAttribute</span><span style="color: #009900;">&#40;</span>attribute<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>&#8230;but with the added plus that we can do things like:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Cycle through the constants.</span>
<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">Attribute</span>.<span style="color: #006633;">Type</span> type <span style="color: #339933;">:</span> <span style="color: #003399;">Attribute</span>.<span style="color: #006633;">Type</span>.<span style="color: #006633;">values</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    ...
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Get a constant from a string.</span>
<span style="color: #003399;">Attribute</span>.<span style="color: #006633;">Type</span> type <span style="color: #339933;">=</span> <span style="color: #003399;">Attribute</span>.<span style="color: #006633;">Type</span>.<span style="color: #006633;">valueOf</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;UNDECLARED&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h3>Improved for-each compatibility</h3>
<p>In Java 5, Sun juiced up the for loop by enabling it to cycle through Iterable instances.  Unfortunately, since JDOM predated this enhancement, some methods return for loop unfriendly Iterator instances.  This results in a lot of unneeded boilerplate:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">Element</span> element <span style="color: #339933;">=</span> ...<span style="color: #339933;">;</span>
Iterator<span style="color: #339933;">&lt;?&gt;</span> it <span style="color: #339933;">=</span> element.<span style="color: #006633;">getDescendants</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">while</span> <span style="color: #009900;">&#40;</span>it.<span style="color: #006633;">hasNext</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    Content descendant <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>Content<span style="color: #009900;">&#41;</span> it.<span style="color: #006633;">next</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #666666; font-style: italic;">// ...</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>CoffeeDOM corrects this by making <code>getDescendants</code> return an Iterable instance instead, allowing it to be used with the for loop:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #003399;">Element</span> element <span style="color: #339933;">=</span> ...<span style="color: #339933;">;</span>
<span style="color: #000000; font-weight: bold;">for</span> <span style="color: #009900;">&#40;</span>Content descendant <span style="color: #339933;">:</span> element.<span style="color: #006633;">getDescendants</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #666666; font-style: italic;">// ...</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>This change only affects classes implementing the JDOM Parent interface.</p>
<h3>Unchecked exceptions</h3>
<p>Constantly having to write exception checking code in Java can obfuscate code, especially if you&#8217;re just only re-throwing or logging checked exceptions to make the compiler happy.  Consider this example in JDOM:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
    SAXBuilder builder <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SAXBuilder<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">Document</span> document <span style="color: #339933;">=</span> builder.<span style="color: #006633;">build</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;path/to/file.xml&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    XMLOutputter outputter <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> XMLOutputter<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    outputter.<span style="color: #006633;">output</span><span style="color: #009900;">&#40;</span>doc, <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">IOException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span>JDOMException e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>To address this issue, CoffeeDOM changes CoffeeDOMException (the CoffeeDOM equivalent to JDOMException) to an unchecked exception.  This reduces some of the exception noise:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// Ahhh... fewer checked exceptions.</span>
<span style="color: #000000; font-weight: bold;">try</span> <span style="color: #009900;">&#123;</span>
    SAXBuilder builder <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SAXBuilder<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #003399;">Document</span> document <span style="color: #339933;">=</span> builder.<span style="color: #006633;">build</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;path/to/file.xml&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    XMLOutputter outputter <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> XMLOutputter<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    outputter.<span style="color: #006633;">output</span><span style="color: #009900;">&#40;</span>doc, <span style="color: #003399;">System</span>.<span style="color: #006633;">out</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span>  <span style="color: #000000; font-weight: bold;">catch</span> <span style="color: #009900;">&#40;</span><span style="color: #003399;">IOException</span> e<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    e.<span style="color: #006633;">printStackTrace</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<h3>Now with less noise</h3>
<p>CoffeeDOM was designed to be an evolutionary step forward for JDOM.  The main design goal of CoffeeDOM was to build on the excellent JDOM API by using modern Java features like generics, enums and covariance method return types.  The result is better compile-time checking, fewer casts and less annoying exception handling.</p>
<h3>Where do I get it? When can I use it?</h3>
<p>CoffeeDOM is available now via Google Code as <a href="http://code.google.com/p/coffeedom/downloads/list">a downloadable JAR file</a> or <a href="http://code.google.com/p/coffeedom/source/browse/">a Mercurial repository</a>.  It is also available from Maven Central using the following dependency markup:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="xml" style="font-family:monospace;"><span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>org.cdmckay.coffeedom<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/groupId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>coffeedom<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/artifactId<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
  <span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>1.0.0<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/version<span style="color: #000000; font-weight: bold;">&gt;</span></span></span>
<span style="color: #009900;"><span style="color: #000000; font-weight: bold;">&lt;/dependency<span style="color: #000000; font-weight: bold;">&gt;</span></span></span></pre></td></tr></table></div>

<h3>Learn more</h3>
<p>As CoffeeDOM is brand new, there isn&#8217;t a whole lot of documentation available beyond <a href="http://cdmckay.org/coffeedom/apidocs/">the Javadoc reference</a> and this article.  However, since CoffeeDOM shares a similar API to JDOM, any JDOM tutorial can make a good starting point.  See <a href="http://www.jdom.org/downloads/docs.html">the JDOM documentation page</a> for links to several tutorials that can get you started.</p>
<p>If code samples are your thing, the CoffeeDOM Mercurial repository has a <a href="http://code.google.com/p/coffeedom/source/browse/#hg%2FCoffeeDOM-samples">CoffeeDOM-samples</a> project that contains all the JDOM sample projects updated for CoffeeDOM.</p>
]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2011/05/20/introducing-coffeedom-a-jdom-fork-for-java-5/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Things I miss from Java while programming in C#</title>
		<link>http://cdmckay.org/blog/2011/05/02/things-i-miss-from-java-while-programming-in-c/</link>
		<comments>http://cdmckay.org/blog/2011/05/02/things-i-miss-from-java-while-programming-in-c/#comments</comments>
		<pubDate>Mon, 02 May 2011 22:19:17 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[.NET]]></category>
		<category><![CDATA[Java]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Programming Languages]]></category>

		<guid isPermaLink="false">http://cdmckay.org/blog/?p=1841</guid>
		<description><![CDATA[One of the perks of being a freelance programmer is that I get to program in a lot of different languages, either because the client has dictated a certain language, has left the choice up to me, or limited me by what is supported by a host (Hi PHP!). As fate would have it, I [...]]]></description>
				<content:encoded><![CDATA[<p>One of the perks of being a freelance programmer is that I get to program in a lot of different languages, either because the client has dictated a certain language, has left the choice up to me, or limited me by what is supported by a host (Hi PHP!).</p>
<p>As fate would have it, I have had the good fortune to have extensive experience with both C# and Java.  While many articles will list things a programmer misses from C# while coding in Java (properties, LINQ, reified generics, type inference, named and optional parameters, closures, continuations), this post intends to look at things a Java programmer might miss while coding in C#.</p>
<p><span id="more-1841"></span></p>
<h3>1. Method return type covariance</h3>
<p>Covariant method return types are one of the lesser known Java features (see <a href="http://java.sun.com/docs/books/jls/third_edition/html/classes.html#8.4.5">Section 8.4.5 in the JLS</a>).  Basically, Java allows you to narrow the return type of a method when overriding it in a sub-class.  For example:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Foo <span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SubFoo <span style="color: #000000; font-weight: bold;">extends</span> Foo <span style="color: #009900;">&#123;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> Bar <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">final</span> Foo foo <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Foo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> Foo getFoo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> foo<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> SubBar <span style="color: #000000; font-weight: bold;">extends</span> Bar <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">final</span> SubFoo subFoo <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> SubFoo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> SubFoo getFoo<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> subFoo<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Although situations where this is needed are seldom, it is a nice feature to have when you need it.  An good example of its usage is in the <a href="http://wicket.apache.org">Apache Wicket framework</a>.  All pages in Wicket typically derive from the <a href="http://wicket.apache.org/apidocs/1.4/org/apache/wicket/markup/html/WebPage.html">WebPage</a> class.  Thus, when you want to access information from a custom Wicket session you need to do something like this:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> UserInfoPage <span style="color: #000000; font-weight: bold;">extends</span> WebPage <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> UserInfoPage<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        FooSession session <span style="color: #339933;">=</span> <span style="color: #009900;">&#40;</span>FooSession<span style="color: #009900;">&#41;</span> getSession<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">String</span> userName <span style="color: #339933;">=</span> session.<span style="color: #006633;">getUserName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        ...
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>However, if you add a class above <code>UserInfoPage</code> called <code>FooPage</code>, you can use a covariant return type to eliminate the unsightly cast:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> FooPage <span style="color: #000000; font-weight: bold;">extends</span> WebPage <span style="color: #009900;">&#123;</span>
    @Override
    <span style="color: #000000; font-weight: bold;">public</span> FooSession getSession<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">return</span> <span style="color: #009900;">&#40;</span>FooSession<span style="color: #009900;">&#41;</span> <span style="color: #000000; font-weight: bold;">super</span>.<span style="color: #006633;">getSession</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
<span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">class</span> UserInfoPage <span style="color: #000000; font-weight: bold;">extends</span> FooPage <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> UserInfoPage<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        FooSession session <span style="color: #339933;">=</span> getSession<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #003399;">String</span> userName <span style="color: #339933;">=</span> session.<span style="color: #006633;">getUserName</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        ...
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>To me this is preferable to adding another method called <code>getFooSession()</code>.  Having two methods can be confusing to a future maintainer (do I want <code>getFooSession()</code> or <code>getSession()</code>? What&#8217;s the difference?) and needlessly clutters your IDE&#8217;s code completion.</p>
<p>There is no equivalent in C#, short of adding a method.  Will C# ever support such a feature?   It&#8217;s hard to say, but probably not anytime soon.  There has been <a href="http://connect.microsoft.com/VisualStudio/feedback/details/90909/need-covariant-return-types-in-c-all-net-langage">a request for covariant method return types since 2004</a>.  Microsoft response then was:</p>
<blockquote><p>We hear this request a lot. We&#8217;ll consider it for the next release.</p></blockquote>
<p>After seven years and three major C# releases, it looks like they&#8217;re still considering it.</p>
<h3>2. Class-like enums</h3>
<p>This one is not a big surprise, as it is typically mentioned as one of the few things that Java has done better that C#.  In C#, enums are basically glorified integral types.  As such, they can&#8217;t contain any extra data without using attributes.  While attributes are nice, accessing them requires quite a bit of boilerplate.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">enum</span> MotionType <span style="color: #008000;">&#123;</span>
    Stationary,
    Walking,
    Running,
    <span style="color: #008000;">&#91;</span>Description<span style="color: #008000;">&#40;</span><span style="color: #666666;">&quot;Time Travelling&quot;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#93;</span>,
    TimeTravelling
<span style="color: #008000;">&#125;</span>
&nbsp;
<span style="color: #008000;">...</span>
&nbsp;
<span style="color: #0000FF;">MotionType</span> type <span style="color: #008000;">=</span> MotionType<span style="color: #008000;">.</span><span style="color: #0000FF;">TimeTravelling</span><span style="color: #008000;">;</span>
<span style="color: #0600FF; font-weight: bold;">var</span> description <span style="color: #008000;">=</span> type
    <span style="color: #008000;">.</span><span style="color: #0000FF;">GetType</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">.</span><span style="color: #0000FF;">GetField</span><span style="color: #008000;">&#40;</span>type<span style="color: #008000;">.</span><span style="color: #0000FF;">ToString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">.</span><span style="color: #0000FF;">GetCustomAttributes</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">typeof</span><span style="color: #008000;">&#40;</span>DescriptionAttribute<span style="color: #008000;">&#41;</span>, <span style="color: #0600FF; font-weight: bold;">false</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">.</span><span style="color: #0000FF;">Cast</span><span style="color: #008000;">&lt;</span>DescriptionAttribute<span style="color: #008000;">&gt;</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">.</span><span style="color: #0000FF;">First</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span>
    <span style="color: #008000;">.</span><span style="color: #0000FF;">Description</span><span style="color: #008000;">;</span>
Console<span style="color: #008000;">.</span><span style="color: #0000FF;">WriteLine</span><span style="color: #008000;">&#40;</span>description<span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>A further restriction of C# enums if that they can&#8217;t be used as a generic constraint.  For example, the following code would not compile:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">public</span> <span style="color: #6666cc; font-weight: bold;">class</span> Monster<span style="color: #008000;">&lt;</span>TMotionType<span style="color: #008000;">&gt;</span> <span style="color: #0600FF; font-weight: bold;">where</span> TMotionType <span style="color: #008000;">:</span> <span style="color: #6666cc; font-weight: bold;">enum</span> <span style="color: #008000;">&#123;</span>
<span style="color: #008000;">&#125;</span></pre></td></tr></table></div>

<p><a href="https://msmvps.com/blogs/jon_skeet/archive/2009/09/10/generic-constraints-for-enums-and-delegates.aspx">According to C# MVP Jon Skeet</a>, this is not a restriction of the CLR, but is actually a restriction of the Microsoft C# compiler.  The best you can do to work around this is to use the broader constraint <code>where : struct</code>, or the <a href="http://code.google.com/p/unconstrained-melody/">Unconstrained Melody library</a>.</p>
<p>Java enums, on the other hand, are specialized classes.  Like C# enums, Java enums can be used with <code>switch</code> statements and be converted to integral types.  However, Java enums go far beyond these simple uses.  Java enums can have instance fields, instance methods and even per-constant methods.    Java generics can also be constrained to take only enums.  </p>
<p>Here&#8217;s how the <code>MotionType</code> enum could be written in Java (notice how easy it is to get the description in this case):</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="java" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">enum</span> MotionType <span style="color: #009900;">&#123;</span>
    STATIONARY<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Stationary&quot;</span><span style="color: #009900;">&#41;</span>,
    WALKING<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Walking&quot;</span><span style="color: #009900;">&#41;</span>,
    RUNNING<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Running&quot;</span><span style="color: #009900;">&#41;</span>,
    TIME_TRAVELLING<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Time Travelling&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">final</span> <span style="color: #003399;">String</span> description<span style="color: #339933;">;</span>
    MotionType<span style="color: #009900;">&#40;</span><span style="color: #003399;">String</span> description<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000000; font-weight: bold;">this</span>.<span style="color: #006633;">description</span> <span style="color: #339933;">=</span> description<span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span>
&nbsp;
...
&nbsp;
<span style="color: #006633;">MotionType</span> type <span style="color: #339933;">=</span> MotionType.<span style="color: #006633;">TIME_TRAVELLING</span><span style="color: #339933;">;</span>
<span style="color: #003399;">System</span>.<span style="color: #006633;">out</span>.<span style="color: #006633;">println</span><span style="color: #009900;">&#40;</span>type.<span style="color: #006633;">description</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>This is just scratching the surface of Java enum capabilities.  I highly recommend reading Chapter 6 of Effective Java or <a href="http://download.oracle.com/javase/1.5.0/docs/guide/language/enums.html">the Oracle enum documentation</a>.</p>
<h3>3. Anonymous inner classes</h3>
<p>This is another it&#8217;s-useful-when-you-need-it feature of Java that C# mostly addresses with events and delegates.  The most common situation for anonymous inner classes in Java is for listeners:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="java" style="font-family:monospace;">Widget widget <span style="color: #339933;">=</span> <span style="color: #000000; font-weight: bold;">new</span> Widget<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000066; font-weight: bold;">void</span> onInteraction<span style="color: #009900;">&#40;</span>WidgetState state<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        ...
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>In C#, this is handled using events and delegates:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;">Widget widget <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Widget<span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span>
widget<span style="color: #008000;">.</span><span style="color: #0000FF;">Interaction</span> <span style="color: #008000;">+=</span> state <span style="color: #008000;">=&gt;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #008000;">...</span>
<span style="color: #008000;">&#125;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>The C# approach is superior in most cases.  There&#8217;s far less clutter and it&#8217;s a lot easier to add listener support to a class in C#.  Furthermore, since it&#8217;s a language feature, you can count on it being more or less the same everywhere.  However, it&#8217;s not without drawbacks.  You&#8217;ll notice in the previous Java example, we can guarantee that there is only one listener.  Furthermore, if we declare <code>onInteraction</code> as abstract, we can also ensure that all concrete implementations of <code>Widget</code> have an <code>onInteraction</code> handler.  The event/delegate approach as written cannot make the same guarantees.</p>
<p>Since C# does not support anonymous inner classes, the best we can do to emulate them is something like this:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;">Widget widget <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Widget<span style="color: #008000;">&#40;</span>state <span style="color: #008000;">=&gt;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #008000;">...</span>
<span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>&#8230;or, if we wanted to make it a little more clear, we could use named parameters:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;">Widget widget <span style="color: #008000;">=</span> <span style="color: #008000;">new</span> Widget<span style="color: #008000;">&#40;</span>onInteraction<span style="color: #008000;">:</span> state <span style="color: #008000;">=&gt;</span> <span style="color: #008000;">&#123;</span>
    <span style="color: #008000;">...</span>
<span style="color: #008000;">&#125;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<h3>Locally scoped final/readonly variables</h3>
<p><a href="http://www.cc2e.com/">In the book Code Complete 2</a>, Steve McConnell recommends that you use final keyword whenever possible.  The justification for this being that less mutable code is easier to keep track of mentally and less likely to be accidentally modified.  Although the practice of making everything final can be weird at first, you eventually become so used to using final that you start to feel a little bit exposed whenever a variable isn&#8217;t marked final.</p>
<p>C# has a nearly equivalent keyword to <code>final</code>: <code>readonly</code>.  However, for some reason, readonly is only usable on the field level.  In the local scope, there is no direct equivalent to final in C#.  The best you can do is use <code>const</code>, but this only works with compile-time constants, so</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="csharp" style="font-family:monospace;"><span style="color: #0600FF; font-weight: bold;">const</span> <span style="color: #6666cc; font-weight: bold;">string</span> foo <span style="color: #008000;">=</span> Foo<span style="color: #008000;">.</span><span style="color: #0000FF;">getString</span><span style="color: #008000;">&#40;</span><span style="color: #008000;">&#41;</span><span style="color: #008000;">;</span></pre></td></tr></table></div>

<p>&#8230;won&#8217;t work.</p>
<h3>Conclusion</h3>
<p>In this article we looked at four features of Java that lack direct C# equivalents: covariant method return types, class-like enums, anonymous inner classes and locally-scoped final/readonly keywords.  C# is often thought of as a &#8220;better Java&#8221;.  However, as this article has shown, this is not true.  Java may not have as many bells and whistles as its newer cousin, but it still offers some compelling features that may never find their way into C#.</p>
]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2011/05/02/things-i-miss-from-java-while-programming-in-c/feed/</wfw:commentRss>
		<slash:comments>42</slash:comments>
		</item>
		<item>
		<title>Eager boolean operators in JavaScript</title>
		<link>http://cdmckay.org/blog/2010/09/09/eager-boolean-operators-in-javascript/</link>
		<comments>http://cdmckay.org/blog/2010/09/09/eager-boolean-operators-in-javascript/#comments</comments>
		<pubDate>Thu, 09 Sep 2010 06:22:11 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://cdmckay.org/blog/?p=1741</guid>
		<description><![CDATA[In most programming languages like Java, JavaScript, C and C#, the boolean operators &#038;&#038; and &#124;&#124; perform short-circuited evaluation. Essentially, this means that a program, when evaluating boolean operators, will only evaluate as many arguments as is necessary to determine the value of a boolean expression. Sometimes, however, this is not the behaviour we want. [...]]]></description>
				<content:encoded><![CDATA[<p>In most programming languages like Java, JavaScript, C and C#, the boolean operators <code>&#038;&#038;</code> and <code>||</code> perform <a href="http://en.wikipedia.org/wiki/Short-circuit_evaluation">short-circuited evaluation</a>. Essentially, this means that a program, when evaluating boolean operators, will only evaluate as many arguments as is necessary to determine the value of a boolean expression.  Sometimes, however, this is not the behaviour we want.</p>
<p>In this article we will look at how to implement non-short-circuited or &#8220;eager&#8221; versions of the JavaScript boolean operators <code>&#038;&#038;</code> and <code>||</code>.</p>
<p><span id="more-1741"></span></p>
<p>As mentioned earlier, most programming languages have short-circuited boolean operators. Short-circuited operators are possible due to certain properties of conjunctions and disjunctions in Boolean logic.  In Boolean logic, for a conjunction <code>x1 &#038;&#038; x2</code>, if the first argument evaluates to false, the whole expression will be false, regardless of what the other argument evaluates to.  Similarly, for a disjunction <code>x1 || x2</code>, if the first argument evaluates to true, the whole expression will be true, regardless of what the other argument evaluates to.</p>
<p>For a more concrete example, consider this snippet of JavaScript:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> str <span style="color: #339933;">=</span> <span style="color: #003366; font-weight: bold;">null</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>str <span style="color: #339933;">!==</span> <span style="color: #003366; font-weight: bold;">null</span> <span style="color: #339933;">&amp;&amp;</span> str.<span style="color: #660066;">length</span> <span style="color: #339933;">&gt;</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    alert<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;String is not null or empty&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
    alert<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;String is null or empty&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>If executed by a browser, this snippet will alert <code>String is null or empty</code>.  It does not throw a <code>TypeError</code> when it hits <code>str.length</code> because <code>str.length</code> is never evaluated.  The JavaScript interpreter skips it once it sees <code>str !== null</code> evaluates to false.  This is an advantage of short-circuiting.</p>
<h3>What if we don&#8217;t want to short the circuit?</h3>
<p>Although short-circuiting is almost always what we want, there are rare instances where we may want to have non-short-circuited or <strong>eager</strong> evaluation.  Consider this example using jQuery:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #000066; font-weight: bold;">var</span> validate <span style="color: #339933;">=</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>element<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    <span style="color: #000066; font-weight: bold;">if</span> <span style="color: #009900;">&#40;</span>element.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">===</span> <span style="color: #3366CC;">&quot;&quot;</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        element.<span style="color: #660066;">addClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;invalid&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span> <span style="color: #000066; font-weight: bold;">else</span> <span style="color: #009900;">&#123;</span>
        element.<span style="color: #660066;">removeClass</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;highlight&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">true</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #000066; font-weight: bold;">var</span> form <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#comments-form&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> name <span style="color: #339933;">=</span> form.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#name&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> email <span style="color: #339933;">=</span> form.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#email&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #000066; font-weight: bold;">var</span> comments <span style="color: #339933;">=</span> form.<span style="color: #660066;">find</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#comments&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
form.<span style="color: #660066;">submit</span><span style="color: #009900;">&#40;</span><span style="color: #000066; 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> validate<span style="color: #009900;">&#40;</span>name<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> validate<span style="color: #009900;">&#40;</span>email<span style="color: #009900;">&#41;</span> <span style="color: #339933;">&amp;&amp;</span> validate<span style="color: #009900;">&#40;</span>comments<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>In this example, we are validating a comments form for a website.  In order to do that, we use a <code>validate</code> function to verify that an HTML input is not empty.  If it is empty, then we add a CSS class to highlight it and then return false to indicate validation failed.  Conversely, if it&#8217;s not empty, we remove the highlight CSS class and then return true to indicate validation succeeded.</p>
<p>Unfortunately, this code will only highlight the first field that fails validation.  Why?  Because the <code>&#038;&#038;</code> short-circuits the first time it evaluates an argument to false and thus the rest of the validations will not be executed.</p>
<p>So how do we force them run?  Well, if we were using a language like Java or C#, we would simply use the <code>&#038;</code> and <code>|</code> operators, which are eager versions of the short-circuited <code>&#038;&#038;</code> and <code>||</code> operators.  Unfortunately, although JavaScript does provide the <code>&#038;</code> and <code>|</code> operators, they return numeric (0 or 1) instead of boolean (true or false) results.</p>
<h3>Eager boolean operators in JavaScript</h3>
<p><em>(Aside: In the following section I use the arithmetic operators <code>*</code> and <code>+</code> to emulate eager boolean operators.  You can just as easily use the bitwise operators <code>&#038;</code> and <code>|</code> in their place.  The only reason I used the arithmetic operators was because I thought they would make for a more interesting article).</em></p>
<p>So we&#8217;ve established that JavaScript has no eager boolean operators.  JavaScript does, however, offer us two arithmetic operators that can almost accomplish our goal: <code>*</code> and <code>+</code>.  These operators, in conjunction with JavaScript&#8217;s type coercion semantics, produce the following truth tables when fed all combinations of true and false (<code>&#038;&#038;</code> and <code>||</code> are shown for comparison):</p>
<table>
<tr>
<th>a</th>
<th>b</th>
<th>a &#038;&#038; b</th>
<th>a * b</th>
<th>a || b</th>
<th>a + b</th>
</tr>
<tr>
<td>false</td>
<td>false</td>
<td>false</td>
<td>0</td>
<td>false</td>
<td>0</td>
</tr>
<tr>
<td>false</td>
<td>true</td>
<td>false</td>
<td>0</td>
<td>true</td>
<td>1</td>
</tr>
<tr>
<td>true</td>
<td>false</td>
<td>false</td>
<td>0</td>
<td>true</td>
<td>1</td>
</tr>
<tr>
<td>true</td>
<td>true</td>
<td>true</td>
<td>1</td>
<td>true</td>
<td>2</td>
</tr>
</table>
<p>In the table you&#8217;ll notice that <code>*</code> and <code>+</code> operators yield almost identical truth tables to the <code>&#038;&#038;</code> and <code>||</code> operators, with the exception being that the short-circuited operators produce boolean values (true and false) while the arithmetic operators produce integer values (0, 1 or 2).  Moreover, unlike the boolean operators, the arithmetic operators are eager.</p>
<p>That means that, in order to use the arithmetic operators to implement eager boolean operators that produce the exact same results as the built-in boolean operators, we need to map 0 to false and map any non-0 values to true.  Fortunately, this is exactly what JavaScript&#8217;s type coercion does when forced to coerce a number into a boolean, which can be accomplished by prefixing the number with two <code>!</code> operators.</p>
<table>
<tr>
<th>a</th>
<th>b</th>
<th>a &#038;&#038; b</th>
<th>!!(a * b)</th>
<th>a || b</th>
<th>!!(a + b)</th>
</tr>
<tr>
<td>false</td>
<td>false</td>
<td>false</td>
<td>false</td>
<td>false</td>
<td>false</td>
</tr>
<tr>
<td>false</td>
<td>true</td>
<td>false</td>
<td>false</td>
<td>true</td>
<td>true</td>
</tr>
<tr>
<td>true</td>
<td>false</td>
<td>false</td>
<td>false</td>
<td>true</td>
<td>true</td>
</tr>
<tr>
<td>true</td>
<td>true</td>
<td>true</td>
<td>true</td>
<td>true</td>
<td>true</td>
</tr>
</table>
<p>And there we have it.  We&#8217;ve implemented eager boolean operators using JavaScript&#8217;s type coercion, the arithmetic operators <code>*</code> and <code>+</code> and the logical not operator <code>!</code>.</p>
<h3>Conclusion</h3>
<p>Boolean operators in most programming languages are short-circuited: they evaluate their terms left to right and stop (i.e. short-circuit) once they can definitively determine the truth value of expression. This behaviour is usually what we want.  However, in some rare cases, we want to evaluate all terms in a boolean expression.  Such an operator is called an eager boolean operator.</p>
<p>Although JavaScript does not provide eager boolean operators, they can be emulated by using a combination of JavaScript&#8217;s type coercion semantics and arithmetic and logical operators.</p>
]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2010/09/09/eager-boolean-operators-in-javascript/feed/</wfw:commentRss>
		<slash:comments>12</slash:comments>
		</item>
		<item>
		<title>How to do an AJAX search with jQTouch, Part 2</title>
		<link>http://cdmckay.org/blog/2010/07/31/how-to-do-an-ajax-search-with-jqtouch-part-2/</link>
		<comments>http://cdmckay.org/blog/2010/07/31/how-to-do-an-ajax-search-with-jqtouch-part-2/#comments</comments>
		<pubDate>Sat, 31 Jul 2010 15:53:25 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[PHP]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://cdmckay.org/blog/?p=1667</guid>
		<description><![CDATA[In the last installment of How to do an AJAX search with jQTouch we looked at how to setup a jQTouch interface with the goal of performing an AJAX search. In this article, we will write the necessary JavaScript to perform that AJAX search, as well as a PHP script to respond to those calls. [...]]]></description>
				<content:encoded><![CDATA[<p>In the last installment of <a href="http://cdmckay.org/blog/2010/04/22/how-to-do-an-ajax-search-with-jqtouch-part-1/">How to do an AJAX search with jQTouch</a> we looked at how to setup a jQTouch interface with the goal of performing an AJAX search. In this article, we will write the necessary JavaScript to perform that AJAX search, as well as a PHP script to respond to those calls.</p>
<p>This article assumes you have read <a href="http://cdmckay.org/blog/2010/04/22/how-to-do-an-ajax-search-with-jqtouch-part-1/">the first part of this series</a>.</p>
<p><span id="more-1667"></span></p>
<p>Before we start, we should take a step back and consider what we&#8217;re about to do.  Our Searcher app has three parts:</p>
<ul>
<li>the client-side jQTouch UI,</li>
<li>the client-side jQuery AJAX calls,</li>
<li>the server-side PHP script (that could easily be backed by a database)</li>
</ul>
<p>At this point we have finished the jQTouch UI.  However, instead of immediately moving on to writing the jQuery AJAX calls, let&#8217;s start with the PHP script instead.  This will help us understand our AJAX calls when it comes time to write them.</p>
<h3>The PHP Script</h3>
<p>Since our PHP script will be performing a search, let&#8217;s create a file called <code>search.php</code> and add a single line of boilerplate:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">&lt;?php</span></pre></td></tr></table></div>

<p>There&#8217;s no need for an end tag.  In fact, in files containing only PHP, <a href="http://framework.zend.com/manual/en/coding-standard.php-file-formatting.html">it is recommended that you do not include an end tag</a>:</p>
<blockquote><p>For files that contain only PHP code, the closing tag (&#8220;?&gt;&#8221;) is never permitted. It is not required by PHP, and omitting it prevents the accidental injection of trailing white space into the response.</p></blockquote>
<p>Our Searcher app needs to search for something, so let&#8217;s pretend it&#8217;s an interface for a music player and we&#8217;re searching for songs. In order to express that idea into PHP code, we need the following elements:</p>
<ul>
<li>a Song class to hold the artist name and the title,</li>
<li>an array holding a bunch of Song objects,</li>
<li>a function for searching the Song array,</li>
<li>a way to get a search value from the AJAX request,</li>
<li>a way to set the MIME type to JSON,</li>
<li>a way to encode our Song array as JSON</li>
</ul>
<p>First, we&#8217;ll need a class to hold the songs (place this starting at line 3 in <code>search.php</code>):</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>3
4
5
6
7
8
9
10
11
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">class</span> Song <span style="color: #009900;">&#123;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$artist</span><span style="color: #339933;">;</span>
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000088;">$title</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000000; font-weight: bold;">public</span> <span style="color: #000000; font-weight: bold;">function</span> __construct<span style="color: #009900;">&#40;</span><span style="color: #000088;">$artist</span><span style="color: #339933;">,</span> <span style="color: #000088;">$title</span><span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">artist</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$artist</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$this</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$title</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>Let&#8217;s break it down for those not familiar with PHP classes:</p>
<ul>
<li>at line 3, we declare the class name</li>
<li>at lines 4-5, we declare two public fields to represent the artist&#8217;s name and the title of the song,</li>
<li>at lines 7-10, we declare the class constructor that we use to initialize the two fields of a Song object</li>
</ul>
<p>Now that we have a way to model songs in our PHP script, we need to make an array of some dummy songs that we can query using AJAX:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>13
14
15
16
17
18
19
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$songs</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #000000; font-weight: bold;">new</span> Song<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Weezer&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Say It Ain't So&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #000000; font-weight: bold;">new</span> Song<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Weezer&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Undone&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #000000; font-weight: bold;">new</span> Song<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Cake&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Meanwhile, Rick James...&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #000000; font-weight: bold;">new</span> Song<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;The Stars&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Ageless Beauty&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #000000; font-weight: bold;">new</span> Song<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Roy Orbison&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;In Dreams&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Now we have something of a faux database setup. How will we search it?  Why, with a search function, of course! Let&#8217;s write that now:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>21
22
23
24
25
26
27
28
29
30
31
32
33
34
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000000; font-weight: bold;">function</span> search<span style="color: #009900;">&#40;</span><span style="color: #000088;">$songs</span><span style="color: #339933;">,</span> <span style="color: #000088;">$value</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><span style="color: #990000;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span>
        <span style="color: #b1b100;">return</span> <span style="color: #000088;">$songs</span><span style="color: #339933;">;</span>
&nbsp;
    <span style="color: #000088;">$matches</span> <span style="color: #339933;">=</span> <span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    <span style="color: #b1b100;">foreach</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$songs</span> <span style="color: #b1b100;">as</span> <span style="color: #000088;">$s</span><span style="color: #009900;">&#41;</span>
    <span style="color: #009900;">&#123;</span>
        <span style="color: #000088;">$matchArtist</span> <span style="color: #339933;">=</span> <span style="color: #990000;">stripos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$s</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">artist</span><span style="color: #339933;">,</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!==</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #000088;">$matchTitle</span> <span style="color: #339933;">=</span> <span style="color: #990000;">stripos</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$s</span><span style="color: #339933;">-&gt;</span><span style="color: #004000;">title</span><span style="color: #339933;">,</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">!==</span> <span style="color: #009900; font-weight: bold;">false</span><span style="color: #339933;">;</span>
        <span style="color: #b1b100;">if</span> <span style="color: #009900;">&#40;</span><span style="color: #000088;">$matchArtist</span> <span style="color: #339933;">||</span> <span style="color: #000088;">$matchTitle</span><span style="color: #009900;">&#41;</span>
            <span style="color: #000088;">$matches</span><span style="color: #009900;">&#91;</span><span style="color: #009900;">&#93;</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$s</span><span style="color: #339933;">;</span>
    <span style="color: #009900;">&#125;</span>
    <span style="color: #b1b100;">return</span> <span style="color: #000088;">$matches</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span></pre></td></tr></table></div>

<p>So what does this function do? Let&#8217;s take a look line-by-line:</p>
<ul>
<li>at lines 22-23, we check to see if the search value is empty (i.e. an empty string or null). If that&#8217;s the case, then we just return all the songs</li>
<li>at line 25, we declare the <code>$matches</code> array. This will contain all the songs that match the search value <code>$value</code></li>
<li>at line 26, we use a foreach loop to iterate through all the Song objects in the <code>$songs</code> array</li>
<li>at lines 28-29, we check to see if the artist name or song title contain the search value <code>$value</code></li>
<li>at line 30-31, we add the Song object to the <code>$matches</code> array if it matches the artist name or song title</li>
<li>at line 33, we return whatever matches (if any) we have found</li>
</ul>
<p>Ok, so now we have a Song class, an array of Song objects, and a function that searches that array. What&#8217;s left in our PHP script? Well, as it stands, we have no way to interface with it. What we need to do now is have some sort of way to respond to the AJAX GET request that our Searcher app will send. To do that, we harness the awesome power of the <code>$_GET</code> <a href="http://php.net/manual/en/language.variables.superglobals.php">superglobal</a>:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>36
37
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #000088;">$value</span> <span style="color: #339933;">=</span> <span style="color: #000088;">$_GET</span><span style="color: #009900;">&#91;</span><span style="color: #0000ff;">'value'</span><span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span>
<span style="color: #000088;">$matches</span> <span style="color: #339933;">=</span> search<span style="color: #009900;">&#40;</span><span style="color: #000088;">$songs</span><span style="color: #339933;">,</span> <span style="color: #000088;">$value</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>On the first line, we extract &#8220;value&#8221; parameter from the <code>$_GET</code> superglobal and assign it to the <code>$value</code> variable.  The second line runs our search function on the <code>$songs</code> database and searches for songs that have an artist or title that contains the <code>$value</code> string.</p>
<p>We&#8217;re almost there!  At this point we&#8217;ve managed to get an array of Song objects that match the passed search value.  All we need to do now is return that PHP array of objects as a JSON-encoded array of objects.  Let&#8217;s do that now:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>39
40
</pre></td><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #990000;">header</span><span style="color: #009900;">&#40;</span><span style="color: #0000ff;">'Content-Type: application/json'</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #b1b100;">echo</span> <span style="color: #990000;">json_encode</span><span style="color: #009900;">&#40;</span><span style="color: #000088;">$matches</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The first line sets the the <a href="http://en.wikipedia.org/wiki/Internet_media_type">MIME type</a> of an HTTP response.  MIME types are a sort of meta data that is used to describe what sort of content is being returned.  In this case, we are telling jQuery (or whatever client that issued the request) that we are returning content that is JSON formatted.  The second line is a built-in PHP function that converts PHP types into a string containing their JSON counterparts.  Basically, it allows us to encode our PHP objects into JavaScript and allow us to access them in a nearly identical way:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// PHP</span>
<span style="color: #990000;">array</span><span style="color: #009900;">&#40;</span>
    <span style="color: #000000; font-weight: bold;">new</span> Song<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Artist 1&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Title 1&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">,</span>
    <span style="color: #000000; font-weight: bold;">new</span> Song<span style="color: #009900;">&#40;</span><span style="color: #0000ff;">&quot;Artist 2&quot;</span><span style="color: #339933;">,</span> <span style="color: #0000ff;">&quot;Title 2&quot;</span><span style="color: #009900;">&#41;</span>
<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Becomes... (after json_encode)</span>
<span style="color: #666666; font-style: italic;">// JavaScript</span>
<span style="color: #009900;">&#91;</span>
    <span style="color: #009900;">&#123;</span> artist<span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;Artist 1&quot;</span><span style="color: #339933;">,</span> title<span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;Title 1&quot;</span> <span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span>
    <span style="color: #009900;">&#123;</span> artist<span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;Artist 2&quot;</span><span style="color: #339933;">,</span> title<span style="color: #339933;">:</span> <span style="color: #0000ff;">&quot;Title 2&quot;</span> <span style="color: #009900;">&#125;</span>
<span style="color: #009900;">&#93;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>And we&#8217;re done the PHP script! If we want to test it, we can load execute the PHP file on our server and try different search values.  For example, entering this URL should return a JSON array with two Song objects:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="php" style="font-family:monospace;"><span style="color: #666666; font-style: italic;">// URL</span>
http<span style="color: #339933;">:</span><span style="color: #666666; font-style: italic;">//example.com/jQTouch-ajax-search/search.php?value=Weezer</span>
&nbsp;
<span style="color: #666666; font-style: italic;">// Response</span>
<span style="color: #009900;">&#91;</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;artist&quot;</span><span style="color: #339933;">:</span><span style="color: #0000ff;">&quot;Weezer&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;title&quot;</span><span style="color: #339933;">:</span><span style="color: #0000ff;">&quot;Say It Ain't So&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span><span style="color: #009900;">&#123;</span><span style="color: #0000ff;">&quot;artist&quot;</span><span style="color: #339933;">:</span><span style="color: #0000ff;">&quot;Weezer&quot;</span><span style="color: #339933;">,</span><span style="color: #0000ff;">&quot;title&quot;</span><span style="color: #339933;">:</span><span style="color: #0000ff;">&quot;Undone&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#93;</span></pre></td></tr></table></div>

<h3>The jQuery AJAX calls</h3>
<p>The finish line is now in sight.  We&#8217;ve successfully written our jQTouch UI and our PHP script.  The last thing we need to do is write some jQuery AJAX calls to execute our search and load the results into our UI.  In order to do that, we need to do the following:</p>
<ul>
<li>listen for the submission of the search form in the jQTouch UI</li>
<li>when the submission happens, get the search value from the form and set it as a parameter in an AJAX call to <code>search.php</code></li>
<li>register a callback that will populate the jQTouch search results when the AJAX call completes</li>
</ul>
<p>Let&#8217;s start with listening for the submit event on the search form.  First, we&#8217;ll need to create a new JavaScript source file <code>search.js</code> and fill it with the following boilerplate:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
    <span style="color: #006600; font-style: italic;">// Put code here.    </span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>What this boilerplate does is give our JavaScript script its own context so that can minimize leaking variables into the global JavaScript namespace.  It also gives us access to the jQuery object in the convenient <code>$</code> form, regardless of whether or not <a href="http://api.jquery.com/jQuery.noConflict/">&#8220;no conflict&#8221; mode is enabled</a>.</p>
<p>Recall in part 1 we defined an HTML search form that looked like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
</pre></td><td class="code"><pre class="html4strict" style="font-family:monospace;"><span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">form</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;search&quot;</span> <span style="color: #000066;">action</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;&quot;</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">div</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;toolbar&quot;</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">h1</span>&gt;</span>Search<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">h1</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">a</span> <span style="color: #000066;">href</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;#&quot;</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;back&quot;</span>&gt;</span>Back<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">a</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">div</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">ul</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;rounded&quot;</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span>&gt;&lt;<span style="color: #000000; font-weight: bold;">input</span> <span style="color: #000066;">type</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;text&quot;</span> <span style="color: #000066;">name</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;search-text&quot;</span> placeholder<span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;Search&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;search-text&quot;</span> <span style="color: #66cc66;">/</span>&gt;&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
    <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">ul</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;edgetoedge&quot;</span> <span style="color: #000066;">id</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;search-results&quot;</span>&gt;</span>
        <span style="color: #009900;">&lt;<span style="color: #000000; font-weight: bold;">li</span> <span style="color: #000066;">class</span><span style="color: #66cc66;">=</span><span style="color: #ff0000;">&quot;sep&quot;</span>&gt;</span>Results<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">li</span>&gt;</span>                
    <span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">ul</span>&gt;</span>
<span style="color: #009900;">&lt;<span style="color: #66cc66;">/</span><span style="color: #000000; font-weight: bold;">form</span>&gt;</span></pre></td></tr></table></div>

<p>Note that on line 1 we defined the id of the form as <code>search</code>. We&#8217;ll use that to find the form and register a handler for its submit event so that we can take action when someone tries to perform a search using the jQTouch UI.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;"><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
$<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#123;</span>
&nbsp;
    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#search&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">submit</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>event<span style="color: #339933;">,</span> info<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
        <span style="color: #000066; font-weight: bold;">var</span> text <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;input[id=search-text]&quot;</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        text.<span style="color: #660066;">blur</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">var</span> results <span style="color: #339933;">=</span> $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#search-results&quot;</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">empty</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
        results.<span style="color: #660066;">append</span><span style="color: #009900;">&#40;</span>$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&lt;li&gt;&quot;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>
            <span style="color: #3366CC;">&quot;class&quot;</span><span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;sep&quot;</span><span style="color: #339933;">,</span>
            text<span style="color: #339933;">:</span> <span style="color: #3366CC;">'Results for &quot;'</span> <span style="color: #339933;">+</span> text.<span style="color: #660066;">val</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span> <span style="color: #339933;">+</span> <span style="color: #3366CC;">'&quot;'</span>
        <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        $.<span style="color: #000066; font-weight: bold;">get</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;search.php&quot;</span><span style="color: #339933;">,</span>
            <span style="color: #009900;">&#123;</span> value<span style="color: #339933;">:</span> text.<span style="color: #660066;">val</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: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>data<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                $.<span style="color: #660066;">each</span><span style="color: #009900;">&#40;</span>data<span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>i<span style="color: #339933;">,</span> song<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
                    <span style="color: #000066; font-weight: bold;">var</span> str <span style="color: #339933;">=</span> song.<span style="color: #660066;">artist</span> 
                        <span style="color: #339933;">+</span> <span style="color: #3366CC;">&quot; - &quot;</span>
                        <span style="color: #339933;">+</span> song.<span style="color: #660066;">title</span><span style="color: #339933;">;</span>
                    $<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;&lt;li&gt;&quot;</span><span style="color: #009900;">&#41;</span>
                        .<span style="color: #660066;">text</span><span style="color: #009900;">&#40;</span>str<span style="color: #009900;">&#41;</span>
                        .<span style="color: #660066;">appendTo</span><span style="color: #009900;">&#40;</span>results<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
                <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
            <span style="color: #009900;">&#125;</span>
        <span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
        <span style="color: #000066; font-weight: bold;">return</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #339933;">;</span>       
    <span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
&nbsp;
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #009900;">&#40;</span>jQuery<span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>There&#8217;s a lot there, so let&#8217;s break it down line-by-line:</p>
<ul>
<li>starting at line 3 and ending at line 32 we have <a href="http://api.jquery.com/ready/">the jQuery ready handler</a> which is run once the DOM has finished loading</li>
<li>starting at line 5 and ending at line 30 we bind a submit event handler to the submit event for the <code>#search</code> form</li>
<li>at lines 6-7, we retrieve the <code>search-text</code> element, assign it to a variable (for later use on line 16), and then blur (i.e. deselect) the element so that the iPhone keyboard will go away</li>
<li>at lines 9-13, we clear any previous items in the list that may be hanging around from a previous search and then load in a new separator (&#8220;sep&#8221;) that contains the search value</li>
<li>at line 15-27, we perform our AJAX GET request to <code>search.php</code> with the value of the search text field as our value parameter (line 16) and a callback that adds the results to the list (lines 17-26)</li>
<li>finally, at line 29, we return false to prevent the default form behaviour (i.e. submitting the form) from being carried out</li>
</ul>
<p>And we&#8217;re done.  All we need to do now and include <code>search.php</code> in the header of <code>index.html</code> so that it&#8217;ll be run:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>12
</pre></td><td class="code"><pre class="html" style="font-family:monospace;">&lt;script src=&quot;search.js&quot; type=&quot;application/x-javascript&quot; charset=&quot;utf-8&quot;&gt;&lt;/script&gt;</pre></td></tr></table></div>

<h3>Conclusion</h3>
<p>In this, the final installment of <em>How to do an AJAX search with jQTouch</em>, we finished off the example application we started in <a href="http://cdmckay.org/blog/2010/04/22/how-to-do-an-ajax-search-with-jqtouch-part-1/">the first part of this series</a>.  </p>
<p>In part 1, we covered building a UI using jQTouch.  In part 2, we helped visualize our problem by pretending that we were writing a front-end UI to a server-side music player.  To do that, we jumped into server side programming with PHP to write a script to serve us JSON-encoded Song objects.  Once we had that setup, we moved back into JavaScript.  In order to make use of the PHP script, we wrote a jQuery handler to respond to a jQTouch form submission.  The handler, once triggered, issued an AJAX call to the PHP script to pull down the appropriate Song objects for display in our UI.</p>
<h3>What Now?</h3>
<p>With respect to the goals of this tutorial, nothing.  However, if you want to pimp up the application a bit more, some things you could do are: </p>
<ul>
<li>Add genres and albums to the PHP Song object and then make the jQTouch UI menu have two additional options: Genres and Albums</li>
<li>Try to make a Now Playing sub-menu that is activated when a Song is picked from the Search screen</li>
</ul>
<h3>Source</h3>
<p>The source code I have written in this article is public domain, and you are free to do what you will with it. Here is <a href="http://cdmckay.org/files/jqtouch-ajax-search-part2.zip">a zip of the source, along with the required jQTouch source</a>. It should work pretty much right out of the box.</p>
]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2010/07/31/how-to-do-an-ajax-search-with-jqtouch-part-2/feed/</wfw:commentRss>
		<slash:comments>24</slash:comments>
		</item>
		<item>
		<title>How to use custom jQuery animation queues</title>
		<link>http://cdmckay.org/blog/2010/06/22/how-to-use-custom-jquery-animation-queues/</link>
		<comments>http://cdmckay.org/blog/2010/06/22/how-to-use-custom-jquery-animation-queues/#comments</comments>
		<pubDate>Tue, 22 Jun 2010 05:07:53 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://cdmckay.org/blog/?p=1620</guid>
		<description><![CDATA[You may not know this, but whenever you use jQuery commands like fadeIn, slideDown, and delay, you are implicitly making use of a jQuery queue behind the scenes. That queue is named fx, and it is the default queue that all animations use unless otherwise specified. In this article, we will look at how jQuery [...]]]></description>
				<content:encoded><![CDATA[<p>You may not know this, but whenever you use jQuery commands like <code>fadeIn</code>, <code>slideDown</code>, and <code>delay</code>, you are implicitly making use of a jQuery queue behind the scenes.  That queue is named <code>fx</code>, and it is the default queue that all animations use unless otherwise specified.</p>
<p>In this article, we will look at how jQuery animation queues work, how to create and manipulate them, and how to use them in a meaningful way.</p>
<p><span id="more-1620"></span></p>
<h3>Queue fundamentals</h3>
<p>Each element has its own set of queues.  For example, if we execute this code:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#foo&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">fadeOut</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#bar&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">fadeOut</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The elements <code>#foo</code> and <code>#bar</code> will fade out more or less simultaneously. This is because they have their own <code>fx</code> queues. However, if we execute the code:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#foo&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">fadeOut</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#foo&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">fadeIn</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>The element <code>#foo</code> will fade out and then fade in, instead of getting stuck in some sort of tug of war for opacity. This is because they are both using the <code>fx</code> queue of <code>#foo</code>, and thus run one after another.</p>
<h3>Creating and manipulating queues</h3>
<p>jQuery provides 5 commands for interacting with queues, which are summarized in the table below.</p>
<table>
<tr>
<th>Command</th>
<th>Summary</th>
</tr>
<tr>
<td>animate</td>
<td>Perform a custom animation of a set of CSS properties.</td>
</tr>
<tr>
<td>queue</td>
<td>Show the queue of functions to be executed on the matched elements, or manipulate the queue of functions to be executed on the matched elements.</td>
</tr>
<tr>
<td>dequeue</td>
<td>Execute the next function on the queue for the matched elements.</td>
</tr>
<tr>
<td>clearQueue</td>
<td>Remove from the queue all items that have not yet been run.</td>
</tr>
<tr>
<td>delay</td>
<td>Set a timer to delay execution of subsequent items in the queue.</td>
</tr>
</table>
<p>So how do we create a queue? We simply use a queue command that takes a <code>queueName</code> argument.  For example, we can use the <code>queue</code> command and add a function to queue. Let&#8217;s try it first with the regular <code>fx</code> queue.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#foo&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">queue</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>next<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    alert<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;I was queued!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    next<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>When this code is run on a page with a <code>#foo</code> element, it immediately emits an alert saying &#8220;I was queued!&#8221;.  This is because the <code>fx</code> queue will dequeue elements right after they&#8217;re added.   We can verify that the <code>fx</code> queue is being used by queuing an animation before our alert.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#foo&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">fadeOut</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">queue</span><span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>next<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    alert<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;I was queued!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    next<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>When this code is run the <code>#foo</code> element fades out and then an alert is shown. Now let&#8217;s try it with a custom queue.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#foo&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">queue</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;custom&quot;</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>next<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    alert<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;I was queued!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    next<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Nothing happens. Why? Because unlike the default <code>fx</code> queue, custom queues are not dequeued immediately. They require an explicit <code>dequeue</code> call to get them started.</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#foo&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">queue</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;custom&quot;</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>next<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    alert<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;I was queued!&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    next<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
.<span style="color: #660066;">dequeue</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;custom&quot;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<h3>What&#8217;s the point?</h3>
<p>At this point you may be wondering, what&#8217;s the point? Why would I ever need to use queues? The answer is: to implement animation time-lines. Imagine you&#8217;re making a game and you want to have an object float up wards for 2000 milliseconds. Furthermore, you would like said object to stay completely opaque for 1000 milliseconds before slowly becoming completely transparent over the remaining 1000 milliseconds.  This time-line is shown below in tabular form (assuming that the object starts at <code>top: 100px</code>).</p>
<table>
<tr>
<th>Time</th>
<th>Top</th>
<th>Opacity</th>
</tr>
<tr>
<td>0</td>
<td>100px</td>
<td>1.0</td>
</tr>
<tr>
<td>500</td>
<td>90px</td>
<td>1.0</td>
</tr>
<tr>
<td>1000</td>
<td>80px</td>
<td>1.0</td>
</tr>
<tr>
<td>1500</td>
<td>70px</td>
<td>0.5</td>
</tr>
<tr>
<td>2000</td>
<td>60px</td>
<td>0.0</td>
</tr>
</table>
<p>At first glance, it appears that the <code>animate</code> command could take care of this:</p>

<div class="wp_syntax"><table><tr><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#object&quot;</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">animate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>opacity<span style="color: #339933;">:</span> <span style="color: #CC0000;">0</span><span style="color: #339933;">,</span> top<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;-=40&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>duration<span style="color: #339933;">:</span> <span style="color: #CC0000;">2000</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span></pre></td></tr></table></div>

<p>Unfortunately, this code will fade the object out over 2000 ms, instead of waiting 1000 ms then fading out over the remaining 1000 ms. <code>delay</code> can&#8217;t help either, because it would delay the upward floating as well. At this point we can either fiddle with timeouts or, you guessed it, use queues.</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
5
6
7
8
9
</pre></td><td class="code"><pre class="javascript" style="font-family:monospace;">$<span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;#object&quot;</span><span style="color: #009900;">&#41;</span>
.<span style="color: #660066;">delay</span><span style="color: #009900;">&#40;</span><span style="color: #CC0000;">1000</span><span style="color: #339933;">,</span> <span style="color: #3366CC;">&quot;fader&quot;</span><span style="color: #009900;">&#41;</span>
.<span style="color: #660066;">queue</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;fader&quot;</span><span style="color: #339933;">,</span> <span style="color: #000066; font-weight: bold;">function</span><span style="color: #009900;">&#40;</span>next<span style="color: #009900;">&#41;</span> <span style="color: #009900;">&#123;</span>
    $<span style="color: #009900;">&#40;</span><span style="color: #000066; font-weight: bold;">this</span><span style="color: #009900;">&#41;</span>.<span style="color: #660066;">animate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>opacity<span style="color: #339933;">:</span> <span style="color: #CC0000;">0</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> 
        <span style="color: #009900;">&#123;</span>duration<span style="color: #339933;">:</span> <span style="color: #CC0000;">1000</span><span style="color: #339933;">,</span> queue<span style="color: #339933;">:</span> <span style="color: #003366; font-weight: bold;">false</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
    next<span style="color: #009900;">&#40;</span><span style="color: #009900;">&#41;</span><span style="color: #339933;">;</span>
<span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span>
.<span style="color: #660066;">dequeue</span><span style="color: #009900;">&#40;</span><span style="color: #3366CC;">&quot;fader&quot;</span><span style="color: #009900;">&#41;</span>
.<span style="color: #660066;">animate</span><span style="color: #009900;">&#40;</span><span style="color: #009900;">&#123;</span>top<span style="color: #339933;">:</span> <span style="color: #3366CC;">&quot;-=40&quot;</span><span style="color: #009900;">&#125;</span><span style="color: #339933;">,</span> <span style="color: #009900;">&#123;</span>duration<span style="color: #339933;">:</span> <span style="color: #CC0000;">2000</span><span style="color: #009900;">&#125;</span><span style="color: #009900;">&#41;</span></pre></td></tr></table></div>

<p>In this example, we have two queues: the <code>fx</code> queue and the <code>fader</code> queue. First off, we setup the <code>fader</code> queue. Since we want to wait 1000 ms before fading, we use the <code>delay</code> command with 1000 ms (line 2). Next, we queue up an animation that fades the object out over 1000 ms (line 3-7). Pay close attention to the <code>queue: false</code> option (line 5) we set in the <code>animate</code> command. This is critical, as it ensures that the animate doesn&#8217;t use the <code>fx</code> queue. Finally, we unleash the queue using <code>dequeue</code> and immediately follow it with a regular <code>fx</code>-using <code>animate</code> call to move the top of the object up 40 pixels (line 9).</p>
<h3>Conclusion</h3>
<p>jQuery makes use of implicit animation queues behind the scenes whenever you run an animation. By taking advantage of jQuery queue commands, custom animation queues can be constructed that enable certain complex animation stuctures, like time-lines, that would be otherwise impossible without using intervals or timeouts.</p>
]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2010/06/22/how-to-use-custom-jquery-animation-queues/feed/</wfw:commentRss>
		<slash:comments>7</slash:comments>
		</item>
		<item>
		<title>Biomorph.js: Natural Selection in JavaScript</title>
		<link>http://cdmckay.org/blog/2010/06/06/biomorph-js-natural-selection-in-javascript/</link>
		<comments>http://cdmckay.org/blog/2010/06/06/biomorph-js-natural-selection-in-javascript/#comments</comments>
		<pubDate>Mon, 07 Jun 2010 00:44:44 +0000</pubDate>
		<dc:creator>cdmckay</dc:creator>
				<category><![CDATA[Gaming]]></category>
		<category><![CDATA[JavaScript]]></category>
		<category><![CDATA[Programming]]></category>
		<category><![CDATA[Web Development]]></category>

		<guid isPermaLink="false">http://cdmckay.org/blog/?p=1588</guid>
		<description><![CDATA[For his 1986 book The Blind Watchmaker, Richard Dawkins developed a program that created &#8220;biomorphs&#8221;: virtual creatures created by a computer simulation. The simulation, called Biomorph, was developed in order to demonstrate the power of natural selection. Recently, in an effort to better understand how the Biomorph program worked, I decided to implement it myself [...]]]></description>
				<content:encoded><![CDATA[<p>For his 1986 book <a href="http://en.wikipedia.org/wiki/The_Blind_Watchmaker">The Blind Watchmaker</a>, <a href="http://en.wikipedia.org/wiki/Richard_Dawkins">Richard Dawkins</a> developed a program that created &#8220;biomorphs&#8221;: virtual creatures created by a computer simulation.  The simulation, called <a href="http://www.rennard.org/alife/english/biomintrgb.html">Biomorph</a>, was developed in order to demonstrate the power of natural selection.</p>
<p><a href="http://cdmckay.org/biomorph"><img class="aligncenter size-full wp-image-1606" title="Biomorph" src="http://cdmckay.org/blog/wp-content/blogs.dir/2/files/2010/06/biomorph-ui.png" alt="" width="350" height="190" /></a></p>
<p>Recently, in an effort to better understand how the Biomorph program worked, I decided to implement it myself in JavaScript using the HTML5 canvas element.  <a href="http://cdmckay.org/biomorph">The result of this endeavour can be found here.</a> Read more to learn how to use the Biomorph program.</p>
<p><span id="more-1588"></span></p>
<h3>How do I work it?</h3>
<p>The user interface contains 9 biomorphs arranged in a 3&#215;3 grid.  This is a biomorph generation.  The centre the grid (with the blue border) contains the parent biomorph.  The parent is the biomorph that the remaining 8 children biomorphs are based on.  The children biomorphs are created by slightly mutating each of the eight genes in a biomorph.</p>
<p>In order to evolve a biomorph towards a certain goal, look at the 8 child biomorphs and select the one that is closest to looking like the biomorph you are aiming for.  For example, if you&#8217;re trying to make a bat biomorph, pick the biomorph that looks most like a bat.  Once you have a chosen a biomorph, click it to generate a new generation of biomorphs based on it.</p>
<p>If you choose the parent biomorph, you will simple be regenerating the child generation.  However, since each gene can move in two directions, you have 16 possible children for each parent.  Thus, if you are unhappy with your choice of children, you can try clicking the parent biomorph again to get one of the 8 other child biomorphs you haven&#8217;t seen.</p>
]]></content:encoded>
			<wfw:commentRss>http://cdmckay.org/blog/2010/06/06/biomorph-js-natural-selection-in-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>
