<?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>Anand Nalya</title>
	<atom:link href="http://anandnalya.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://anandnalya.com</link>
	<description>blog</description>
	<lastBuildDate>Tue, 01 May 2012 10:25:08 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=</generator>
		<item>
		<title>Clustering of synthetic control data in R</title>
		<link>http://anandnalya.com/2012/05/01/clustering-of-synthetic-control-data-in-r/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=clustering-of-synthetic-control-data-in-r</link>
		<comments>http://anandnalya.com/2012/05/01/clustering-of-synthetic-control-data-in-r/#comments</comments>
		<pubDate>Tue, 01 May 2012 09:56:07 +0000</pubDate>
		<dc:creator>anand</dc:creator>
				<category><![CDATA[Codeprix]]></category>
		<category><![CDATA[clustering]]></category>
		<category><![CDATA[R]]></category>

		<guid isPermaLink="false">http://anandnalya.com/?p=183</guid>
		<description><![CDATA[This is an R implementation for clustering example provided with Mahuot. The orignal problem description is: A time series of control charts needs to be clustered into their close knit groups. The data set we use is synthetic and so &#8230; <a href="http://anandnalya.com/2012/05/01/clustering-of-synthetic-control-data-in-r/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>This is an R implementation for <a href="https://cwiki.apache.org/confluence/display/MAHOUT/Clustering+of+synthetic+control+data">clustering example provided with Mahuot</a>. The orignal problem description is:</p>
<blockquote><p>A time series of control charts needs to be clustered into their close knit groups. The data set we use is synthetic and so resembles real world information in an anonymized format. It contains six different classes (Normal, Cyclic, Increasing trend, Decreasing trend, Upward shift, Downward shift). With these trends occurring on the input data set, the Mahout clustering algorithm will cluster the data into their corresponding class buckets. At the end of this example, you&#8217;ll get to learn how to perform clustering using Mahout.</p></blockquote>
<p>We will be doing the same but using R instead of Mahout. The input dataset is available <a href="http://archive.ics.uci.edu/ml/databases/synthetic_control/synthetic_control.data">here</a>.</p>
<p>For running this example, in addition to <a href="http://www.r-project.org/">R</a>, you also need to install the <a href="http://cran.r-project.org/web/packages/flexclust/">flexclust</a> package available from CRAN. It provides a number of methods for clustering and cluster-visualization.</p>
<p>Here is the script:<br />
<div id="gist-2566854" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'>x <span class="o">&lt;-</span> read.table<span class="p">(</span><span class="s">&quot;synthetic_control.data&quot;</span><span class="p">)</span></div><div class='line' id='LC2'>cat<span class="p">(</span> <span class="s">&quot;read&quot;</span><span class="p">,</span> length<span class="p">(</span>x<span class="p">[,</span><span class="m">1</span><span class="p">]),</span> <span class="s">&quot;records.\n&quot;</span><span class="p">)</span></div><div class='line' id='LC3'><br/></div><div class='line' id='LC4'><span class="c1"># load clustering library</span></div><div class='line' id='LC5'>library<span class="p">(</span>flexclust<span class="p">)</span></div><div class='line' id='LC6'><br/></div><div class='line' id='LC7'><span class="c1"># get number of clusters from user</span></div><div class='line' id='LC8'>n <span class="o">&lt;-</span> as.integer<span class="p">(</span> readline<span class="p">(</span><span class="s">&quot;Enter number of clusters: &quot;</span><span class="p">))</span> </div><div class='line' id='LC9'><br/></div><div class='line' id='LC10'><span class="c1"># run kmeans clustering on the dataset</span></div><div class='line' id='LC11'>cl1 <span class="o">&lt;-</span> cclust<span class="p">(</span>x<span class="p">,</span> n<span class="p">)</span></div><div class='line' id='LC12'><br/></div><div class='line' id='LC13'>print<span class="p">(</span><span class="s">&quot;clustering complete&quot;</span><span class="p">)</span></div><div class='line' id='LC14'><br/></div><div class='line' id='LC15'><span class="c1"># show summary of clustering</span></div><div class='line' id='LC16'>summary<span class="p">(</span>cl1<span class="p">)</span></div><div class='line' id='LC17'><br/></div><div class='line' id='LC18'><span class="c1"># plot the clusters</span></div><div class='line' id='LC19'>plot<span class="p">(</span>cl1<span class="p">,</span> main<span class="o">=</span><span class="s">&quot;Clusters&quot;</span><span class="p">)</span></div><div class='line' id='LC20'><br/></div><div class='line' id='LC21'>readline<span class="p">(</span><span class="s">&quot;Press enter for cluster histogram&quot;</span><span class="p">)</span></div><div class='line' id='LC22'>m<span class="o">&lt;-</span>info<span class="p">(</span>cl1<span class="p">,</span> <span class="s">&quot;size&quot;</span><span class="p">)</span> <span class="c1"># size of each cluster</span></div><div class='line' id='LC23'>hist<span class="p">(</span>rep<span class="p">(</span><span class="m">1</span>:n<span class="p">,</span> m<span class="p">),</span> breaks<span class="o">=</span><span class="m">0</span>:n<span class="p">,</span> xlab<span class="o">=</span><span class="s">&quot;Cluster No.&quot;</span><span class="p">,</span> main<span class="o">=</span><span class="s">&quot;Cluster Plot&quot;</span><span class="p">)</span></div><div class='line' id='LC24'><br/></div><div class='line' id='LC25'>readline<span class="p">(</span><span class="s">&quot;Press enter for a plot of distance of data points from its cluster centorid&quot;</span><span class="p">)</span></div><div class='line' id='LC26'>stripes<span class="p">(</span>cl1<span class="p">)</span></div><div class='line' id='LC27'>print<span class="p">(</span><span class="s">&quot;done&quot;</span><span class="p">)</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/2566854/d6959dd548c6e51d4d01ff7c034fa4e5e19bfb44/clustering.R" style="float:right;">view raw</a>
            <a href="https://gist.github.com/2566854#file_clustering.r" style="float:right;margin-right:10px;color:#666">clustering.R</a>
            <a href="https://gist.github.com/2566854">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>
</p>
<p>Here are the graphs produced when we run the above script with no. of clusters, <code>n=7</code></p>
<h2>Clusters</h2>
<p><img src="http://anandnalya.github.com/wpcdn/images/183/cluster.jpg" alt="clusters"/></p>
<h2>Frequency Histogram</h2>
<p><img src="http://anandnalya.github.com/wpcdn/images/183/histogram.jpg" alt="frequency"/></p>
<h2>Distance from centroid</h2>
<p><img src="http://anandnalya.github.com/wpcdn/images/183/distance.jpg" alt="centroid distance"/></p>
]]></content:encoded>
			<wfw:commentRss>http://anandnalya.com/2012/05/01/clustering-of-synthetic-control-data-in-r/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Building a Single Page Webapp with jQuery</title>
		<link>http://anandnalya.com/2012/03/19/building-a-single-page-webapp-with-jquery/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=building-a-single-page-webapp-with-jquery</link>
		<comments>http://anandnalya.com/2012/03/19/building-a-single-page-webapp-with-jquery/#comments</comments>
		<pubDate>Mon, 19 Mar 2012 10:15:59 +0000</pubDate>
		<dc:creator>anand</dc:creator>
				<category><![CDATA[Codeprix]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[jQuery]]></category>
		<category><![CDATA[REST]]></category>
		<category><![CDATA[single page webapp]]></category>

		<guid isPermaLink="false">http://anandnalya.com/?p=168</guid>
		<description><![CDATA[In a typical single page web app, you load a html page and keep updating it by requesting data from the server either periodically or whenever the user asks for. Such an application is well suited for data driven systems. &#8230; <a href="http://anandnalya.com/2012/03/19/building-a-single-page-webapp-with-jquery/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>In a typical single page web app, you load a html page and keep updating it by requesting data from the server either periodically or whenever the user asks for. Such an application is well suited for data driven systems. For example, if you already have a REST API, you can easily create a single page web client for it. The best way of understanding what a single page web app is by looking at the web interface of <a href="http://mail.google.com">Gmail</a></p>
<p>So let us define what we want to achieve in the end with our application:</p>
<ul>
<li>We should be able to load different views on the same page.</li>
<li>We should be able to translate user intentions (clicks) into appropriate javascript function calls.</li>
<li>The url shown in the address bar of the browser should represent the state of the application. That is, when the user refreshes the browser, it should have the same content as prior to refresh.</li>
</ul>
<h2>The Root Page</h2>
<p>So let us begin by defining the basic structure of our application. <code>index.html</code> is our application page that will be modified during the lifetime of our application. For the sake of simplicity, let us assume that we will be making changes to <code>#application</code> div only. </p>
<div id="gist-2100959" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="cp">&lt;!DOCTYPE HTML PUBLIC &quot;-//W3C//DTD HTML 4.01//EN&quot; &quot;http://www.w3.org/TR/html4/strict.dtd&quot;&gt;</span></div><div class='line' id='LC2'><span class="nt">&lt;html&gt;</span></div><div class='line' id='LC3'><span class="nt">&lt;head&gt;</span></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;script </span><span class="na">type=</span><span class="s">&quot;text/javascript&quot;</span> <span class="na">src=</span><span class="s">&quot;./scripts/sugar.js&quot;</span><span class="nt">&gt;&lt;/script&gt;</span></div><div class='line' id='LC5'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;script </span><span class="na">type=</span><span class="s">&quot;text/javascript&quot;</span> <span class="na">src=</span><span class="s">&quot;./scripts/jquery-1.7.js&quot;</span><span class="nt">&gt;&lt;/script&gt;</span></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;script </span><span class="na">type=</span><span class="s">&quot;text/javascript&quot;</span> <span class="na">src=</span><span class="s">&quot;./scripts/app.js&quot;</span><span class="nt">&gt;&lt;/script&gt;</span></div><div class='line' id='LC7'><span class="nt">&lt;/head&gt;</span></div><div class='line' id='LC8'><span class="nt">&lt;body&gt;</span></div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;div</span> <span class="na">id=</span><span class="s">&quot;doc&quot;</span><span class="nt">&gt;</span></div><div class='line' id='LC10'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;div</span> <span class="na">id=</span><span class="s">&quot;hd&quot;</span><span class="nt">&gt;</span></div><div class='line' id='LC11'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Header/Navigation goes here</div><div class='line' id='LC12'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;/div&gt;</span></div><div class='line' id='LC13'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;div</span> <span class="na">id=</span><span class="s">&quot;mainContent&quot;</span> <span class="na">class=</span><span class="s">&quot;content&quot;</span><span class="nt">&gt;</span></div><div class='line' id='LC14'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;div</span> <span class="na">id=</span><span class="s">&quot;app&quot;</span> <span class="na">role=</span><span class="s">&quot;application&quot;</span><span class="nt">&gt;</span></div><div class='line' id='LC15'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;This is the content area </div><div class='line' id='LC16'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;/div&gt;</span></div><div class='line' id='LC17'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;/div&gt;</span></div><div class='line' id='LC18'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;div</span> <span class="na">id=</span><span class="s">&quot;ft&quot;</span><span class="nt">&gt;</span></div><div class='line' id='LC19'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Footer goes here </div><div class='line' id='LC20'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;/div&gt;</span></div><div class='line' id='LC21'><span class="nt">&lt;/body&gt;</span></div><div class='line' id='LC22'><span class="nt">&lt;/html&gt;</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/2100959/4678e552fb6881e35196d48920aceb387066fd95/index.html" style="float:right;">view raw</a>
            <a href="https://gist.github.com/2100959#file_index.html" style="float:right;margin-right:10px;color:#666">index.html</a>
            <a href="https://gist.github.com/2100959">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<h2>Intercepting link clicks</h2>
<p>Now we need to intercept all the click actions on the links and change the url hash accordingly. For handling the url hashes, we are using the <a href="http://benalman.com/projects/jquery-bbq-plugin/">Jquery BBQ plugin</a>. Intercepting the link clicks will help us in preventing the default navigation to other pages. Also, as this is the required behaviour for all the links, we will setup the interceptor at global level. If required you can change the scope of this interception by modifying the jQuery selector where we define the interception.</p>
<div id="gist-2100959" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nx">$</span><span class="p">(</span><span class="nb">document</span><span class="p">).</span><span class="nx">ready</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span></div><div class='line' id='LC2'><br/></div><div class='line' id='LC3'>	<span class="c1">// convert all a/href to a#href</span></div><div class='line' id='LC4'>	<span class="nx">$</span><span class="p">(</span><span class="s2">&quot;body&quot;</span><span class="p">).</span><span class="nx">delegate</span><span class="p">(</span><span class="s2">&quot;a&quot;</span><span class="p">,</span> <span class="s2">&quot;click&quot;</span><span class="p">,</span> <span class="kd">function</span><span class="p">(){</span></div><div class='line' id='LC5'>		<span class="kd">var</span> <span class="nx">href</span> <span class="o">=</span> <span class="nx">$</span><span class="p">(</span><span class="k">this</span><span class="p">).</span><span class="nx">attr</span><span class="p">(</span><span class="s2">&quot;href&quot;</span><span class="p">);</span> <span class="c1">// modify the selector here to change the scope of intercpetion</span></div><div class='line' id='LC6'><br/></div><div class='line' id='LC7'>		 <span class="c1">// Push this URL &quot;state&quot; onto the history hash.</span></div><div class='line' id='LC8'>		<span class="nx">$</span><span class="p">.</span><span class="nx">bbq</span><span class="p">.</span><span class="nx">pushState</span><span class="p">(</span><span class="nx">href</span><span class="p">,</span><span class="mi">2</span><span class="p">);</span></div><div class='line' id='LC9'><br/></div><div class='line' id='LC10'>		<span class="c1">// Prevent the default click behavior.</span></div><div class='line' id='LC11'>		<span class="k">return</span> <span class="kc">false</span><span class="p">;</span></div><div class='line' id='LC12'>	<span class="p">});</span></div><div class='line' id='LC13'><span class="p">});</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/2100959/fb58eea5526f9f6ee09ea3dc97d999027852db7d/app_1.js" style="float:right;">view raw</a>
            <a href="https://gist.github.com/2100959#file_app_1.js" style="float:right;margin-right:10px;color:#666">app_1.js</a>
            <a href="https://gist.github.com/2100959">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<h2>Defining the Routes</h2>
<p>Next, we need to define the routes that are mapped to url hash. Also, since each hashchange represents an action on part of user, we will also define what function to execute for each hashchange. We do this by listening for <code>haschange</code> event and firing the appropriate js function.</p>
<div id="gist-2100959" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nx">$</span><span class="p">(</span><span class="nb">document</span><span class="p">).</span><span class="nx">ready</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span></div><div class='line' id='LC2'><br/></div><div class='line' id='LC3'>	<span class="c1">// Bind a callback that executes when document.location.hash changes.</span></div><div class='line' id='LC4'>	<span class="nx">$</span><span class="p">(</span><span class="nb">window</span><span class="p">).</span><span class="nx">bind</span><span class="p">(</span> <span class="s2">&quot;hashchange&quot;</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">e</span><span class="p">)</span> <span class="p">{</span></div><div class='line' id='LC5'>		<span class="kd">var</span> <span class="nx">url</span> <span class="o">=</span> <span class="nb">Object</span><span class="p">.</span><span class="nx">extended</span><span class="p">(</span><span class="nx">$</span><span class="p">.</span><span class="nx">bbq</span><span class="p">.</span><span class="nx">getState</span><span class="p">()).</span><span class="nx">keys</span><span class="p">();</span></div><div class='line' id='LC6'><br/></div><div class='line' id='LC7'>		<span class="k">if</span><span class="p">(</span><span class="nx">url</span><span class="p">.</span><span class="nx">length</span><span class="o">==</span><span class="mi">1</span><span class="p">){</span></div><div class='line' id='LC8'>			<span class="nx">url</span> <span class="o">=</span> <span class="nx">url</span><span class="p">[</span><span class="mi">0</span><span class="p">];</span></div><div class='line' id='LC9'>		<span class="p">}</span><span class="k">else</span><span class="p">{</span></div><div class='line' id='LC10'>			<span class="k">return</span><span class="p">;</span></div><div class='line' id='LC11'>		<span class="p">}</span></div><div class='line' id='LC12'><br/></div><div class='line' id='LC13'>		<span class="c1">// url action mapping</span></div><div class='line' id='LC14'>		<span class="k">if</span><span class="p">(</span><span class="nx">url</span><span class="p">.</span><span class="nx">has</span><span class="p">(</span><span class="sr">/^\/users$/</span><span class="p">)){</span></div><div class='line' id='LC15'>			<span class="nx">showUserList</span><span class="p">();</span></div><div class='line' id='LC16'>		<span class="p">}</span> <span class="k">else</span> <span class="k">if</span> <span class="p">(</span><span class="nx">url</span><span class="p">.</span><span class="nx">has</span><span class="p">(</span><span class="sr">/^\/users\/\d+$/</span><span class="p">)){</span> <span class="c1">// matching /users/1234</span></div><div class='line' id='LC17'>			<span class="nx">showUser</span><span class="p">(</span><span class="nx">url</span><span class="p">)</span></div><div class='line' id='LC18'>		<span class="p">}</span></div><div class='line' id='LC19'>		<span class="c1">// add more routes</span></div><div class='line' id='LC20'>	<span class="p">});</span></div><div class='line' id='LC21'><span class="p">});</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/2100959/792a6310785d4f6eea4f48de190c44a6c95d6e75/app_2.js" style="float:right;">view raw</a>
            <a href="https://gist.github.com/2100959#file_app_2.js" style="float:right;margin-right:10px;color:#666">app_2.js</a>
            <a href="https://gist.github.com/2100959">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<h2>Initialization</h2>
<p>Even though, we have proper routes and actions defined now, our index page is still empty. We now need to initialize the app. This is done by setting the default <code>hash</code>, which will fire the <code>hashchange</code> event when the page loads or in case the user has refreshed the page, just triggering the <code>hashchange</code> event manually.</p>
<div id="gist-2100959" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nx">$</span><span class="p">(</span><span class="nb">document</span><span class="p">).</span><span class="nx">ready</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span>	</div><div class='line' id='LC2'>	<span class="k">if</span><span class="p">(</span><span class="nb">window</span><span class="p">.</span><span class="nx">location</span><span class="p">.</span><span class="nx">hash</span><span class="o">==</span><span class="s1">&#39;&#39;</span><span class="p">){</span></div><div class='line' id='LC3'>		<span class="nb">window</span><span class="p">.</span><span class="nx">location</span><span class="p">.</span><span class="nx">hash</span><span class="o">=</span><span class="s2">&quot;#/users&quot;</span><span class="p">;</span> <span class="c1">// home page, show the default view (user list)</span></div><div class='line' id='LC4'>	<span class="p">}</span><span class="k">else</span><span class="p">{</span></div><div class='line' id='LC5'>		<span class="nx">$</span><span class="p">(</span><span class="nb">window</span><span class="p">).</span><span class="nx">trigger</span><span class="p">(</span> <span class="s2">&quot;hashchange&quot;</span> <span class="p">);</span> <span class="c1">// user refreshed the browser, fire the appropriate function</span></div><div class='line' id='LC6'>	<span class="p">}</span></div><div class='line' id='LC7'><span class="p">});</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/2100959/afdecf8cf2c80dc79df64d4c8aeca03490bc2315/app_3.js" style="float:right;">view raw</a>
            <a href="https://gist.github.com/2100959#file_app_3.js" style="float:right;margin-right:10px;color:#666">app_3.js</a>
            <a href="https://gist.github.com/2100959">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<h2>Sample Action</h2>
<p>Now that we have the skeleton for our application ready, lets have a look at a sample action. Suppose the default action, is showing a list of users [<code>#/users</code>]. We have mapped this hash to the function <code>showUserList</code>. Now there are several ways of combining the html structure and the json data for userList, but we will be using the simplest of methods. First we will get the html fragment representing the userList using the  <code>$.load</code> function, and then we will populate it with actual data that we get from the REST api.
<div id="gist-2100959" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="kd">var</span> <span class="nx">showUserList</span> <span class="o">=</span> <span class="kd">function</span><span class="p">(){</span></div><div class='line' id='LC2'>	<span class="nx">$</span><span class="p">(</span><span class="s1">&#39;#app&#39;</span><span class="p">).</span><span class="nx">load</span><span class="p">(</span><span class="s1">&#39;/html/users.html #userListDiv&#39;</span><span class="p">,</span> <span class="kd">function</span><span class="p">()</span> <span class="p">{</span></div><div class='line' id='LC3'>		<span class="nx">$</span><span class="p">.</span><span class="nx">getJSON</span><span class="p">(</span><span class="s1">&#39;/users&#39;</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">data</span><span class="p">)</span> <span class="p">{</span></div><div class='line' id='LC4'><br/></div><div class='line' id='LC5'>			<span class="c1">// create the user list</span></div><div class='line' id='LC6'>			<span class="kd">var</span> <span class="nx">items</span> <span class="o">=</span> <span class="p">[</span> <span class="s1">&#39;&lt;ul&gt;&#39;</span><span class="p">];</span></div><div class='line' id='LC7'>			<span class="nx">$</span><span class="p">.</span><span class="nx">each</span><span class="p">(</span><span class="nx">data</span><span class="p">,</span> <span class="kd">function</span><span class="p">(</span><span class="nx">index</span><span class="p">,</span> <span class="nx">item</span><span class="p">)</span> <span class="p">{</span></div><div class='line' id='LC8'>				<span class="nx">items</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="s1">&#39;&lt;li&gt;&lt;a href=&quot;/users/&quot;&#39;</span> <span class="o">+</span> <span class="nx">item</span><span class="p">.</span><span class="nx">id</span> <span class="o">+</span> <span class="s1">&#39;&quot;&gt;&#39;</span></div><div class='line' id='LC9'>						<span class="o">+</span> <span class="nx">item</span><span class="p">.</span><span class="nx">name</span> <span class="o">+</span> <span class="s1">&#39;&lt;/a&gt;&lt;/li&gt;&#39;</span><span class="p">);</span></div><div class='line' id='LC10'>			<span class="p">});</span></div><div class='line' id='LC11'>			<span class="nx">items</span><span class="p">.</span><span class="nx">push</span><span class="p">(</span><span class="s1">&#39;&lt;/ul&gt;&#39;</span><span class="p">);</span></div><div class='line' id='LC12'><br/></div><div class='line' id='LC13'>			<span class="kd">var</span> <span class="nx">result</span> <span class="o">=</span> <span class="nx">items</span><span class="p">.</span><span class="nx">join</span><span class="p">(</span><span class="s1">&#39;&#39;</span><span class="p">);</span></div><div class='line' id='LC14'><br/></div><div class='line' id='LC15'>			<span class="c1">// clear current user list</span></div><div class='line' id='LC16'>			<span class="nx">$</span><span class="p">(</span><span class="s1">&#39;#userListDiv&#39;</span><span class="p">).</span><span class="nx">html</span><span class="p">(</span><span class="s1">&#39;&#39;</span><span class="p">);</span></div><div class='line' id='LC17'><br/></div><div class='line' id='LC18'>			<span class="c1">// add new user list</span></div><div class='line' id='LC19'>			<span class="nx">$</span><span class="p">(</span><span class="nx">result</span><span class="p">).</span><span class="nx">appendTo</span><span class="p">(</span><span class="s1">&#39;#userListDiv&#39;</span><span class="p">);</span></div><div class='line' id='LC20'>		<span class="p">});</span></div><div class='line' id='LC21'>	<span class="p">}</span></div><div class='line' id='LC22'><span class="p">};</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/2100959/cffa6990804e2b0de1fb2b918bfd763fa2fb22c1/app_4.js" style="float:right;">view raw</a>
            <a href="https://gist.github.com/2100959#file_app_4.js" style="float:right;margin-right:10px;color:#666">app_4.js</a>
            <a href="https://gist.github.com/2100959">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>If you find all the string concatenation going on in above snippet a bit sloppy, you can always use your favourite template plugin for jQuery. </p>
<p>Note: The <code>Object.extended</code> function in above snippets comes from the wonderful <a href="http://sugarjs.com">Sugar.js</a> library that makes working with native javascript objects a breeze.</p>
]]></content:encoded>
			<wfw:commentRss>http://anandnalya.com/2012/03/19/building-a-single-page-webapp-with-jquery/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Idea for Windows Phone Team &#8211; Make switching easier and less costlier</title>
		<link>http://anandnalya.com/2012/01/04/idea-for-windows-phone-team-make-switching-easier-and-less-costlier/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=idea-for-windows-phone-team-make-switching-easier-and-less-costlier</link>
		<comments>http://anandnalya.com/2012/01/04/idea-for-windows-phone-team-make-switching-easier-and-less-costlier/#comments</comments>
		<pubDate>Wed, 04 Jan 2012 06:55:44 +0000</pubDate>
		<dc:creator>anand</dc:creator>
				<category><![CDATA[Thinking]]></category>
		<category><![CDATA[apps]]></category>
		<category><![CDATA[microsft]]></category>
		<category><![CDATA[wp7]]></category>

		<guid isPermaLink="false">http://anandnalya.com/2012/01/04/idea-for-windows-phone-team-make-switching-easier-and-less-costlier/</guid>
		<description><![CDATA[Microsoft is already paying developers for porting their applications from iOS and Android. This is all well for the developers but what about the end users. They have already invested a lot in the apps on those platform. So the &#8230; <a href="http://anandnalya.com/2012/01/04/idea-for-windows-phone-team-make-switching-easier-and-less-costlier/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Microsoft is already paying developers for porting their applications from iOS and Android. This is all well for the developers but what about the end users. They have already invested a lot in the apps on those platform. So the cost of switching is not just the hardware/carrier-contract cost but also buying all those apps again for WP7. Also, what happens to all the data that is stored in the apps on other platforms?</p>
<p>Microsoft, here is an idea for: pay extra to developer to enable importing data from other platforms. And make all those apps free for the consumers who already purchased those apps on some other platform. </p>
]]></content:encoded>
			<wfw:commentRss>http://anandnalya.com/2012/01/04/idea-for-windows-phone-team-make-switching-easier-and-less-costlier/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Role based views using css and javascript</title>
		<link>http://anandnalya.com/2011/11/16/role-based-views-using-css-and-javascript/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=role-based-views-using-css-and-javascript</link>
		<comments>http://anandnalya.com/2011/11/16/role-based-views-using-css-and-javascript/#comments</comments>
		<pubDate>Wed, 16 Nov 2011 07:40:24 +0000</pubDate>
		<dc:creator>anand</dc:creator>
				<category><![CDATA[Codeprix]]></category>
		<category><![CDATA[css]]></category>
		<category><![CDATA[javascript]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[roles]]></category>
		<category><![CDATA[security]]></category>

		<guid isPermaLink="false">http://anandnalya.com/?p=157</guid>
		<description><![CDATA[In web applications, role based views are normally done using server side if..else tags etc. When developing pure javascript clients, one way of doing role based views is again using javascript conditionals. This approach suffers from the fact that role-related &#8230; <a href="http://anandnalya.com/2011/11/16/role-based-views-using-css-and-javascript/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>In web applications, role based views are normally done using server side <code>if..else</code> <a href="http://static.springsource.org/spring-security/site/docs/3.0.x/reference/taglibs.html#d0e6296">tags</a> etc. When developing pure javascript clients, one way of doing role based views is again using javascript conditionals. This approach suffers from the fact that role-related code is scattered throughout the document. Another simple way to achieve the same is using css classes.</p>
<p>Let us assume that we have three roles, <code>ADMIN</code> and <code>USER</code> and <code>GUEST</code>. Then we will define three css classes as follows:</p>
<div id="gist-1369451" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nc">.role_admin</span><span class="o">,</span> <span class="nc">.role_user</span><span class="o">,</span> <span class="nc">.role_guest</span><span class="p">{</span></div><div class='line' id='LC2'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">display</span><span class="o">:</span><span class="k">none</span><span class="p">;</span></div><div class='line' id='LC3'><span class="p">}</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1369451/f8e3f87ba993b3f17cb7cac9404c8b2566263ed3/role.css" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1369451#file_role.css" style="float:right;margin-right:10px;color:#666">role.css</a>
            <a href="https://gist.github.com/1369451">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>Now consider the following html doc, that has css classes attached to elements according to the current user&#8217;s role.</p>
<div id="gist-1369451" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nt">&lt;html&gt;</span></div><div class='line' id='LC2'><span class="nt">&lt;head&gt;</span></div><div class='line' id='LC3'><span class="nt">&lt;link</span> <span class="na">href=</span><span class="s">&quot;./role.css&quot;</span> <span class="na">rel=</span><span class="s">&quot;stylesheet&quot;</span> <span class="na">type=</span><span class="s">&quot;text/css&quot;</span> <span class="nt">/&gt;</span></div><div class='line' id='LC4'><span class="nt">&lt;/head&gt;</span></div><div class='line' id='LC5'><span class="nt">&lt;body&gt;</span></div><div class='line' id='LC6'><span class="nt">&lt;h1</span> <span class="na">class=</span><span class="s">&quot;role_admin&quot;</span><span class="nt">&gt;</span>This should be visible to admin only.<span class="nt">&lt;h1&gt;</span></div><div class='line' id='LC7'><span class="nt">&lt;h2</span> <span class="na">class=</span><span class="s">&quot;role_user&quot;</span><span class="nt">&gt;</span>This is for the normal logged in user.<span class="nt">&lt;/h1&gt;</span></div><div class='line' id='LC8'><span class="nt">&lt;h3</span> <span class="na">class=</span><span class="s">&quot;role_guest&quot;</span><span class="nt">&gt;</span>And this is for the guest.<span class="nt">&lt;/h1&gt;</span></div><div class='line' id='LC9'><span class="nt">&lt;/body&gt;</span></div><div class='line' id='LC10'><span class="nt">&lt;/html&gt;</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1369451/a30ef0aa9a9852da64669667cfd8b3fd922dc6eb/roleSample.html" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1369451#file_role_sample.html" style="float:right;margin-right:10px;color:#666">roleSample.html</a>
            <a href="https://gist.github.com/1369451">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>At this stage if you load the document in a browser, you will not see anything as nothing all the roles are invisible. So lets spruce this up with a bit of javascript to set proper css class properties.</p>
<div id="gist-1369451" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nx">$</span><span class="p">(</span><span class="nb">document</span><span class="p">).</span><span class="nx">ready</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span></div><div class='line' id='LC2'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// We are assuming that jQuery and jQuery Cookie plugin are included in the doc</span></div><div class='line' id='LC3'><br/></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">role</span> <span class="o">=</span> <span class="nx">$</span><span class="p">.</span><span class="nx">cookie</span><span class="p">(</span><span class="s1">&#39;user_role&#39;</span><span class="p">);</span> <span class="c1">// Should return one of &#39;ADMIN&#39;, &#39;USER&#39; OR &#39;GUEST&#39;</span></div><div class='line' id='LC5'><br/></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// set property for relevant css class</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">cssClass</span> <span class="o">=</span> <span class="s1">&#39;.role_&#39;</span><span class="o">+</span> <span class="nx">role</span><span class="p">.</span><span class="nx">toLowerCase</span><span class="p">();</span></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">$</span><span class="p">(</span><span class="s2">&quot;&lt;style type=&#39;text/css&#39;&gt; &quot;</span><span class="o">+</span> <span class="nx">cssClass</span> <span class="o">+</span> <span class="s2">&quot;{ display:block } &lt;/style&gt;&quot;</span><span class="p">).</span><span class="nx">appendTo</span><span class="p">(</span><span class="s2">&quot;head&quot;</span><span class="p">);</span></div><div class='line' id='LC9'><span class="p">});</span></div><div class='line' id='LC10'><br/></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1369451/d27ccc00e3b0607785bb11cec4d118a9ca62695b/role.js" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1369451#file_role.js" style="float:right;margin-right:10px;color:#666">role.js</a>
            <a href="https://gist.github.com/1369451">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>If a cookie <code>role</code> is set that represents the current user&#8217;s role then adding the above snippet of code will enable all the elements tagged with css class <code>.role_{roleCookie}</code> to be visible.</p>
<p>Even though this is a very simple implementation, we can modify it easily to take into account more complex scenarios. For example, if you have completely ordered role based visibility (access level), i.e. given role R1 and R2, we can always tell which role has more visibility, then we can extend as follows:
<div id="gist-1369451" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="nx">$</span><span class="p">(</span><span class="nb">document</span><span class="p">).</span><span class="nx">ready</span><span class="p">(</span><span class="kd">function</span><span class="p">()</span> <span class="p">{</span></div><div class='line' id='LC2'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// We are assuming that jQuery and jQuery Cookie plugin are included in the doc</span></div><div class='line' id='LC3'><br/></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="kd">var</span> <span class="nx">role</span> <span class="o">=</span> <span class="nx">$</span><span class="p">.</span><span class="nx">cookie</span><span class="p">(</span><span class="s1">&#39;user_role&#39;</span><span class="p">);</span> <span class="c1">// Should return one of &#39;ADMIN&#39;, &#39;USER&#39; OR &#39;GUEST&#39;</span></div><div class='line' id='LC5'><br/></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// set property for relevant css class</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">if</span><span class="p">(</span><span class="nx">role</span> <span class="o">==</span> <span class="s2">&quot;ADMIN&quot;</span><span class="p">)</span> <span class="p">{</span></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// make everything visible</span></div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">$</span><span class="p">(</span><span class="s2">&quot;&lt;style type=&#39;text/css&#39;&gt;.role_admin, .role_user, .role_guest {display:block} &lt;/style&gt;&quot;</span><span class="p">).</span><span class="nx">appendTo</span><span class="p">(</span><span class="s2">&quot;head&quot;</span><span class="p">);</span></div><div class='line' id='LC10'><br/></div><div class='line' id='LC11'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span> <span class="k">else</span> <span class="k">if</span><span class="p">(</span><span class="nx">app</span><span class="p">.</span><span class="nx">role</span> <span class="o">==</span> <span class="s2">&quot;USER&quot;</span><span class="p">)</span> <span class="p">{</span></div><div class='line' id='LC12'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// make user and guest part visible, admin part will remain invisible</span></div><div class='line' id='LC13'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">$</span><span class="p">(</span><span class="s2">&quot;&lt;style type=&#39;text/css&#39;&gt;.role_user, .role_guest {display:block} &lt;/style&gt;&quot;</span><span class="p">).</span><span class="nx">appendTo</span><span class="p">(</span><span class="s2">&quot;head&quot;</span><span class="p">);</span></div><div class='line' id='LC14'><br/></div><div class='line' id='LC15'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span> <span class="k">else</span> <span class="k">if</span><span class="p">(</span><span class="nx">app</span><span class="p">.</span><span class="nx">role</span> <span class="o">==</span> <span class="s2">&quot;GUEST&quot;</span><span class="p">)</span> <span class="p">{</span></div><div class='line' id='LC16'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="c1">// only show the guest part</span></div><div class='line' id='LC17'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nx">$</span><span class="p">(</span><span class="s2">&quot;&lt;style type=&#39;text/css&#39;&gt;.role_guest {display:block} &lt;/style&gt;&quot;</span><span class="p">).</span><span class="nx">appendTo</span><span class="p">(</span><span class="s2">&quot;head&quot;</span><span class="p">);</span></div><div class='line' id='LC18'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">}</span></div><div class='line' id='LC19'><span class="p">});</span></div><div class='line' id='LC20'><br/></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1369451/ca1c750910a67bc77f119a2551e42561337a85f6/roleOrdered.js" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1369451#file_role_ordered.js" style="float:right;margin-right:10px;color:#666">roleOrdered.js</a>
            <a href="https://gist.github.com/1369451">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p><em>Caution:</em> One thing to note here is that all the parts of the document are sent to all the users, only what is visible on the browser is role based. So, server side authorisation checks will always be there as nothing stops a suspecting user from looking into the source and firing that dreaded request.</p>
]]></content:encoded>
			<wfw:commentRss>http://anandnalya.com/2011/11/16/role-based-views-using-css-and-javascript/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>RIP Steve Jobs</title>
		<link>http://anandnalya.com/2011/10/06/rip-steve-jobs/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=rip-steve-jobs</link>
		<comments>http://anandnalya.com/2011/10/06/rip-steve-jobs/#comments</comments>
		<pubDate>Thu, 06 Oct 2011 04:05:54 +0000</pubDate>
		<dc:creator>anand</dc:creator>
				<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://anandnalya.com/?p=151</guid>
		<description><![CDATA[RIP Steve Jobs]]></description>
			<content:encoded><![CDATA[<p><a href="http://www.apple.com/stevejobs/">RIP Steve Jobs</a></p>
]]></content:encoded>
			<wfw:commentRss>http://anandnalya.com/2011/10/06/rip-steve-jobs/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Hadoop Tuning Notes</title>
		<link>http://anandnalya.com/2011/09/13/hadoop-tuning-note/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=hadoop-tuning-note</link>
		<comments>http://anandnalya.com/2011/09/13/hadoop-tuning-note/#comments</comments>
		<pubDate>Tue, 13 Sep 2011 08:26:41 +0000</pubDate>
		<dc:creator>anand</dc:creator>
				<category><![CDATA[Codeprix]]></category>
		<category><![CDATA[config]]></category>
		<category><![CDATA[deployment]]></category>
		<category><![CDATA[hadoop]]></category>
		<category><![CDATA[tuning]]></category>

		<guid isPermaLink="false">http://anandnalya.com/?p=147</guid>
		<description><![CDATA[This is a quick dump of my notes for hadoop tuning. General Hadoop is designed to use multiple cores and disks, so it will be able to take full advantage of more powerful hardware. Don&#8217;t use RAID for HDFS datanodes &#8230; <a href="http://anandnalya.com/2011/09/13/hadoop-tuning-note/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>This is a quick dump of my notes for hadoop tuning.</p>
<h2>General</h2>
<ul>
<li>Hadoop is designed to use multiple cores and disks, so it will be able to take full advantage of more powerful hardware.</li>
<li>Don&#8217;t use RAID for HDFS datanodes as redundancy is handled by HDFS. RAID should be used for namenodes as it provides protection against metadata corruption.</li>
<li>Machine running the namenode should run on a 64-bit system to avoid 3GB limit on JVM heap size.</li>
<li>In case the cluster consists of more than one rack, it is recommended to tell Hadoop about the network topology. Rack awareness will help Hadoop in calculating data locality while assigning MapReduce tasks and it will also help HDFS to choose replica location for files more intelligently.</li>
<li>Two processors will be engaged by datanode and tasktracker, and the remaining <code>n-2</code> processors can have a factor of 1 to 2 extra jobs.</li>
<li>On Master node, each of namenode, jobtracker and secondary namenode takes 1000M of memory. If you have a large number of files than increase the JVM heap size for namenode and secondary namenode.</li>
</ul>
<h2>Configuration Parameters</h2>
<h3>HDFS</h3>
<dl>
<dt>dfs.block.size</dt>
<dd>The block size used by HDFS which defaults to 64MB. On large clusters this can be increased to 128MB or 256MB to reduce memory requirements on namenode and also to increase the size of data given to map tasks.</dd>
<dt>dfs.name.dir</dt>
<dd>should give a list of directories where the namenode persists copies of data. It should be one or two local disks and a remote disk such as nfs mounted directory so that in case of node failure, metadata can be recovered from the remote disk.</dd>
<dt>dfs.data.dir</dt>
<dd>specifies the list of directories used by datanodes to store data. It should always be local disks and if there are multiple disks then each directory should be on different disk so as to maximize parallel read and writes.</dd>
<dt>fs.checkpoint.dir</dt>
<dd>list of directories where secondary namenode keeps checkpoints. It should use redundant disks for the same reason as <code>dfs.name.dir</code></dd>
<dt>dfs.replication</dt>
<dd>number of copies of data to be maintained. It should be at least 2 more than the number of machines that are expected to fail everyday in the cluster.</dd>
<dt>dfs.access.time.precision</dt>
<dd>The precision in msec that access times are maintained. It this value is 0, no access time are maintained resulting in performance boosts. <em>Also, Storage disks should be mounted with <code>noatime</code> which disables last access time updates during file reads resulting in considerable performance gains.</em></dd>
<dt>dfs.datanode.handler.count</dt>
<dd>Number of threads handling block requests. In case on multiple physical disks, the throughput can increase by increasing this number from default value of 3.</dd>
<dt>dfs.namenode.handler.count</dt>
<dd>Number of threads on Namenode. This number should be increase from default value of 10 for large clusters.</dd>
</dl>
<h3>MapReduce</h3>
<dl>
<dt>mapred.local.dir</dt>
<dd>is the list of directories where intermediate data and working files are store by the tasks. These should be a number of local disks to facilitate parallel IO. Also, these partitions should the same which are used by datanodes to store data <code>(dfs.data.dir)</code>
<dd>
<dt>mapred.tasktracker.map.tasks.maximum, mapred.tasktracker.reduce.tasks.maximum</dt>
<dd>specifies the maximum number of map and reduce tasks that can be run at the same time. It should be a multiple of number of cores.</dd>
<dt>mapred.job.tracker.handler.count</dt>
<dd>Number of server threads for handling tasktracker request. Default value is 10 and the recommended value is 4% of the tasktracker nodes.</dd>
<dt>mapred.child.java.opts</dt>
<dd>increase the JVM heap memory of tasks that require more memory.</dd>
</dl>
<h3>Others</h3>
<dl>
<dt>core-site.xml::io.file.buffer.size</dt>
<dd>This is buffer size used by hadoop during IO which default to 4KB. On modern system it can be increased to 64KB or 128KB for performance gains.</dd>
</dl>
]]></content:encoded>
			<wfw:commentRss>http://anandnalya.com/2011/09/13/hadoop-tuning-note/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Building a simple [Yahoo] S4 application</title>
		<link>http://anandnalya.com/2011/09/05/building-a-simple-yahoo-s4-application/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=building-a-simple-yahoo-s4-application</link>
		<comments>http://anandnalya.com/2011/09/05/building-a-simple-yahoo-s4-application/#comments</comments>
		<pubDate>Mon, 05 Sep 2011 13:22:04 +0000</pubDate>
		<dc:creator>anand</dc:creator>
				<category><![CDATA[Codeprix]]></category>
		<category><![CDATA[event processing]]></category>
		<category><![CDATA[hadoop]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[s4]]></category>
		<category><![CDATA[stream processing]]></category>
		<category><![CDATA[yahoo s4]]></category>

		<guid isPermaLink="false">http://anandnalya.com/?p=139</guid>
		<description><![CDATA[<p><a href="http://s4.io/">S4</a> is a distributed stream processing platform from Yahoo. It is often seen as the real-time counterpart of <a href="http://hadoop.apache.org/">Hadoop</a>. S4 being fault tolerant and horizontally scalable helps you in building very large stream processing application that can do anything from detecting earthquakes to finding that perfect bit of advertising that the visitor on your website is most likely to click.</p>
<p>At its core, an S4 application consists of a number of Processing Elements <em>(PEs)</em> that are wired together with the help of a <a href="http://static.springsource.org/spring/docs/3.1.0.M2/spring-framework-reference/html/beans.html#beans-definition">spring</a> configuration file that defines the <em>PEs</em> and the flow of events in the system. Also, events are produced by event producers that listen that sends these events to the client adapter for S4, from where, the S4 platform takes over and dispatch it to appropriate processing elements. After processing these events, <em>PEs</em> can choose to dispatch them to other <em>PEs</em> for further processing or they can choose to produce output events. Thus, arbitrarily complex behavior can be derived together by wiring a simple set of <em>PEs</em>.</p>

<p>S4 comes with a few example applications, but here is a much simpler <a href="https://github.com/anandnalya/S4WordCount">S4WordCount</a> application that shows how to:
<ol><li>Keep state in a <em>PE</em>.</li>
<li>Dispatch events from a <em>PE</em>.</li>
<li>Process multiple events from a single <em>PE</em>.</li>
<li>Write a simple java client for sending events to S4.</li></ol></p> <a href="http://anandnalya.com/2011/09/05/building-a-simple-yahoo-s4-application/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p><a href="http://s4.io/">S4</a> is a distributed stream processing platform from Yahoo. It is often seen as the real-time counterpart of <a href="http://hadoop.apache.org/">Hadoop</a>. S4 being fault tolerant and horizontally scalable helps you in building very large stream processing application that can do anything from detecting earthquakes to finding that perfect bit of advertising that the visitor on your website is most likely to click.</p>
<p>At its core, an S4 application consists of a number of Processing Elements <em>(PEs)</em> that are wired together with the help of a <a href="http://static.springsource.org/spring/docs/3.1.0.M2/spring-framework-reference/html/beans.html#beans-definition">spring</a> configuration file that defines the <em>PEs</em> and the flow of events in the system. Also, events are produced by event producers that listen that sends these events to the client adapter for S4, from where, the S4 platform takes over and dispatch it to appropriate processing elements. After processing these events, <em>PEs</em> can choose to dispatch them to other <em>PEs</em> for further processing or they can choose to produce output events. Thus, arbitrarily complex behavior can be derived together by wiring a simple set of <em>PEs</em>.</p>
<p>S4 comes with a few example applications, but here is a much simpler <a href="https://github.com/anandnalya/S4WordCount">S4WordCount</a> application that shows how to:</p>
<ol>
<li>Keep state in a <em>PE</em>.</li>
<li>Dispatch events from a <em>PE</em>.</li>
<li>Process multiple events from a single <em>PE</em>.</li>
<li>Write a simple java client for sending events to S4.</li>
</ol>
<p>In S4WordCount, we will build a simple <code>WordReceiverPE</code>, that will receive events in the form of word and will simply print these words on <code>stdout</code>. It will also identify sentences in the word stream and then forward these sentences for further processing to <code>SenteceReceiverPE</code>. <code>WordReceiverPE</code> will also produce receive sentence events and print them to <code>stdout</code>.</p>
<p>First lets have a look at <code>Word</code> and <code>Sentence</code>, the event object used in our example:</p>
<div id="gist-1194444" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="kn">package</span> <span class="n">test</span><span class="o">.</span><span class="na">s4</span><span class="o">;</span></div><div class='line' id='LC2'><br/></div><div class='line' id='LC3'><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Word</span> <span class="o">{</span></div><div class='line' id='LC4'>	<span class="kd">private</span> <span class="n">String</span> <span class="n">string</span><span class="o">;</span></div><div class='line' id='LC5'><br/></div><div class='line' id='LC6'>	<span class="kd">public</span> <span class="n">String</span> <span class="nf">getString</span><span class="o">()</span> <span class="o">{</span></div><div class='line' id='LC7'>		<span class="k">return</span> <span class="n">string</span><span class="o">;</span></div><div class='line' id='LC8'>	<span class="o">}</span></div><div class='line' id='LC9'><br/></div><div class='line' id='LC10'>	<span class="kd">public</span> <span class="kt">void</span> <span class="nf">setString</span><span class="o">(</span><span class="n">String</span> <span class="n">message</span><span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC11'>		<span class="k">this</span><span class="o">.</span><span class="na">string</span> <span class="o">=</span> <span class="n">message</span><span class="o">;</span></div><div class='line' id='LC12'>	<span class="o">}</span></div><div class='line' id='LC13'><br/></div><div class='line' id='LC14'>	<span class="nd">@Override</span></div><div class='line' id='LC15'>	<span class="kd">public</span> <span class="n">String</span> <span class="nf">toString</span><span class="o">()</span> <span class="o">{</span></div><div class='line' id='LC16'>		<span class="k">return</span> <span class="s">&quot;Word [string=&quot;</span> <span class="o">+</span> <span class="n">string</span> <span class="o">+</span> <span class="s">&quot;]&quot;</span><span class="o">;</span></div><div class='line' id='LC17'>	<span class="o">}</span></div><div class='line' id='LC18'><span class="o">}</span></div><div class='line' id='LC19'><br/></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1194444/84b7b2b714cefe17d525904424cd5854a0d32aae/Word.java" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1194444#file_word.java" style="float:right;margin-right:10px;color:#666">Word.java</a>
            <a href="https://gist.github.com/1194444">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>
<br />
<div id="gist-1194444" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="kn">package</span> <span class="n">test</span><span class="o">.</span><span class="na">s4</span><span class="o">;</span></div><div class='line' id='LC2'><br/></div><div class='line' id='LC3'><span class="kd">public</span> <span class="kd">class</span> <span class="nc">Sentence</span> <span class="o">{</span></div><div class='line' id='LC4'>	<span class="kd">private</span> <span class="n">String</span> <span class="n">string</span><span class="o">;</span></div><div class='line' id='LC5'><br/></div><div class='line' id='LC6'>	<span class="kd">public</span> <span class="nf">Sentence</span><span class="o">(){</span></div><div class='line' id='LC7'>		<span class="c1">// default constructor</span></div><div class='line' id='LC8'>	<span class="o">}</span></div><div class='line' id='LC9'><br/></div><div class='line' id='LC10'>	<span class="kd">public</span> <span class="nf">Sentence</span><span class="o">(</span><span class="n">String</span> <span class="n">string</span><span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC11'>		<span class="k">this</span><span class="o">.</span><span class="na">string</span> <span class="o">=</span> <span class="n">string</span><span class="o">;</span></div><div class='line' id='LC12'>	<span class="o">}</span></div><div class='line' id='LC13'><br/></div><div class='line' id='LC14'>	<span class="kd">public</span> <span class="n">String</span> <span class="nf">getString</span><span class="o">()</span> <span class="o">{</span></div><div class='line' id='LC15'>		<span class="k">return</span> <span class="n">string</span><span class="o">;</span></div><div class='line' id='LC16'>	<span class="o">}</span></div><div class='line' id='LC17'><br/></div><div class='line' id='LC18'>	<span class="kd">public</span> <span class="kt">void</span> <span class="nf">setString</span><span class="o">(</span><span class="n">String</span> <span class="n">message</span><span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC19'>		<span class="k">this</span><span class="o">.</span><span class="na">string</span> <span class="o">=</span> <span class="n">message</span><span class="o">;</span></div><div class='line' id='LC20'>	<span class="o">}</span></div><div class='line' id='LC21'><br/></div><div class='line' id='LC22'>	<span class="kd">public</span> <span class="n">String</span> <span class="nf">getSentenceId</span><span class="o">(){</span></div><div class='line' id='LC23'>		<span class="c1">// all sentences have the same key</span></div><div class='line' id='LC24'>		<span class="k">return</span> <span class="s">&quot;1&quot;</span><span class="o">;</span></div><div class='line' id='LC25'>	<span class="o">}</span></div><div class='line' id='LC26'>	<span class="kd">public</span> <span class="kt">void</span> <span class="nf">setSentenceId</span><span class="o">(</span><span class="n">String</span> <span class="n">id</span><span class="o">){</span></div><div class='line' id='LC27'>		<span class="c1">// do nothing</span></div><div class='line' id='LC28'>	<span class="o">}</span></div><div class='line' id='LC29'><br/></div><div class='line' id='LC30'>	<span class="nd">@Override</span></div><div class='line' id='LC31'>	<span class="kd">public</span> <span class="n">String</span> <span class="nf">toString</span><span class="o">()</span> <span class="o">{</span></div><div class='line' id='LC32'>		<span class="k">return</span> <span class="s">&quot;Sentence [string=&quot;</span> <span class="o">+</span> <span class="n">string</span> <span class="o">+</span> <span class="s">&quot;]&quot;</span><span class="o">;</span></div><div class='line' id='LC33'>	<span class="o">}</span></div><div class='line' id='LC34'><br/></div><div class='line' id='LC35'><span class="o">}</span></div><div class='line' id='LC36'><br/></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1194444/0eccaefede65714d3eafaaf0b563749819280b42/Sentence.java" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1194444#file_sentence.java" style="float:right;margin-right:10px;color:#666">Sentence.java</a>
            <a href="https://gist.github.com/1194444">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>S4 uses keys, which is a set of some properties of the event object, for routing/dispatching events. In this case since <code>Word</code> is the key-less entry point into the system, and we don&#8217;t have any key for it, but for <code>Sentence</code> which will be processed further, we have a <code>Sentence.sentenceId</code> as the key. (For simplicity, all <code>Sentence</code> have the same <code>sentenceId, 1</code>)</p>
<p>Now let&#8217;s have a look at our first <em>PE</em>, i.e. <code>WordReceiverPE</code>:</p>
<div id="gist-1194444" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="kn">package</span> <span class="n">test</span><span class="o">.</span><span class="na">s4</span><span class="o">;</span></div><div class='line' id='LC2'><br/></div><div class='line' id='LC3'><span class="kn">import</span> <span class="nn">io.s4.dispatcher.Dispatcher</span><span class="o">;</span></div><div class='line' id='LC4'><span class="kn">import</span> <span class="nn">io.s4.processor.AbstractPE</span><span class="o">;</span></div><div class='line' id='LC5'><br/></div><div class='line' id='LC6'><span class="cm">/**</span></div><div class='line' id='LC7'><span class="cm"> * This class receive word and sentence events and print them to stdout</span></div><div class='line' id='LC8'><span class="cm"> */</span></div><div class='line' id='LC9'><span class="kd">public</span> <span class="kd">class</span> <span class="nc">WordReceiverPE</span> <span class="kd">extends</span> <span class="n">AbstractPE</span> <span class="o">{</span></div><div class='line' id='LC10'>	<span class="kd">private</span> <span class="n">StringBuilder</span> <span class="n">builder</span> <span class="o">=</span> <span class="k">new</span> <span class="n">StringBuilder</span><span class="o">();</span></div><div class='line' id='LC11'><br/></div><div class='line' id='LC12'>	<span class="cm">/**</span></div><div class='line' id='LC13'><span class="cm">	 * Dispatcher that will dispatch events on &lt;code&gt;Sentence *&lt;/code&gt; stream.</span></div><div class='line' id='LC14'><span class="cm">	 */</span></div><div class='line' id='LC15'>	<span class="kd">private</span> <span class="n">Dispatcher</span> <span class="n">dispatcher</span><span class="o">;</span></div><div class='line' id='LC16'><br/></div><div class='line' id='LC17'>	<span class="kd">public</span> <span class="n">Dispatcher</span> <span class="nf">getDispatcher</span><span class="o">()</span> <span class="o">{</span></div><div class='line' id='LC18'>		<span class="k">return</span> <span class="n">dispatcher</span><span class="o">;</span></div><div class='line' id='LC19'>	<span class="o">}</span></div><div class='line' id='LC20'><br/></div><div class='line' id='LC21'>	<span class="kd">public</span> <span class="kt">void</span> <span class="nf">setDispatcher</span><span class="o">(</span><span class="n">Dispatcher</span> <span class="n">dispatcher</span><span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC22'>		<span class="k">this</span><span class="o">.</span><span class="na">dispatcher</span> <span class="o">=</span> <span class="n">dispatcher</span><span class="o">;</span></div><div class='line' id='LC23'>	<span class="o">}</span></div><div class='line' id='LC24'><br/></div><div class='line' id='LC25'>	<span class="cm">/**</span></div><div class='line' id='LC26'><span class="cm">	 * Process word events. This prints out the received word and also builds</span></div><div class='line' id='LC27'><span class="cm">	 * the sentence that will be dispatched once we found the end of the</span></div><div class='line' id='LC28'><span class="cm">	 * sentence identified by &lt;code&gt;.&lt;/code&gt;</span></div><div class='line' id='LC29'><span class="cm">	 * </span></div><div class='line' id='LC30'><span class="cm">	 * @param word</span></div><div class='line' id='LC31'><span class="cm">	 *            Word received on &lt;code&gt;RawWords *&lt;/code&gt; stream.</span></div><div class='line' id='LC32'><span class="cm">	 */</span></div><div class='line' id='LC33'>	<span class="kd">public</span> <span class="kt">void</span> <span class="nf">processEvent</span><span class="o">(</span><span class="n">Word</span> <span class="n">word</span><span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC34'>		<span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">&quot;Received: &quot;</span> <span class="o">+</span> <span class="n">word</span><span class="o">);</span></div><div class='line' id='LC35'><br/></div><div class='line' id='LC36'>		<span class="c1">// keep building the sentence</span></div><div class='line' id='LC37'>		<span class="n">builder</span><span class="o">.</span><span class="na">append</span><span class="o">(</span><span class="sc">&#39; &#39;</span><span class="o">).</span><span class="na">append</span><span class="o">(</span><span class="n">word</span><span class="o">.</span><span class="na">getString</span><span class="o">().</span><span class="na">trim</span><span class="o">());</span></div><div class='line' id='LC38'><br/></div><div class='line' id='LC39'>		<span class="k">if</span> <span class="o">(</span><span class="n">builder</span><span class="o">.</span><span class="na">toString</span><span class="o">().</span><span class="na">endsWith</span><span class="o">(</span><span class="s">&quot;.&quot;</span><span class="o">))</span> <span class="o">{</span></div><div class='line' id='LC40'>			<span class="n">System</span><span class="o">.</span><span class="na">err</span><span class="o">.</span><span class="na">print</span><span class="o">(</span><span class="s">&quot;End of sentence found&quot;</span><span class="o">);</span></div><div class='line' id='LC41'><br/></div><div class='line' id='LC42'>			<span class="c1">// dispatch a Sentence event</span></div><div class='line' id='LC43'>			<span class="n">dispatcher</span><span class="o">.</span><span class="na">dispatchEvent</span><span class="o">(</span><span class="s">&quot;Sentence&quot;</span><span class="o">,</span> <span class="k">new</span> <span class="n">Sentence</span><span class="o">(</span><span class="n">builder</span><span class="o">.</span><span class="na">toString</span><span class="o">()));</span></div><div class='line' id='LC44'><br/></div><div class='line' id='LC45'>			<span class="c1">// reset buffer.</span></div><div class='line' id='LC46'>			<span class="n">builder</span><span class="o">.</span><span class="na">setLength</span><span class="o">(</span><span class="mi">0</span><span class="o">);</span></div><div class='line' id='LC47'>		<span class="o">}</span></div><div class='line' id='LC48'>	<span class="o">}</span></div><div class='line' id='LC49'><br/></div><div class='line' id='LC50'>	<span class="cm">/**</span></div><div class='line' id='LC51'><span class="cm">	 * Process sentence events. This method demonstrated that one class can</span></div><div class='line' id='LC52'><span class="cm">	 * recieve multiple type of events on different Streams.</span></div><div class='line' id='LC53'><span class="cm">	 * </span></div><div class='line' id='LC54'><span class="cm">	 * @param sentence</span></div><div class='line' id='LC55'><span class="cm">	 *            Sentence recieved on &lt;code&gt;Sentence *&lt;code&gt; stream.</span></div><div class='line' id='LC56'><span class="cm">	 */</span></div><div class='line' id='LC57'>	<span class="kd">public</span> <span class="kt">void</span> <span class="nf">processEvent</span><span class="o">(</span><span class="n">Sentence</span> <span class="n">sentence</span><span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC58'>		<span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">&quot;Received Sentence(WordReceiverPE) : &quot;</span> <span class="o">+</span> <span class="n">sentence</span><span class="o">);</span></div><div class='line' id='LC59'>	<span class="o">}</span></div><div class='line' id='LC60'><br/></div><div class='line' id='LC61'>	<span class="nd">@Override</span></div><div class='line' id='LC62'>	<span class="kd">public</span> <span class="kt">void</span> <span class="nf">output</span><span class="o">()</span> <span class="o">{</span></div><div class='line' id='LC63'>		<span class="c1">// TODO Auto-generated method stub</span></div><div class='line' id='LC64'>	<span class="o">}</span></div><div class='line' id='LC65'><br/></div><div class='line' id='LC66'>	<span class="nd">@Override</span></div><div class='line' id='LC67'>	<span class="kd">public</span> <span class="n">String</span> <span class="nf">getId</span><span class="o">()</span> <span class="o">{</span></div><div class='line' id='LC68'>		<span class="k">return</span> <span class="k">this</span><span class="o">.</span><span class="na">getClass</span><span class="o">().</span><span class="na">getName</span><span class="o">();</span></div><div class='line' id='LC69'>	<span class="o">}</span></div><div class='line' id='LC70'><span class="o">}</span></div><div class='line' id='LC71'><br/></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1194444/5c95abd7494b8a851364a4a42945c3ddc82db1bc/WordReceiverPE.java" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1194444#file_word_receiver_pe.java" style="float:right;margin-right:10px;color:#666">WordReceiverPE.java</a>
            <a href="https://gist.github.com/1194444">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>We define a <code>StringBuilder</code> that will be used to accumulate words to form a <code>Sentence</code>. The <code>processEvent(Word)</code> method simply prints the received word on <code>stdout</code> and appends the word to the builder. It then checks if the sentence is complete by checking for <code>.</code> at the end of the <code>builder</code>  and if so, it creates a <code>Sentence</code> event(object) and dispatches it to the <code>Sentence</code> stream. Once dispatched the <code>processEvent(Sentence)</code> will receive that event and will again print the sentence to <code>stdout</code>.</p>
<p>Now let&#8217;s have a look at <code>SenteceReceiverPE</code>, our second <em>PE</em> which does nothing but print the received <code>Sentence</code> on <code>stdout</code>.</p>
<div id="gist-1194444" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="kn">package</span> <span class="n">test</span><span class="o">.</span><span class="na">s4</span><span class="o">;</span></div><div class='line' id='LC2'><br/></div><div class='line' id='LC3'><span class="kn">import</span> <span class="nn">io.s4.processor.AbstractPE</span><span class="o">;</span></div><div class='line' id='LC4'><br/></div><div class='line' id='LC5'><span class="cm">/**</span></div><div class='line' id='LC6'><span class="cm"> * This class receives Sentence events and print them on stdout.</span></div><div class='line' id='LC7'><span class="cm"> */</span></div><div class='line' id='LC8'><span class="kd">public</span> <span class="kd">class</span> <span class="nc">SentenceReceiverPE</span> <span class="kd">extends</span> <span class="n">AbstractPE</span> <span class="o">{</span></div><div class='line' id='LC9'><br/></div><div class='line' id='LC10'>	<span class="cm">/**</span></div><div class='line' id='LC11'><span class="cm">	 * Print received sentence event.</span></div><div class='line' id='LC12'><span class="cm">	 * @param sentence</span></div><div class='line' id='LC13'><span class="cm">	 */</span></div><div class='line' id='LC14'>	<span class="kd">public</span> <span class="kt">void</span> <span class="nf">processEvent</span><span class="o">(</span><span class="n">Sentence</span> <span class="n">sentence</span><span class="o">){</span></div><div class='line' id='LC15'>		<span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">&quot;Received Sentence: &quot;</span> <span class="o">+</span> <span class="n">sentence</span><span class="o">);</span></div><div class='line' id='LC16'>	<span class="o">}</span></div><div class='line' id='LC17'><br/></div><div class='line' id='LC18'>	<span class="nd">@Override</span></div><div class='line' id='LC19'>	<span class="kd">public</span> <span class="kt">void</span> <span class="nf">output</span><span class="o">()</span> <span class="o">{</span></div><div class='line' id='LC20'>		<span class="c1">// TODO Auto-generated method stub</span></div><div class='line' id='LC21'>	<span class="o">}</span></div><div class='line' id='LC22'><br/></div><div class='line' id='LC23'>	<span class="nd">@Override</span></div><div class='line' id='LC24'>	<span class="kd">public</span> <span class="n">String</span> <span class="nf">getId</span><span class="o">()</span> <span class="o">{</span></div><div class='line' id='LC25'>		<span class="k">return</span> <span class="k">this</span><span class="o">.</span><span class="na">getClass</span><span class="o">().</span><span class="na">getName</span><span class="o">();</span></div><div class='line' id='LC26'>	<span class="o">}</span></div><div class='line' id='LC27'><br/></div><div class='line' id='LC28'><span class="o">}</span></div><div class='line' id='LC29'><br/></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1194444/b86ab76dadb1f09c84ebbad2a2ac232343df975a/SentenceReceiverPE.java" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1194444#file_sentence_receiver_pe.java" style="float:right;margin-right:10px;color:#666">SentenceReceiverPE.java</a>
            <a href="https://gist.github.com/1194444">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>Finally, lets see the content of the application config file that wires all this together and forms a valid S4 application. The name of the config file should follow the naming convention of <code>&lt;AppName&gt;-config.xml</code>. In this case we will call the config file <code>S4WordCount-conf.xml</code></p>
<div id="gist-1194444" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="cp">&lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;</span></div><div class='line' id='LC2'><span class="nt">&lt;beans</span> <span class="na">xmlns=</span><span class="s">&quot;http://www.springframework.org/schema/beans&quot;</span></div><div class='line' id='LC3'>	<span class="na">xmlns:xsi=</span><span class="s">&quot;http://www.w3.org/2001/XMLSchema-instance&quot;</span></div><div class='line' id='LC4'>	<span class="na">xsi:schemaLocation=</span><span class="s">&quot;http://www.springframework.org/schema/beans</span></div><div class='line' id='LC5'><span class="s">       http://www.springframework.org/schema/beans/spring-beans-2.0.xsd&quot;</span><span class="nt">&gt;</span></div><div class='line' id='LC6'><br/></div><div class='line' id='LC7'>	<span class="nt">&lt;bean</span> <span class="na">id=</span><span class="s">&quot;wordCatcher&quot;</span> <span class="na">class=</span><span class="s">&quot;test.s4.WordReceiverPE&quot;</span><span class="nt">&gt;</span></div><div class='line' id='LC8'>	   <span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&quot;dispatcher&quot;</span> <span class="na">ref=</span><span class="s">&quot;dispatcher&quot;</span><span class="nt">/&gt;</span></div><div class='line' id='LC9'><br/></div><div class='line' id='LC10'>		<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&quot;keys&quot;</span><span class="nt">&gt;</span></div><div class='line' id='LC11'>		  <span class="c">&lt;!--  Listen for both words and sentences --&gt;</span></div><div class='line' id='LC12'>			<span class="nt">&lt;list&gt;</span></div><div class='line' id='LC13'>				<span class="nt">&lt;value&gt;</span>RawWords *<span class="nt">&lt;/value&gt;</span></div><div class='line' id='LC14'>				<span class="nt">&lt;value&gt;</span>Sentence *<span class="nt">&lt;/value&gt;</span></div><div class='line' id='LC15'>			<span class="nt">&lt;/list&gt;</span></div><div class='line' id='LC16'>		<span class="nt">&lt;/property&gt;</span></div><div class='line' id='LC17'>	<span class="nt">&lt;/bean&gt;</span></div><div class='line' id='LC18'><br/></div><div class='line' id='LC19'>	   <span class="nt">&lt;bean</span> <span class="na">id=</span><span class="s">&quot;sentenceCatcher&quot;</span> <span class="na">class=</span><span class="s">&quot;test.s4.SentenceReceiverPE&quot;</span><span class="nt">&gt;</span></div><div class='line' id='LC20'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&quot;keys&quot;</span><span class="nt">&gt;</span></div><div class='line' id='LC21'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;list&gt;</span></div><div class='line' id='LC22'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;value&gt;</span>Sentence *<span class="nt">&lt;/value&gt;</span></div><div class='line' id='LC23'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;/list&gt;</span></div><div class='line' id='LC24'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;/property&gt;</span></div><div class='line' id='LC25'>&nbsp;&nbsp;&nbsp;&nbsp;<span class="nt">&lt;/bean&gt;</span></div><div class='line' id='LC26'><br/></div><div class='line' id='LC27'>	<span class="nt">&lt;bean</span> <span class="na">id=</span><span class="s">&quot;dispatcher&quot;</span> <span class="na">class=</span><span class="s">&quot;io.s4.dispatcher.Dispatcher&quot;</span></div><div class='line' id='LC28'>		<span class="na">init-method=</span><span class="s">&quot;init&quot;</span><span class="nt">&gt;</span></div><div class='line' id='LC29'>		<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&quot;partitioners&quot;</span><span class="nt">&gt;</span></div><div class='line' id='LC30'>			<span class="nt">&lt;list&gt;</span></div><div class='line' id='LC31'>			<span class="c">&lt;!-- Partition on senteceId which is &quot;1&quot; for all sentences. --&gt;</span></div><div class='line' id='LC32'>				<span class="nt">&lt;ref</span> <span class="na">bean=</span><span class="s">&quot;sentenceIdPartitioner&quot;</span> <span class="nt">/&gt;</span></div><div class='line' id='LC33'>			<span class="nt">&lt;/list&gt;</span></div><div class='line' id='LC34'>		<span class="nt">&lt;/property&gt;</span></div><div class='line' id='LC35'>		<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&quot;eventEmitter&quot;</span> <span class="na">ref=</span><span class="s">&quot;commLayerEmitter&quot;</span> <span class="nt">/&gt;</span></div><div class='line' id='LC36'>		<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&quot;loggerName&quot;</span> <span class="na">value=</span><span class="s">&quot;s4&quot;</span> <span class="nt">/&gt;</span></div><div class='line' id='LC37'>	<span class="nt">&lt;/bean&gt;</span></div><div class='line' id='LC38'><br/></div><div class='line' id='LC39'>	<span class="nt">&lt;bean</span> <span class="na">id=</span><span class="s">&quot;sentenceIdPartitioner&quot;</span> <span class="na">class=</span><span class="s">&quot;io.s4.dispatcher.partitioner.DefaultPartitioner&quot;</span><span class="nt">&gt;</span></div><div class='line' id='LC40'>		<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&quot;streamNames&quot;</span><span class="nt">&gt;</span></div><div class='line' id='LC41'>			<span class="nt">&lt;list&gt;</span></div><div class='line' id='LC42'>				<span class="nt">&lt;value&gt;</span>Sentence<span class="nt">&lt;/value&gt;</span></div><div class='line' id='LC43'>			<span class="nt">&lt;/list&gt;</span></div><div class='line' id='LC44'>		<span class="nt">&lt;/property&gt;</span></div><div class='line' id='LC45'>		<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&quot;hashKey&quot;</span><span class="nt">&gt;</span></div><div class='line' id='LC46'>			<span class="nt">&lt;list&gt;</span></div><div class='line' id='LC47'>				<span class="nt">&lt;value&gt;</span>sentenceId<span class="nt">&lt;/value&gt;</span></div><div class='line' id='LC48'>			<span class="nt">&lt;/list&gt;</span></div><div class='line' id='LC49'>		<span class="nt">&lt;/property&gt;</span></div><div class='line' id='LC50'>		<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&quot;hasher&quot;</span> <span class="na">ref=</span><span class="s">&quot;hasher&quot;</span> <span class="nt">/&gt;</span></div><div class='line' id='LC51'>		<span class="nt">&lt;property</span> <span class="na">name=</span><span class="s">&quot;debug&quot;</span> <span class="na">value=</span><span class="s">&quot;true&quot;</span> <span class="nt">/&gt;</span></div><div class='line' id='LC52'>	<span class="nt">&lt;/bean&gt;</span></div><div class='line' id='LC53'><br/></div><div class='line' id='LC54'><span class="nt">&lt;/beans&gt;</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1194444/1b9943f836fcedc94a783707181c57a6c1c4b22b/S4WordCount-conf.xml" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1194444#file_s4_word_count_conf.xml" style="float:right;margin-right:10px;color:#666">S4WordCount-conf.xml</a>
            <a href="https://gist.github.com/1194444">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>This is a spring bean definition file. The first bean, <code>wordCatcher</code> is an object of class <code>test.s4.WordReceiverPE</code> and it injects a  dispatcher called <code>dispatcher</code> into <code>WordRecieverPE</code>. We will look at properties of dispatcher later. <code>keys</code> define the stream on which our PE will be listening for event. <code>RawWords *</code> means that it will be receiving all events on the stream named <code>RawWords</code> irrespective of their keys. Similar is the intention for <code>Sentence *</code>.</p>
<p>Our second bean is <code>sentenceCatcher</code> which is an object of class <code>test.s4.SentenceReceiverPE</code> and will accept all events on stream called <code>Sentence</code>.</p>
<p>Third is the definition for the <code>dispatcher</code> which we injected into <code>wordCatcher</code>. The dispatcher needs a <code>partitioner</code> that partitions the events based on some key and then dispatches them to appropriate <em>PEs</em>. In this case we are using the <code>DefaultPartitioner</code> whose properties are defined later by <code>sentenceIdPartitoner</code> bean which says that partition the event objects on <code>Sentence</code> stream by <code>sentenceId</code> property. <code>dispatcher</code> uses the S4 provided <code>commLayerEmitter</code> to emit the events.</p>
<h2>Running the application</h2>
<p>To run this application on the S4:</p>
<ol>
<li>Setup S4 as documented <a href="http://docs.s4.io/tutorials/getting_started.html#set-up-s4">here</a>.</li>
<li>Create a <code>S4WordCount.jar</code> from above classes.</li>
<li>Deploy the application on S4, by creating the following directory structure:
<pre><code>	/$S4_IMAGE
		/s4-apps
			/S4WordCount
				S4WordCount-conf.xml
				/lib
					S4Wordcount.jar</code></pre>
</li>
<li>Start S4: <code>$S4_IMAGE/scripts/start-s4.sh -r client-adapter &amp;</code></li>
<li>Start client adapter: <code>$S4_IMAGE/scripts/run-client-adapter.sh -s client-adapter -g s4 -d $S4_IMAGE/s4-core/conf/default/client-stub-conf.xml &amp;</code></li>
</ol>
<p>Now S4 is ready to receive events. Following is an event sender that uses the <a href="http://docs.s4.io/manual/client_usage.html">java client library</a> to send events to S4. It reads one word at a time from <code>stdin</code> and sends it to S4.</p>
<div id="gist-1194444" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="kn">package</span> <span class="n">test</span><span class="o">.</span><span class="na">s4</span><span class="o">.</span><span class="na">generator</span><span class="o">;</span></div><div class='line' id='LC2'><br/></div><div class='line' id='LC3'><span class="kn">import</span> <span class="nn">io.s4.client.Driver</span><span class="o">;</span></div><div class='line' id='LC4'><span class="kn">import</span> <span class="nn">io.s4.client.Message</span><span class="o">;</span></div><div class='line' id='LC5'><br/></div><div class='line' id='LC6'><span class="kn">import</span> <span class="nn">java.io.BufferedReader</span><span class="o">;</span></div><div class='line' id='LC7'><span class="kn">import</span> <span class="nn">java.io.IOException</span><span class="o">;</span></div><div class='line' id='LC8'><span class="kn">import</span> <span class="nn">java.io.InputStreamReader</span><span class="o">;</span></div><div class='line' id='LC9'><span class="kn">import</span> <span class="nn">java.io.Reader</span><span class="o">;</span></div><div class='line' id='LC10'><br/></div><div class='line' id='LC11'><span class="kd">public</span> <span class="kd">class</span> <span class="nc">TestMessageSender</span> <span class="o">{</span></div><div class='line' id='LC12'>	<span class="kd">public</span> <span class="kd">static</span> <span class="kt">void</span> <span class="nf">main</span><span class="o">(</span><span class="n">String</span><span class="o">[]</span> <span class="n">args</span><span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC13'>		<span class="k">if</span> <span class="o">(</span><span class="n">args</span><span class="o">.</span><span class="na">length</span> <span class="o">&lt;</span> <span class="mi">1</span><span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC14'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">System</span><span class="o">.</span><span class="na">err</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">&quot;No host name specified&quot;</span><span class="o">);</span></div><div class='line' id='LC15'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">System</span><span class="o">.</span><span class="na">exit</span><span class="o">(</span><span class="mi">1</span><span class="o">);</span></div><div class='line' id='LC16'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span></div><div class='line' id='LC17'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">String</span> <span class="n">hostName</span> <span class="o">=</span> <span class="n">args</span><span class="o">[</span><span class="mi">0</span><span class="o">];</span></div><div class='line' id='LC18'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div><div class='line' id='LC19'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">if</span> <span class="o">(</span><span class="n">args</span><span class="o">.</span><span class="na">length</span> <span class="o">&lt;</span> <span class="mi">2</span><span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC20'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">System</span><span class="o">.</span><span class="na">err</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">&quot;No port specified&quot;</span><span class="o">);</span></div><div class='line' id='LC21'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">System</span><span class="o">.</span><span class="na">exit</span><span class="o">(</span><span class="mi">1</span><span class="o">);</span></div><div class='line' id='LC22'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span></div><div class='line' id='LC23'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div><div class='line' id='LC24'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="kt">int</span> <span class="n">port</span> <span class="o">=</span> <span class="o">-</span><span class="mi">1</span><span class="o">;</span></div><div class='line' id='LC25'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">try</span> <span class="o">{</span></div><div class='line' id='LC26'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">port</span> <span class="o">=</span> <span class="n">Integer</span><span class="o">.</span><span class="na">parseInt</span><span class="o">(</span><span class="n">args</span><span class="o">[</span><span class="mi">1</span><span class="o">]);</span></div><div class='line' id='LC27'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="n">NumberFormatException</span> <span class="n">nfe</span><span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC28'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">System</span><span class="o">.</span><span class="na">err</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">&quot;Bad port number specified: &quot;</span> <span class="o">+</span> <span class="n">args</span><span class="o">[</span><span class="mi">1</span><span class="o">]);</span></div><div class='line' id='LC29'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">System</span><span class="o">.</span><span class="na">exit</span><span class="o">(</span><span class="mi">1</span><span class="o">);</span></div><div class='line' id='LC30'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span></div><div class='line' id='LC31'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div><div class='line' id='LC32'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">if</span> <span class="o">(</span><span class="n">args</span><span class="o">.</span><span class="na">length</span> <span class="o">&lt;</span> <span class="mi">3</span><span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC33'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">System</span><span class="o">.</span><span class="na">err</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">&quot;No stream name specified&quot;</span><span class="o">);</span></div><div class='line' id='LC34'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">System</span><span class="o">.</span><span class="na">exit</span><span class="o">(</span><span class="mi">1</span><span class="o">);</span></div><div class='line' id='LC35'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span></div><div class='line' id='LC36'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">String</span> <span class="n">streamName</span> <span class="o">=</span> <span class="n">args</span><span class="o">[</span><span class="mi">2</span><span class="o">];</span></div><div class='line' id='LC37'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div><div class='line' id='LC38'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">if</span> <span class="o">(</span><span class="n">args</span><span class="o">.</span><span class="na">length</span> <span class="o">&lt;</span> <span class="mi">4</span><span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC39'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">System</span><span class="o">.</span><span class="na">err</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">&quot;No class name specified&quot;</span><span class="o">);</span></div><div class='line' id='LC40'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">System</span><span class="o">.</span><span class="na">exit</span><span class="o">(</span><span class="mi">1</span><span class="o">);</span></div><div class='line' id='LC41'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span></div><div class='line' id='LC42'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">String</span> <span class="n">clazz</span> <span class="o">=</span> <span class="n">args</span><span class="o">[</span><span class="mi">3</span><span class="o">];</span>       </div><div class='line' id='LC43'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div><div class='line' id='LC44'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">Driver</span> <span class="n">d</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Driver</span><span class="o">(</span><span class="n">hostName</span><span class="o">,</span> <span class="n">port</span><span class="o">);</span></div><div class='line' id='LC45'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">Reader</span> <span class="n">inputReader</span> <span class="o">=</span> <span class="kc">null</span><span class="o">;</span></div><div class='line' id='LC46'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">BufferedReader</span> <span class="n">br</span> <span class="o">=</span> <span class="kc">null</span><span class="o">;</span></div><div class='line' id='LC47'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">try</span> <span class="o">{</span></div><div class='line' id='LC48'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">if</span> <span class="o">(!</span><span class="n">d</span><span class="o">.</span><span class="na">init</span><span class="o">())</span> <span class="o">{</span></div><div class='line' id='LC49'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">System</span><span class="o">.</span><span class="na">err</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">&quot;Driver initialization failed&quot;</span><span class="o">);</span></div><div class='line' id='LC50'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">System</span><span class="o">.</span><span class="na">exit</span><span class="o">(</span><span class="mi">1</span><span class="o">);</span></div><div class='line' id='LC51'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span></div><div class='line' id='LC52'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div><div class='line' id='LC53'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">if</span> <span class="o">(!</span><span class="n">d</span><span class="o">.</span><span class="na">connect</span><span class="o">())</span> <span class="o">{</span></div><div class='line' id='LC54'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">System</span><span class="o">.</span><span class="na">err</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">&quot;Driver initialization failed&quot;</span><span class="o">);</span></div><div class='line' id='LC55'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">System</span><span class="o">.</span><span class="na">exit</span><span class="o">(</span><span class="mi">1</span><span class="o">);</span>           </div><div class='line' id='LC56'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span></div><div class='line' id='LC57'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;</div><div class='line' id='LC58'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">inputReader</span> <span class="o">=</span> <span class="k">new</span> <span class="n">InputStreamReader</span><span class="o">(</span><span class="n">System</span><span class="o">.</span><span class="na">in</span><span class="o">);</span></div><div class='line' id='LC59'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">br</span> <span class="o">=</span> <span class="k">new</span> <span class="n">BufferedReader</span><span class="o">(</span><span class="n">inputReader</span><span class="o">);</span></div><div class='line' id='LC60'><br/></div><div class='line' id='LC61'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">for</span>  <span class="o">(</span><span class="n">String</span> <span class="n">inputLine</span> <span class="o">=</span> <span class="kc">null</span><span class="o">;</span> <span class="o">(</span><span class="n">inputLine</span> <span class="o">=</span> <span class="n">br</span><span class="o">.</span><span class="na">readLine</span><span class="o">())</span> <span class="o">!=</span> <span class="kc">null</span><span class="o">;)</span> <span class="o">{</span></div><div class='line' id='LC62'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">String</span> <span class="n">string</span> <span class="o">=</span> <span class="s">&quot;{\&quot;string\&quot;:\&quot;&quot;</span><span class="o">+</span><span class="n">inputLine</span><span class="o">+</span><span class="s">&quot;\&quot;}&quot;</span><span class="o">;</span></div><div class='line' id='LC63'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">System</span><span class="o">.</span><span class="na">out</span><span class="o">.</span><span class="na">println</span><span class="o">(</span><span class="s">&quot;sending &quot;</span> <span class="o">+</span> <span class="n">string</span><span class="o">);</span></div><div class='line' id='LC64'>				<span class="n">Message</span> <span class="n">m</span> <span class="o">=</span> <span class="k">new</span> <span class="n">Message</span><span class="o">(</span><span class="n">streamName</span><span class="o">,</span> <span class="n">clazz</span><span class="o">,</span> <span class="n">string</span><span class="o">);</span></div><div class='line' id='LC65'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">d</span><span class="o">.</span><span class="na">send</span><span class="o">(</span><span class="n">m</span><span class="o">);</span></div><div class='line' id='LC66'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span></div><div class='line' id='LC67'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="n">IOException</span> <span class="n">e</span><span class="o">)</span> <span class="o">{</span></div><div class='line' id='LC68'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">e</span><span class="o">.</span><span class="na">printStackTrace</span><span class="o">();</span></div><div class='line' id='LC69'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span></div><div class='line' id='LC70'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">finally</span> <span class="o">{</span></div><div class='line' id='LC71'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">try</span> <span class="o">{</span> <span class="n">d</span><span class="o">.</span><span class="na">disconnect</span><span class="o">();</span> <span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="n">Exception</span> <span class="n">e</span><span class="o">)</span> <span class="o">{}</span></div><div class='line' id='LC72'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">try</span> <span class="o">{</span> <span class="n">br</span><span class="o">.</span><span class="na">close</span><span class="o">();</span> <span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="n">Exception</span> <span class="n">e</span><span class="o">)</span> <span class="o">{}</span></div><div class='line' id='LC73'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">try</span> <span class="o">{</span> <span class="n">inputReader</span><span class="o">.</span><span class="na">close</span><span class="o">();</span> <span class="o">}</span> <span class="k">catch</span> <span class="o">(</span><span class="n">Exception</span> <span class="n">e</span><span class="o">)</span> <span class="o">{}</span></div><div class='line' id='LC74'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="o">}</span></div><div class='line' id='LC75'>	<span class="o">}</span></div><div class='line' id='LC76'><span class="o">}</span></div><div class='line' id='LC77'><br/></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1194444/4f762ba33a7344d3ed94ec7f6a92cccddc18b826/TestMessageSender.java" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1194444#file_test_message_sender.java" style="float:right;margin-right:10px;color:#666">TestMessageSender.java</a>
            <a href="https://gist.github.com/1194444">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

<p>Run client: <code>java TestMessageSender localhost 2334 RawWords test.s4.Word</code></p>
<p>Here is the sample output on the S4 console:</p>
<pre><code>
Received: Word [string=this]
Received: Word [string=is]
Received: Word [string=a]
Received: Word [string=sentence.]

Using fast path!
Value 1, partition id 0
wrapper is stream:Sentence keys:[{sentenceId = 1}:0] keyNames:null event:Sentence [string= this is a sentence.] => 0
Received Sentence(WordReceiverPE) : Sentence [string= this is a sentence.]
Received Sentence: Sentence [string= this is a sentence.]
</code></pre></p>
]]></content:encoded>
			<wfw:commentRss>http://anandnalya.com/2011/09/05/building-a-simple-yahoo-s4-application/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>Curious case of Lewis Carrol, tournament runner ups and sorting</title>
		<link>http://anandnalya.com/2011/08/26/curious-case-of-lewis-carrol-tournament-runner-ups-and-sorting/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=curious-case-of-lewis-carrol-tournament-runner-ups-and-sorting</link>
		<comments>http://anandnalya.com/2011/08/26/curious-case-of-lewis-carrol-tournament-runner-ups-and-sorting/#comments</comments>
		<pubDate>Fri, 26 Aug 2011 10:18:57 +0000</pubDate>
		<dc:creator>anand</dc:creator>
				<category><![CDATA[Codeprix]]></category>
		<category><![CDATA[algorithms]]></category>
		<category><![CDATA[python]]></category>
		<category><![CDATA[sorting]]></category>

		<guid isPermaLink="false">http://anandnalya.com/?p=134</guid>
		<description><![CDATA[No less than four grand slams are played every year to find the best tennis player in the world. These are knockout tournaments in which, at each stage half of the players lose and get out. Two [supposedly] best players &#8230; <a href="http://anandnalya.com/2011/08/26/curious-case-of-lewis-carrol-tournament-runner-ups-and-sorting/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>No less than four grand slams are played every year to find the best tennis player in the world. These are knockout tournaments in which, at each stage half of the players lose and get out. Two <i>[supposedly]</i> best players clash in the finals and the winner of that game is declared the champion (the best player) and the loser the runner up (the second best player). All is fine with our champion but there is just a small problem with our runner up: He might not be the second best player of the tournament.</p>
<p>How?</p>
<p>Let us assume that there were 16 players in the beginning with seedings 1 to 16, 16 being the best. Consider the following results for the matches in the tournament</p>
<pre>
15
	15
1
		15
5
	9
9
			16
12
	16
16
		16
4
	4
3
				16
11
	11
6
		11
7
	10
10
			14
2
	8
8
		14
13
	14
14
</pre>
<p>As expected, 16 is the winner but the runner up is 14 not 15. 15 crashed out in the semi finals playing against the champion. </p>
<p>So what can we conclude from this?</p>
<ol>
<li>Runner up may not be the second best player of the tournament.</li>
<li><code>log n</code> matches are required to decide the best player in the tournament.</li>
<li>We can find the second best player in the tournament looking at the match results of the winner which is a list of <code>log n</code> players. At some point he must have defeated the second best player.</li>
</ol>
<p>Here is a program that gives you the second best player in <code>(n - 1) + ((log n) - 1) = n + (log n) - 2</code> comparisons:</p>
<div id="gist-1173103" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="c"># Finding the second best player in a knockout tournament</span></div><div class='line' id='LC2'><br/></div><div class='line' id='LC3'><span class="n">defeatedListMap</span> <span class="o">=</span> <span class="p">{}</span></div><div class='line' id='LC4'><br/></div><div class='line' id='LC5'><span class="k">def</span> <span class="nf">fillMap</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">):</span></div><div class='line' id='LC6'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">if</span> <span class="n">x</span> <span class="ow">in</span> <span class="n">defeatedListMap</span><span class="p">:</span></div><div class='line' id='LC7'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">defeatedListMap</span><span class="p">[</span><span class="n">x</span><span class="p">]</span><span class="o">.</span><span class="n">add</span><span class="p">(</span><span class="n">y</span><span class="p">)</span></div><div class='line' id='LC8'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">else</span><span class="p">:</span></div><div class='line' id='LC9'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">defeatedListMap</span><span class="p">[</span><span class="n">x</span><span class="p">]</span> <span class="o">=</span> <span class="nb">set</span><span class="p">([</span><span class="n">y</span><span class="p">])</span></div><div class='line' id='LC10'><br/></div><div class='line' id='LC11'><br/></div><div class='line' id='LC12'><span class="k">def</span> <span class="nf">largest</span><span class="p">(</span><span class="n">array</span><span class="p">):</span></div><div class='line' id='LC13'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">if</span> <span class="nb">len</span><span class="p">(</span><span class="n">array</span><span class="p">)</span> <span class="o">==</span> <span class="mi">0</span><span class="p">:</span></div><div class='line' id='LC14'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="n">array</span></div><div class='line' id='LC15'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">elif</span> <span class="nb">len</span><span class="p">(</span><span class="n">array</span><span class="p">)</span> <span class="o">==</span> <span class="mi">1</span><span class="p">:</span></div><div class='line' id='LC16'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="n">array</span><span class="p">[</span><span class="mi">0</span><span class="p">];</span></div><div class='line' id='LC17'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">elif</span> <span class="nb">len</span><span class="p">(</span><span class="n">array</span><span class="p">)</span><span class="o">==</span> <span class="mi">2</span><span class="p">:</span></div><div class='line' id='LC18'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">fillMap</span><span class="p">(</span><span class="n">array</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span> <span class="n">array</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span></div><div class='line' id='LC19'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">if</span><span class="p">(</span><span class="n">array</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span><span class="o">&gt;</span><span class="n">array</span><span class="p">[</span><span class="mi">1</span><span class="p">]):</span></div><div class='line' id='LC20'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">fillMap</span><span class="p">(</span><span class="n">array</span><span class="p">[</span><span class="mi">0</span><span class="p">],</span><span class="n">array</span><span class="p">[</span><span class="mi">1</span><span class="p">])</span></div><div class='line' id='LC21'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="n">array</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span></div><div class='line' id='LC22'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">else</span><span class="p">:</span></div><div class='line' id='LC23'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">fillMap</span><span class="p">(</span><span class="n">array</span><span class="p">[</span><span class="mi">1</span><span class="p">],</span> <span class="n">array</span><span class="p">[</span><span class="mi">0</span><span class="p">])</span></div><div class='line' id='LC24'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="n">array</span><span class="p">[</span><span class="mi">1</span><span class="p">]</span></div><div class='line' id='LC25'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">else</span><span class="p">:</span></div><div class='line' id='LC26'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">x</span> <span class="o">=</span> <span class="n">largest</span><span class="p">(</span><span class="n">array</span><span class="p">[:</span><span class="nb">len</span><span class="p">(</span><span class="n">array</span><span class="p">)</span><span class="o">/</span><span class="mi">2</span><span class="p">])</span></div><div class='line' id='LC27'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">y</span> <span class="o">=</span> <span class="n">largest</span><span class="p">(</span><span class="n">array</span><span class="p">[</span><span class="nb">len</span><span class="p">(</span><span class="n">array</span><span class="p">)</span><span class="o">/</span><span class="mi">2</span><span class="p">:])</span></div><div class='line' id='LC28'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">if</span> <span class="n">x</span> <span class="o">&gt;</span> <span class="n">y</span><span class="p">:</span></div><div class='line' id='LC29'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">fillMap</span><span class="p">(</span><span class="n">x</span><span class="p">,</span><span class="n">y</span><span class="p">)</span></div><div class='line' id='LC30'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="n">x</span></div><div class='line' id='LC31'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">else</span><span class="p">:</span></div><div class='line' id='LC32'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="n">fillMap</span><span class="p">(</span><span class="n">y</span><span class="p">,</span><span class="n">x</span><span class="p">)</span></div><div class='line' id='LC33'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="k">return</span> <span class="n">y</span></div><div class='line' id='LC34'><br/></div><div class='line' id='LC35'><br/></div><div class='line' id='LC36'><span class="c"># generate a randomize draw for matches</span></div><div class='line' id='LC37'><span class="kn">import</span> <span class="nn">random</span></div><div class='line' id='LC38'><span class="n">inputList</span> <span class="o">=</span> <span class="nb">range</span><span class="p">(</span><span class="mi">1</span><span class="p">,</span><span class="mi">17</span><span class="p">)</span></div><div class='line' id='LC39'><span class="n">random</span><span class="o">.</span><span class="n">shuffle</span><span class="p">(</span><span class="n">inputList</span><span class="p">)</span></div><div class='line' id='LC40'><br/></div><div class='line' id='LC41'><span class="c">#get the champion</span></div><div class='line' id='LC42'><span class="n">champion</span>  <span class="o">=</span>  <span class="n">largest</span><span class="p">(</span><span class="n">inputList</span><span class="p">)</span></div><div class='line' id='LC43'><br/></div><div class='line' id='LC44'><span class="c"># look for the runner up in the list of players defeatee by champion</span></div><div class='line' id='LC45'><span class="n">runnerUp</span> <span class="o">=</span> <span class="n">largest</span><span class="p">(</span><span class="nb">list</span><span class="p">(</span><span class="n">defeatedListMap</span><span class="o">.</span><span class="n">get</span><span class="p">(</span><span class="n">champion</span><span class="p">)))</span></div><div class='line' id='LC46'><br/></div><div class='line' id='LC47'><span class="k">print</span> <span class="n">champion</span><span class="p">,</span> <span class="n">runnerUp</span></div><div class='line' id='LC48'><br/></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1173103/1870d33d5a7a273b75e69c7737aba5f02b3014c3/tournament.py" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1173103#file_tournament.py" style="float:right;margin-right:10px;color:#666">tournament.py</a>
            <a href="https://gist.github.com/1173103">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>

]]></content:encoded>
			<wfw:commentRss>http://anandnalya.com/2011/08/26/curious-case-of-lewis-carrol-tournament-runner-ups-and-sorting/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Java Code Generators &#8211; A short rant</title>
		<link>http://anandnalya.com/2011/07/27/java-code-generators-a-short-rant/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=java-code-generators-a-short-rant</link>
		<comments>http://anandnalya.com/2011/07/27/java-code-generators-a-short-rant/#comments</comments>
		<pubDate>Wed, 27 Jul 2011 06:02:51 +0000</pubDate>
		<dc:creator>anand</dc:creator>
				<category><![CDATA[Codeprix]]></category>
		<category><![CDATA[Thinking]]></category>
		<category><![CDATA[code generators]]></category>
		<category><![CDATA[django]]></category>
		<category><![CDATA[java]]></category>
		<category><![CDATA[play]]></category>
		<category><![CDATA[productivity]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[ror]]></category>

		<guid isPermaLink="false">http://anandnalya.com/?p=124</guid>
		<description><![CDATA[Java is known to be a verbose language and the situation worsens when you step into bloated enterprise java world. You need to write tons of code and configure a lot of JXXX to make your simple webapp work. Though &#8230; <a href="http://anandnalya.com/2011/07/27/java-code-generators-a-short-rant/">Continue reading <span class="meta-nav">&#8594;</span></a>]]></description>
			<content:encoded><![CDATA[<p>Java is known to be a verbose language and the situation worsens when you step into bloated enterprise java world. You need to write tons of code and configure a lot of <em>JXXX</em> to make your simple webapp work. Though the situation is improving in the recent years with the introduction of convention over configuration approach and also of annotation based configurations but still, if you compare the amount of code required for a functional webapp in Java then it would be at least 2X to 4X more than that of similar webapps written in other frameworks like <a href="https://www.djangoproject.com/">Django</a> or <a href="http://rubyonrails.org/">RoR</a>.</p>
<p>A lot of java frameworks and IDEs tries to hide this complexity by generating code &#8211; from simple getters and setters to entire DAO layers and what not &#8211; that might give small productivity gains in the beginning but eventually every additional line of code in your project, either hand-written or generated by a state of art code generator, someone will need to maintain and evolve it, which according to <a href="http://users.jyu.fi/~koskinen/smcosts.htm">some</a> is almost 90% of the total software costs.</p>
<p>Thats why framework like <a href="http://www.playframework.org/">Play</a> feels like fresh air and it seems to be making inroads in the bloated enterprise java world.</p>
]]></content:encoded>
			<wfw:commentRss>http://anandnalya.com/2011/07/27/java-code-generators-a-short-rant/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>My first Scheme program</title>
		<link>http://anandnalya.com/2010/08/23/my-first-scheme-program/?utm_source=rss&#038;utm_medium=rss&#038;utm_campaign=my-first-scheme-program</link>
		<comments>http://anandnalya.com/2010/08/23/my-first-scheme-program/#comments</comments>
		<pubDate>Mon, 23 Aug 2010 11:45:58 +0000</pubDate>
		<dc:creator>anand</dc:creator>
				<category><![CDATA[Codeprix]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[scheme]]></category>
		<category><![CDATA[sicp]]></category>

		<guid isPermaLink="false">http://anandnalya.com/?p=116</guid>
		<description><![CDATA[I&#8217;ve just started experimenting with Scheme, specifically to follow Structure and Interpretation of Computer Programs. Here is a Scheme procedure that returns the sum of squares of two larger numbers out of the given three.]]></description>
			<content:encoded><![CDATA[<p>I&#8217;ve just started experimenting with Scheme, specifically to follow <a href="http://mitpress.mit.edu/sicp/full-text/book/book.html">Structure and Interpretation of Computer Programs</a>. Here is a Scheme procedure that returns the sum of squares of two larger numbers out of the given three.<br />
<div id="gist-1124413" class="gist">

        <div class="gist-file">
          <div class="gist-data gist-syntax">
              <div class="highlight"><pre><div class='line' id='LC1'><span class="p">(</span><span class="k">define </span><span class="p">(</span><span class="nf">sumLargeSquare</span> <span class="nv">a</span> <span class="nv">b</span> <span class="nv">c</span><span class="p">)</span></div><div class='line' id='LC2'>&nbsp;&nbsp;<span class="p">(</span><span class="k">cond </span><span class="p">((</span><span class="k">and </span><span class="p">(</span><span class="nf">&amp;lt</span><span class="c1">; a b) (&amp;lt; a c)) (+ (* b b) (* c c)))</span></div><div class='line' id='LC3'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">((</span><span class="k">and </span><span class="p">(</span><span class="nf">&amp;lt</span><span class="c1">; b a) (&amp;lt; b c)) (+ (* a a) (* c c)))</span></div><div class='line' id='LC4'>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<span class="p">(</span><span class="k">else </span><span class="p">(</span><span class="nb">+ </span><span class="p">(</span><span class="nb">* </span><span class="nv">a</span> <span class="nv">a</span><span class="p">)</span> <span class="p">(</span><span class="nb">* </span><span class="nv">b</span> <span class="nv">b</span><span class="p">))))</span></div><div class='line' id='LC5'><span class="p">)</span></div></pre></div>
          </div>

          <div class="gist-meta">
            <a href="https://gist.github.com/raw/1124413/eb93d491f382fe38c9b71f1aa7c4f6e59479421e/gistfile1.scm" style="float:right;">view raw</a>
            <a href="https://gist.github.com/1124413#file_gistfile1.scm" style="float:right;margin-right:10px;color:#666">gistfile1.scm</a>
            <a href="https://gist.github.com/1124413">This Gist</a> brought to you by <a href="http://github.com">GitHub</a>.
          </div>
        </div>
</div>
 </p>
]]></content:encoded>
			<wfw:commentRss>http://anandnalya.com/2010/08/23/my-first-scheme-program/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
	</channel>
</rss>

