<?xml version="1.0" encoding="utf-8" standalone="yes" ?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
  <channel>
    <title>cat /dev/random</title>
    <link>https://prakhar.me/index.xml</link>
    <description>Recent content on cat /dev/random</description>
    <generator>Hugo -- gohugo.io</generator>
    <language>en-us</language>
    <lastBuildDate>Fri, 01 Jan 2016 12:15:24 +0530</lastBuildDate>
    <atom:link href="https://prakhar.me/index.xml" rel="self" type="application/rss+xml" />
    
    <item>
      <title>2015 - A year in review</title>
      <link>https://prakhar.me/articles/2015-year-in-review/</link>
      <pubDate>Fri, 01 Jan 2016 12:15:24 +0530</pubDate>
      
      <guid>https://prakhar.me/articles/2015-year-in-review/</guid>
      <description>

&lt;p&gt;It&amp;rsquo;s that time of year when &lt;a href=&#34;http://blog.fogus.me/2015/12/29/the-best-things-and-stuff-of-2015/&#34;&gt;people&lt;/a&gt; &lt;a href=&#34;http://nathanbarry.com/2015-review/&#34;&gt;are&lt;/a&gt; &lt;a href=&#34;https://medium.com/@cheeaun/2015-in-review-a55c23d2af59#.kedfupgln&#34;&gt;writing&lt;/a&gt; about what they accomplished in the year, sites are publishing countless &lt;em&gt;the-best-of&lt;/em&gt;  &lt;a href=&#34;http://www.themacro.com/articles/2015/12/yc-2015-reading-list/&#34;&gt;lists&lt;/a&gt; and the only thing I&amp;rsquo;m left wondering seeing &lt;a href=&#34;https://www.youtube.com/watch?v=KK9bwTlAvgo&#34;&gt;YouTube Rewind&lt;/a&gt; is - &amp;ldquo;How come I&amp;rsquo;ve never heard any of this?&amp;rdquo;&lt;/p&gt;

&lt;p&gt;Since &lt;a href=&#34;http://prakhar.me/articles/2014-year-in-review/&#34;&gt;last year&lt;/a&gt;, I too have started this ritual of writing down key highlights of my year so that I can look back a few years from now and have a chuckle at my naiveté.&lt;/p&gt;

&lt;h3 id=&#34;open-source&#34;&gt;Open Source&lt;/h3&gt;

&lt;p&gt;2015 was a &lt;strong&gt;great&lt;/strong&gt; year for me on Github. I did quite a bunch of work across various projects, different technologies and overall had a blast compulsively typing the innocuous looking incantation - &lt;code&gt;git push&lt;/code&gt;&lt;/p&gt;

&lt;figure&gt; &lt;img src=&#34;https://prakhar.me/images/2015-githubstreak.png&#34;&gt; 
    &lt;figcaption&gt;Yay! Lots of green!&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;The highlight of the year, however, was my selection in Google Summer of Code 2015 and the project (called &lt;a href=&#34;http://surveyman.github.io/&#34;&gt;Surveyman&lt;/a&gt;) that I worked on during the summer. Having a great mentor, an exciting problem and a chance to work on React gave me enough motivation to spend late-nights after my day-job to hack on the project for approximately 4 months non-stop!&lt;/p&gt;

&lt;p&gt;The first chunk of grey blocks visible around the month of March are due to a mini-burnout that I suffered. Coupled with college admissions announcements and a major release at my &lt;a href=&#34;http://xcite.com&#34;&gt;workplace&lt;/a&gt; I had no energy to hack after coming back from work. In order to recuperate, I decided to not write code on 2 days of the week - which is what you see in bottom gray boxes in April. In hindsight, it worked out well as the weekends were spent playing squash and finishing a couple of &lt;a href=&#34;https://www.destinythegame.com/&#34;&gt;video&lt;/a&gt; &lt;a href=&#34;http://www.rockstargames.com/V/&#34;&gt;games&lt;/a&gt; that were pending on my list for quite a while. Note to self: Do try this again!&lt;/p&gt;

&lt;h3 id=&#34;new-tech-react-go-and-clojure&#34;&gt;New Tech - React, Go and Clojure&lt;/h3&gt;

&lt;p&gt;If 2014 was the year of Nodejs, 2015 was the year of React. Although I jumped on the React bandwagon pretty late, I got an opportunity to work on a quite a few &lt;a href=&#34;https://github.com/search?utf8=%E2%9C%93&amp;amp;q=react+user%3Aprakhar1989&amp;amp;type=Repositories&amp;amp;ref=advsearch&amp;amp;l=&amp;amp;l=&#34;&gt;fun projects&lt;/a&gt;. From boilerplates to chrome extensions - React till now has scaled to all my needs. It feels great to have an awesome tool in my arsenal that I can wield whenever I need something done on the front-end.&lt;/p&gt;

&lt;p&gt;Towards the end of last year I promised myself to learn a &lt;a href=&#34;https://en.wikipedia.org/wiki/Lisp_%28programming_language%29&#34;&gt;Lisp&lt;/a&gt; and that&amp;rsquo;s how I stumbled onto Clojure. The sales pitch of offering the trifecta of expressiveness (macros), practicality (JVM) and concurrency sounded like a compelling alternative to the languages I was using for most of my day-to-day work. So roughly 365 days ago, I tried a &lt;a href=&#34;https://github.com/prakhar1989/clj-spellchecker&#34;&gt;small experiment&lt;/a&gt; wherein I ported some Python code over to Clojure and came away pretty impressed. In the ensuing months, after spending a good chunk of my time working on &lt;a href=&#34;http://www.4clojure.com/&#34;&gt;4clojure&lt;/a&gt; and a &lt;a href=&#34;http://mooc.fi/courses/2014/clojure/index.html&#34;&gt;mini-course&lt;/a&gt;, I eventually started writing (but unfortunately didn&amp;rsquo;t finish) a &lt;a href=&#34;http://www.clojurebyexample.org/&#34;&gt;guide&lt;/a&gt; for others. To be honest, the only reason I decided to learn React was to get started with &lt;a href=&#34;https://github.com/omcljs/om&#34;&gt;Om&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;Long story short, little did I know that the language that I had become so fond of during the year, will eventually help me land an internship!&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;I&amp;rsquo;m super excited to share that I&amp;rsquo;ll be interning this summer in the beautiful city of LA with &lt;a href=&#34;http://factual.com&#34;&gt;Factual&lt;/a&gt; where I&amp;rsquo;ll be building distributed systems in Clojure!&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;My affair with Go is still in its very early stages. Go is an ideal candidate for building easy-to-deploy concurrent programs and that&amp;rsquo;s what I used it for. Started initially with building a &lt;a href=&#34;https://github.com/prakhar1989/cloud-projects/blob/master/sqstwitt/worker/worker.go&#34;&gt;worker&lt;/a&gt; that consumes messages from a queue, I eventually ended up writing a crawler for &lt;a href=&#34;https://github.com/prakhar1989/bekanjoos/tree/master/crawler&#34;&gt;bekanjoos&lt;/a&gt; in Go as well. Overall, I think Go is a language very easy to get productive with. It is simple, has easy-to-grok concurrency primitives and produces statically compiled artifacts which are an absolute pleasure to deploy on servers. At the risk of sounding controversial - I&amp;rsquo;d recommend that every Python / Node.js developer should give Go a try for their next backend project.&lt;/p&gt;

&lt;h3 id=&#34;courses-and-papers&#34;&gt;Courses and Papers&lt;/h3&gt;

&lt;p&gt;On the academic front, this year I fared better than last year. Outside of grad school, I completed two courses - &lt;a href=&#34;https://prakhar.me/articles/big-data-with-spark/&#34;&gt;Intro to Big Data in Spark&lt;/a&gt; and &lt;a href=&#34;https://prakhar.me/articles/scalable-machine-learning/&#34;&gt;Scalable Machine Learning&lt;/a&gt;. All wasn&amp;rsquo;t as rosy though, I started with a couple more but had to drop out as I was unable to cope up. I plan to give these courses a second attempt in this year - let&amp;rsquo;s hope I am more successful this time.&lt;/p&gt;

&lt;p&gt;One department that I drastically need to improve in is reading technical papers. I&amp;rsquo;m still excruciatingly slow when it comes to reading papers and even more so when I need to blog about it. My grandiose plans of &lt;a href=&#34;https://prakhar.me/tags/distributed-systems/&#34;&gt;blogging&lt;/a&gt; about every distributed systems paper I read collapsed as the semester picked up pace and I started falling behind in classes. Clearly spending 20 hours on reading one paper, making notes and then writing about is not scalable. Writing &lt;a href=&#34;https://github.com/prakhar1989/Paper-Summaries&#34;&gt;paper summaries&lt;/a&gt; on the other hand has been a bit easier as you expect the reader to have an idea of the paper already. The only positive aspect was that I met my goal of reading more than 15 papers spanning across cloud computing, distributed systems and information retrieval.&lt;/p&gt;

&lt;h3 id=&#34;personal&#34;&gt;Personal&lt;/h3&gt;

&lt;p&gt;As far as relationships go, 2015 was just fantastic. The most exciting part of joining graduate school was meeting more like-minded nerds! I made a bunch of new friends, met school friends after eons and even played host to a complete &lt;a href=&#34;http://softwareengineeringdaily.com/about/&#34;&gt;stranger&lt;/a&gt; for an afternoon!&lt;/p&gt;

&lt;p&gt;Last year&amp;rsquo;s post ends with me promising to get more &lt;em&gt;active&lt;/em&gt;. Thankfully, having a &lt;a href=&#34;https://www.instagram.com/p/9eLp5kr4Hg/?taken-by=prakhar.srivastav&#34;&gt;room&lt;/a&gt; overlooking the picturesque &lt;a href=&#34;http://www.nycgovparks.org/park-features/riverside-park/virtual-tour&#34;&gt;Riverside Park&lt;/a&gt; meant that I didn&amp;rsquo;t have to work hard to fight my &lt;a href=&#34;http://sethgodin.typepad.com/seths_blog/2010/01/quieting-the-lizard-brain.html&#34;&gt;lizard brain&lt;/a&gt; to stay indoors. As a result, I started &lt;a href=&#34;http://www.strava.com/athletes/10955633&#34;&gt;logging my runs&lt;/a&gt; and eventually ended up running my &lt;a href=&#34;https://www.strava.com/activities/396309765&#34;&gt;first 10k&lt;/a&gt; around Central Park!&lt;/p&gt;

&lt;h3 id=&#34;plans-for-2016&#34;&gt;Plans for 2016&lt;/h3&gt;

&lt;ul&gt;
&lt;li&gt;Build a non-trivial mobile app using React Native&lt;/li&gt;
&lt;li&gt;Write more Clojure(script). Publish something useful on &lt;a href=&#34;http://clojars.org&#34;&gt;Clojars&lt;/a&gt;&lt;/li&gt;
&lt;li&gt;Blog about at least 5 more technical papers.&lt;/li&gt;
&lt;li&gt;Run at least 150 miles over the course of the entire year&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Here&amp;rsquo;s to an equally productive and fun 2016! 🍻&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Understanding RPCs - Part II</title>
      <link>https://prakhar.me/articles/understanding-rpcs-partII/</link>
      <pubDate>Thu, 24 Sep 2015 20:33:29 +0300</pubDate>
      
      <guid>https://prakhar.me/articles/understanding-rpcs-partII/</guid>
      <description>

&lt;p&gt;In the &lt;a href=&#34;https://prakhar.me/google.com&#34;&gt;previous&lt;/a&gt; post, we introduced remote procedure calls and their use within a distributed system. In this post, we are going to dive deep into the concept of semantics - first in the context of distributed systems in general and then discuss various RPC semantics.&lt;/p&gt;

&lt;h3 id=&#34;semantics&#34;&gt;Semantics&lt;/h3&gt;

&lt;p&gt;We&amp;rsquo;re now going to briefly talk about one of the most important aspects in a distributed systems - semantics. What do you mean by semantics? Before we get to that, let&amp;rsquo;s talk about failures.  As we all know, failures are the norm rather than an exception in a distributed system. Failures be of many forms - network, machine or even datacenters. Hence, one of the goals in designing a distributed system is for it to be fault tolerant. We would ideally want our systems to hide the failures as much as possible and be available to our users.&lt;/p&gt;

&lt;p&gt;However, as we&amp;rsquo;ll learn later, all kinds of failures cannot be completely hidden from the user. When one of the replica goes down, you can hide that from the user but when the majority go down it can be very hard to keep the system functioning. So what happens when your system cannot handle a failure mode or has some other limitation? The idea is to expose those &lt;strong&gt;semantics&lt;/strong&gt; very clearly in your APIs to your users so that they can then in turn plan for that scenario.&lt;/p&gt;

&lt;p&gt;One such example can be found in &lt;a href=&#34;http://rethinkdb.com/docs/consistency/#settings&#34;&gt;RethinkDB docs&lt;/a&gt; under the write acknowledgements section which is a configuration setting that can be set by the user.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The default is majority, meaning writes will be acknowledged when a majority of (voting) replicas have confirmed their writes. The other possible option is single, meaning writes will be acknowledged when a single replica acknowledges it.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;By giving this option to the user, the creators of the RethinkDB allow the users to make a tradeoff between safety and performance.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;RethinkDB’s default settings prioritize safety over performance, except in one case: read_mode defaults to single rather than majority. The majority read mode requires sending a query to all of the replicas and waiting for a majority to reply, which significantly degrades performance.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Hence, by clearly laying out semantics designers of distributed systems can keep their users informed.&lt;/p&gt;

&lt;h3 id=&#34;rpcs-semantics&#34;&gt;RPCs Semantics&lt;/h3&gt;

&lt;p&gt;With that out of the way, let&amp;rsquo;s see how what kind of semantics are exposed by RPCs. One very important point to consider is that since RPCs are the building blocks of distributed systems, the semantics dictated by RPCs bubble up to higher layers and affect the design of a distributed system.&lt;/p&gt;

&lt;p&gt;In the previous post we took at look at one of the key design goals of an RPC system.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Make the process of executing code on a remote machine as simple and straight-forward as calling a local function.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Surely this is a very worthwhile goal to pursue. Having a model that works similar to the local environment helps programmers in reasoning easily about the program and makes it easier to build distributed systems application. But the question remains that how far do RPCs systems generally come in achieveing this goal. How similar are they to LPCs (Local Procedure Calls) and to what extent does it the free the programmer from the mental overhead of thinking about communication in a distributed system?&lt;/p&gt;

&lt;p&gt;Let&amp;rsquo;s say you are writing a program and decide to make a local procedure call. In this case two things can happen -&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The call returns which means that either procedure was successful or it met with an exception.&lt;/li&gt;
&lt;li&gt;The call didn&amp;rsquo;t return which means that it got stuck in an infinite loop and it didn&amp;rsquo;t complete. In this case, the call was not successful.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;In both these cases, the final state of the system is clearly known and is easy to reason about. In our day-to-day work we deal with each scenarios multiple times and by now have become well equipped to deal with them.&lt;/p&gt;

&lt;p&gt;When communicating over a network, things are not this straightforward. Consider the case when the programmer makes a remote procedure call. When the call returns, you know that the call succeeded - it&amp;rsquo;s all fine and dandy. However, when the call fails then are multiple scenarios that could&amp;rsquo;ve happened -&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;The call didn&amp;rsquo;t reach the server.&lt;/li&gt;
&lt;li&gt;The call was executed but mid-way the server crashed.&lt;/li&gt;
&lt;li&gt;The call was executed, the results were returned but the return message did not reach the client.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;All of a sudden, something which seemed quite trivial has become sufficiently complex. To some extent, some of these problems can be dealt with repeated &lt;code&gt;ACK&lt;/code&gt;s between the server and client. This is the approach as taken by the paper. The challenge with these approaches is that more messages put a stronger bound on the performance of the system.&lt;/p&gt;

&lt;p&gt;In the face of these difficulties, there are three &lt;strong&gt;semantics&lt;/strong&gt; that have been proposed for RPC systems -&lt;/p&gt;

&lt;h4 id=&#34;at-least-once&#34;&gt;At-least-once&lt;/h4&gt;

&lt;p&gt;At-least-once are the easiest to implement. As you can guess, the promise here is that the call would be executed one times or more. The way this works that the client keeps making the call to the server as long as it doesn&amp;rsquo;t recieve an &lt;code&gt;ACK&lt;/code&gt;. In case the call is executed on the remote service but the &lt;code&gt;ACK&lt;/code&gt; got lost, the client re-sends the call and server executes it. Quite clearly, one can see that the call might be executed multiple times - this is precisely where the at-least-once semantics comes from. The psuedo-code for this can look like the following -&lt;/p&gt;

&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;method_executed&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;bp&#34;&gt;False&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;while&lt;/span&gt; &lt;span class=&#34;ow&#34;&gt;not&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;method_executed&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;ack&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;send_RPC_and_wait_for_ack&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;timeout&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;T&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ack&lt;/span&gt; &lt;span class=&#34;ow&#34;&gt;and&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ack&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;SUCCESS&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;
        &lt;span class=&#34;n&#34;&gt;method_executed&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;bp&#34;&gt;True&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;When would this be useful? These semantics are useful when the operation being requested by the client is &lt;strong&gt;idempodent&lt;/strong&gt; i.e repeated executions of the same call do not affect the state. An example of an idempodent operation is the setting a bit to 1 when it is zero and not doing anything otherwise. No matter how many times you call this operation, once the bit has been set, subsequent calls do not change the state.&lt;/p&gt;

&lt;h4 id=&#34;at-most-once&#34;&gt;At-most-once&lt;/h4&gt;

&lt;p&gt;In at-most-once the server either executes the call once or not at all. The client makes a call, the server executes it and sends an &lt;code&gt;ACK&lt;/code&gt;. Whether or not the &lt;code&gt;ACK&lt;/code&gt; is received by the client, the server does not run the procedure again.&lt;/p&gt;

&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;request_sent&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;bp&#34;&gt;False&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;while&lt;/span&gt; &lt;span class=&#34;ow&#34;&gt;not&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;request_sent&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;# only care about msg delivery&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;ack&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;send_RPC_and_wait_for_ack&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;timeout&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;T&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;request_sent&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;bp&#34;&gt;True&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ack&lt;/span&gt; &lt;span class=&#34;ow&#34;&gt;and&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;ack&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;FAILURE&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;
        &lt;span class=&#34;n&#34;&gt;request_sent&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;bp&#34;&gt;True&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;h4 id=&#34;exactly-once&#34;&gt;Exactly-once&lt;/h4&gt;

&lt;p&gt;Exactly-once semantics are the gold standard of RPC semantics. It guarantees that each call is executed just once thereby mimicing a local procedure call. However, exactly-once semantics are extremely hard to build given the complexities in a distributed system.&lt;/p&gt;

&lt;p&gt;To understand where the difficulty comes from let&amp;rsquo;s think about storing some state on the server to keep track of what all calls have been completed. This way when a new call comes in, depending on whether it has been executed or not we can take an appropriate action.&lt;/p&gt;

&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-python&#34; data-lang=&#34;python&#34;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&#34;c1&#34;&gt;# On the server side&lt;/span&gt;
&lt;span class=&#34;n&#34;&gt;state&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{}&lt;/span&gt;                       &lt;span class=&#34;c1&#34;&gt;# to manage the state&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;ow&#34;&gt;not&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;state&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;client&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;][&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;method&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]:&lt;/span&gt;    &lt;span class=&#34;c1&#34;&gt;# if not executed previously&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;resp&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;handle&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;method&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;        &lt;span class=&#34;c1&#34;&gt;# (1)&lt;/span&gt;
    &lt;span class=&#34;n&#34;&gt;state&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;client&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;][&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;method&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;resp&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;# (2) &lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;resp&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;else&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;                            &lt;span class=&#34;c1&#34;&gt;# has been executed previously&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;state&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;client&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;][&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;method&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;]&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;# return cached response&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Though the code above simple there are two big assumptions that it rests on. For starters, consider what happens when the server fails after executing &lt;code&gt;(1)&lt;/code&gt; but before &lt;code&gt;(2)&lt;/code&gt;. In this case, the server hasn&amp;rsquo;t actually recorded the fact that the execution is complete. When the server is brought back up, and the client sends the request again, the server ends up executing the call again. Hence, the exactly-once semantics are broken.&lt;/p&gt;

&lt;p&gt;One way out then is to find a way to make &lt;code&gt;(1)&lt;/code&gt; and &lt;code&gt;(2)&lt;/code&gt; atomic - ensuring either both of these commands execute or none of these commands execute. If you&amp;rsquo;ve done a course on databases, you know that this itself is a hard problem to solve.&lt;/p&gt;

&lt;h3 id=&#34;other-limitations&#34;&gt;Other limitations&lt;/h3&gt;

&lt;p&gt;Aside from the semantics, there are two other aspects of RPCs which are worth talking about -&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;&lt;p&gt;&lt;strong&gt;Address Space&lt;/strong&gt;: Suppose you&amp;rsquo;re making a local call and you need to pass in a big datastruture (e.g. linked list with 1000 nodes) to the method. How do you do that? You pass a reference to the object. The reason why this works is that in local call, the caller and the callee share the same address space. This is not true in an RPC. In such a scenario, you have to think about creative ways of wiring over a big datastructure efficiently over the network.&lt;/p&gt;&lt;/li&gt;

&lt;li&gt;&lt;p&gt;&lt;strong&gt;Performance&lt;/strong&gt;: Performance in a RPC system is order of magnitude slower than the equivalent local call. Apart from the overhead of marshalling etc. the very fact is that the Round-trip-time puts a lower bound on performance - which in itself is substantial. Hence, typically RPC systems addtionally have an asynchronous version of their API so that the main thread on the caller is not blocked while it waits for the results.&lt;/p&gt;&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&#34;conclusion&#34;&gt;Conclusion&lt;/h3&gt;

&lt;p&gt;In conclusion what we&amp;rsquo;ve seen is the mere fact of adding an unreliable network makes the goals of building a LPC-like abstraction so elusive. Although most of these problems have been solved to a large extent thanks to latest advances &amp;amp; the research but the point remains that the RPCs are much more complex than they seem to be. Hopefully with these set of blog posts, I&amp;rsquo;ve whetted your appetite enough to pursue these topics further.&lt;/p&gt;

&lt;p&gt;Till next time!&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Understanding RPCs - Part I</title>
      <link>https://prakhar.me/articles/understanding-rpcs/</link>
      <pubDate>Wed, 23 Sep 2015 20:33:29 +0300</pubDate>
      
      <guid>https://prakhar.me/articles/understanding-rpcs/</guid>
      <description>

&lt;p&gt;Having looked at a distributed system i.e the domain name system in the &lt;a href=&#34;https://prakhar.me/articles/the-domain-name-system/&#34;&gt;previous&lt;/a&gt; post, lets turn our attention to something more fundamental. In this blog post, we are going to start diving deeper into one of the basic blocks of Distributed Systems - Remote Procedure Calls or RPCs.&lt;/p&gt;

&lt;p&gt;The &lt;a href=&#34;http://www.cs.virginia.edu/~zaher/classes/CS656/birrel.pdf&#34;&gt;paper&lt;/a&gt; that we&amp;rsquo;re going to be looking at today is authored by the duo of &lt;a href=&#34;https://en.wikipedia.org/wiki/Bruce_Jay_Nelson&#34;&gt;Nelson&lt;/a&gt; and &lt;a href=&#34;https://birrell.org/andrew/me/bio.php&#34;&gt;Birell&lt;/a&gt; who were the first set of people to build an RPC implementation for their work at Xerox PARC. Nelson incidentally is also credited for the coining the term!&lt;/p&gt;

&lt;h3 id=&#34;why-rpcs&#34;&gt;Why RPCs?&lt;/h3&gt;

&lt;p&gt;In the last post, we defined a distributed system as below -&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A distributed system is a set of &lt;em&gt;independent&lt;/em&gt; machines that &lt;em&gt;coordinate&lt;/em&gt; over a &lt;em&gt;network&lt;/em&gt; to achieve a common goal.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;One of the important keywords in the above definition is &lt;em&gt;coordinate&lt;/em&gt;. However, in order to coordinate, systems first need a way to communicate. Given that we have a messaging layer (the network) what kind of a communication abstraction we can build that can be helpful to programmers while building distributed systems?&lt;/p&gt;

&lt;p&gt;RPCs are one such abstraction borrowed from programming languages that have a simple goal:&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Make the process of executing code on a remote machine as simple and straight-forward as calling a local function.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;But why did we specifically choose procedures? Let&amp;rsquo;s hear what the authors have to say -  &lt;em&gt;RPCs are based on the observation that procedure calls are a well known and well understood mechanism for transfer of control and data within a program running on a single computer. Therefore, it is proposed that this same mechanism be extended to provide for transfer of control and data across a communication network.&lt;/em&gt;&lt;/p&gt;

&lt;h3 id=&#34;components&#34;&gt;Components&lt;/h3&gt;

&lt;figure&gt;
    &lt;img data-action=&#34;zoom&#34; src=&#34;https://prakhar.me/images/rpcs.jpg&#34;&gt;&lt;/img&gt;
    &lt;figcaption&gt;Components involved in a RPC. Image courtesy: Birell and Nelson&#39;s RPC paper&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;The underlying mechanism of a RPC is simple - the caller machine makes a procedure call over a network to the callee machine which then executes the procedure locally, collects the results and sends it over to the caller.  The figure above lays out the key components in an RPC system, which at a high-level has two main pieces: a stub generator and the run-time library.&lt;/p&gt;

&lt;p&gt;The stubs are responsible for placing a specification and packing / unpacking the arguments falling that specification into the message. This message is then forwarded to the runtime so that it can be wired to the callee (in case of the user-stub). The process of packing the arguments is usually called &lt;strong&gt;marshalling&lt;/strong&gt; or &lt;strong&gt;serialization&lt;/strong&gt; of the message. Likewise, &lt;strong&gt;unmarshalling&lt;/strong&gt; or &lt;strong&gt;deserialization&lt;/strong&gt; involves extracting information received into something which the system can understand.&lt;/p&gt;

&lt;p&gt;When writing a distributed application, a programmer first writes an interface module in which she specifies the procedure names, the types of the arguments it takes and finally the return types. The compiler then uses this definition and generates the user-stub and the client-stub.&lt;/p&gt;

&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-java&#34; data-lang=&#34;java&#34;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&#34;c1&#34;&gt;// An example of an interface for generating stubs&lt;/span&gt;
&lt;span class=&#34;n&#34;&gt;service&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;FacebookService&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;{&lt;/span&gt;
  &lt;span class=&#34;c1&#34;&gt;// Returns a descriptive name of the service&lt;/span&gt;
  &lt;span class=&#34;n&#34;&gt;string&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;getName&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;(),&lt;/span&gt;

  &lt;span class=&#34;c1&#34;&gt;// Returns the version of the service&lt;/span&gt;
  &lt;span class=&#34;n&#34;&gt;string&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;getVersion&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;(),&lt;/span&gt;
    
  &lt;span class=&#34;c1&#34;&gt;// Gets an option&lt;/span&gt;
  &lt;span class=&#34;n&#34;&gt;string&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;getOption&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;string&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;key&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;),&lt;/span&gt;

  &lt;span class=&#34;c1&#34;&gt;// Gets all options&lt;/span&gt;
  &lt;span class=&#34;n&#34;&gt;map&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;n&#34;&gt;string&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;string&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;gt;&lt;/span&gt; &lt;span class=&#34;nf&#34;&gt;getOptions&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;()&lt;/span&gt;
&lt;span class=&#34;o&#34;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;The runtime is responsible for retransmissions, acknowledgments, packet routing, and encryption. It handles much of the heavy lifting in the RPC system. One of the key challenges faced by the runtime is locating the remote service. Once the service is located, the RPC package then &lt;strong&gt;binds&lt;/strong&gt; the importer of the interface to an exporter of the interface.&lt;/p&gt;

&lt;h3 id=&#34;naming&#34;&gt;Naming&lt;/h3&gt;

&lt;p&gt;The problem of &lt;strong&gt;naming&lt;/strong&gt; is a common one in distributed systems. In a cluster how do we know the names and addresses of each of the machines? How do we maintain this list and do we keep this dynamic? We&amp;rsquo;ll look at these problems much later when we talk about directory services and service discovery.&lt;/p&gt;

&lt;p&gt;In the paper the authors use Grapevine, a distributed service that provides DNS service, resource locating service, authentication and mail service.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;The major attraction of using Grapevine is that it is widely and reliably available. Grapevine is distributed across multiple servers strategically located in our internet topology, and is configured to maintain at least three copies of each database entry. Since the Grapevine servers themselves are highly reliable and the data is replicated, it is extremely rare for us to be unable to look up a database entry.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;For more info, checkout the &lt;a href=&#34;http://web.cs.wpi.edu/~cs4513/d07/Papers/Birrell,%20Levin,%20et.%20al.,%20Grapevine.pdf&#34;&gt;paper&lt;/a&gt; on Grapevine.&lt;/p&gt;

&lt;h3 id=&#34;protocol&#34;&gt;Protocol&lt;/h3&gt;

&lt;p&gt;Once the caller knows which callee it needs to communicate with, the next question is which transport level protocol to use? The authors had an option of going ahead with a PUP byte stream protocol but decided against it as the protocol was ideal for sending bigger data packets. In the case of RPC, the design goal was different -&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;One aim we emphasized in our protocol design was minimizing the elapsed real-time between initiating a call and getting results. With protocols for bulk data transfer this is not important: most of the time is spent actually transferring the data.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;In the modern world, we have an option of using UDP (an unreliable protocol) or TCP (a reliable protocol). While the choice might seem clear, the caveat here is that building RPC on top of a reliable communication protocol can lead to a severe drop in performance. Hence, many RPC packages are built on top of unreliable communication layers (e.g. UDP) and then bake in the extra logic for dealing with ACKs, retries etc.&lt;/p&gt;

&lt;p&gt;Now would be a good time to talk about &lt;a href=&#34;https://thrift.apache.org/&#34;&gt;Thrift&lt;/a&gt; - the RPC framework developed at Facebook. Thrift has its own interface language called &lt;a href=&#34;https://thrift.apache.org/docs/idl&#34;&gt;IDL&lt;/a&gt; that is used to generate the stubs. The communication protocol uses &lt;a href=&#34;https://developers.google.com/protocol-buffers/&#34;&gt;protocol buffers&lt;/a&gt; as its data format under the hood. Unlike XML/JSON protocol buffers are a binary format and hence is much smaller, less ambiguous and faster than a plain-text data format.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Apache Thrift allows you to define data types and service interfaces in a simple definition file. Taking that file as input, the compiler generates code to be used to easily build RPC clients and servers that communicate seamlessly across programming languages.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;Nowadays Thrift is being used more and more amongst heterogenous services to talk amongst each other - for example, you could write a user authentication service in Java, but call it from your Ruby web application.&lt;/p&gt;

&lt;h3 id=&#34;conclusion&#34;&gt;Conclusion&lt;/h3&gt;

&lt;p&gt;In this post we went over the overall idea behind RPCs and took a deep dive into the components which form a RPC system. In the next post we are going to continue our discussion about RPCs by talking about semantics and some limitations of RPCs.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>The Domain Name System</title>
      <link>https://prakhar.me/articles/the-domain-name-system/</link>
      <pubDate>Fri, 18 Sep 2015 20:33:29 +0300</pubDate>
      
      <guid>https://prakhar.me/articles/the-domain-name-system/</guid>
      <description>

&lt;p&gt;One of the classes that I&amp;rsquo;ve taken this semester is &lt;a href=&#34;http://roxanageambasu.github.io/ds2-class/2-papers/&#34;&gt;Advanced Distributed Systems&lt;/a&gt;. The course is primarily a research seminar wherein the students are required to read a couple of research topics for each class and participate in a two hour discussion which is guided by the professor. My goal in these series of blog posts is to summarize the papers in an approchable manner primarily to test my own understanding of the topic.&lt;/p&gt;

&lt;h3 id=&#34;the-domain-name-system&#34;&gt;The Domain Name System&lt;/h3&gt;

&lt;p&gt;The first paper discussion for this class was a &lt;a href=&#34;http://www.dtic.mil/dtic/tr/fulltext/u2/a203901.pdf&#34;&gt;paper&lt;/a&gt; on the &lt;strong&gt;Developement of the DNS&lt;/strong&gt;. This paper examines the ideas behind the initial design of the DNS in 1983 and discusses the evolution of these ideas into a working implementation. Why is this paper important in a distributed systems class? Before we answer that, lets try to define a distributed system.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;A distributed system is a set of &lt;em&gt;independent&lt;/em&gt; machines that &lt;em&gt;coordinate&lt;/em&gt; over a &lt;em&gt;network&lt;/em&gt; to achieve a common goal.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;If you look at the definition (particuarly the emphasized words) above carefully, you&amp;rsquo;ll realize that the DNS indeed behaves as a distributed system. Infact the DNS is one of earliest examples of a distributed system deployed at scale.&lt;/p&gt;

&lt;h3 id=&#34;the-paper&#34;&gt;The paper&lt;/h3&gt;

&lt;figure&gt;
    &lt;img data-action=&#34;zoom&#34; src=&#34;https://upload.wikimedia.org/wikibooks/en/7/72/Strucutre-of-dns.jpg&#34;&gt;&lt;/img&gt;
    &lt;figcaption&gt;The hierarchical structure of DNS. Image courtesy: Wikibooks&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;ul&gt;
&lt;li&gt;Before the DNS, the &lt;code&gt;HOSTS.TXT&lt;/code&gt; file was used for publishing the mapping between host names and addresses. Eventually as the number of users and workstations grew it became harder and harder to transmit the file due to its increasing size.&lt;/li&gt;
&lt;li&gt;An early design goal of the DNS system was, aside from feature-parity with &lt;code&gt;HOSTS.TXT&lt;/code&gt;, was the ability for the system to be independent of network topology and to be capable of encapsulating other names spaces.&lt;/li&gt;
&lt;li&gt;The initial DNS design assumed the necessity of striking a balance between a very lean service and a completely general distributed database. The leaner and more general the service, the easier it would be to extend and replace the existing implementation. Similarly, although the service offers lookup and persistence like a database it would not provide other features such as atomic guarantees around updates.&lt;/li&gt;
&lt;li&gt;The active components of the DNS are of two major types: name servers and resolvers.

&lt;ul&gt;
&lt;li&gt;Name servers are repositories of information, and answer queries using whatever information they possess.&lt;/li&gt;
&lt;li&gt;Resolvers interface to client programs, and embody the algorithms necessary to find a name server that has the information sought by the client.&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;li&gt;Early on it was decided that the DNS would exhibit a distributed system of control and its design (as a tree with organizations being in control of their subtree) grew as a natural consequence of this choice.&lt;/li&gt;
&lt;li&gt;The domain name of a node is the concatenation of all labels on the path from the node to the root of the tree. Hence, the domain &lt;code&gt;www.google.com&lt;/code&gt; is a node in the tree and has &lt;code&gt;www&lt;/code&gt;, &lt;code&gt;google&lt;/code&gt;, &lt;code&gt;com&lt;/code&gt; and &lt;code&gt;.&lt;/code&gt; as its ancestors. This has been demonstrated more clearly in the DNS resolution section below.&lt;/li&gt;
&lt;li&gt;The DNS provides two major mechanisms for transferring data from its ultimate source to ultimate destination: zones and caching.

&lt;ul&gt;
&lt;li&gt;Zones are sections of the system-wide database which are controlled by a specific organization. The organization controlling a zone is responsible for distributing current copies of the zones to multiple servers which make the zones available to clients throughout the Internet.&lt;br /&gt;&lt;/li&gt;
&lt;li&gt;Caching is a mechanism whereby data acquired in response to a client’s request can be locally stored against future requests by the same or other client. The mechanism for controlling caching is a time-to-live (TTL) field. However, all components of the DNS prefer authoritative information to cached information when both are available locally.&lt;/li&gt;
&lt;/ul&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The rest of the paper talks about the issues with implementing and gradually migrating the servers to use the new servers. Their experience with performance, for example, is quite indicative of how hard it is to benchmark systems that have multiple components all of which keep changing quickly.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;A related surprise was the difficulty in making reasonable measurements of DNS performance. We had planned to measure the performance of DNS components in order to estimate costs for future enhancement and growth, and to guide tuning of existing retransmission intervals, but the measurements were often swamped by unrelated effects due to gateway changes, new DNS software releases, and the like.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3 id=&#34;negative-caching&#34;&gt;Negative Caching&lt;/h3&gt;

&lt;p&gt;A &lt;strong&gt;negative cache&lt;/strong&gt; is a cache that also stores &amp;ldquo;negative&amp;rdquo; responses, i.e. failures. This means that a program remembers the result indicating a failure even after the cause has been corrected. In DNS, negative caching is a feature and the authors make a strong case for it in the paper.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Our conclusion is that any naming system that relies on caching for performance may need caching for negative results as well. Such a mechanism has been added to the DNS as an optional feature, with impressive performance gains in cases where it is supported in both the involved name servers and resolvers.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The primary reason for maintaining a negative cache is performance. As seen by the authors, typically one out of every four DNS queries were for negative results i.e asking resolvers for hosts or data that did not exist. To ensure that the volume of queries do not impact the system, the servers and resolvers would cache these negative responses with its own TTL.&lt;/p&gt;

&lt;h3 id=&#34;root-servers&#34;&gt;Root Servers&lt;/h3&gt;

&lt;p&gt;The most interesting part in the paper for me was the idea of root servers - the servers which form the apex of the domain name system. In absence of a caching mechanism every DNS query would have to flow via one of these root servers. How many servers are there? Let&amp;rsquo;s run &lt;code&gt;dig&lt;/code&gt; to find out.&lt;/p&gt;

&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-raw&#34; data-lang=&#34;raw&#34;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&#34;err&#34;&gt;$ dig NS +noadditional +noquestion +nocomments +nocmd +nostats . @8.8.8.8&lt;/span&gt;

	NS	a.root-servers.net&lt;span class=&#34;err&#34;&gt;&lt;/span&gt;
	NS	b.root-servers.net&lt;span class=&#34;err&#34;&gt;&lt;/span&gt;
	NS	c.root-servers.net&lt;span class=&#34;err&#34;&gt;&lt;/span&gt;
	NS	d.root-servers.net&lt;span class=&#34;err&#34;&gt;&lt;/span&gt;
	NS	e.root-servers.net&lt;span class=&#34;err&#34;&gt;&lt;/span&gt;
	NS	f.root-servers.net&lt;span class=&#34;err&#34;&gt;&lt;/span&gt;
	NS	g.root-servers.net&lt;span class=&#34;err&#34;&gt;&lt;/span&gt;
	NS	h.root-servers.net&lt;span class=&#34;err&#34;&gt;&lt;/span&gt;
	NS	i.root-servers.net&lt;span class=&#34;err&#34;&gt;&lt;/span&gt;
	NS	j.root-servers.net&lt;span class=&#34;err&#34;&gt;&lt;/span&gt;
	NS	k.root-servers.net&lt;span class=&#34;err&#34;&gt;&lt;/span&gt;
	NS	l.root-servers.net&lt;span class=&#34;err&#34;&gt;&lt;/span&gt;
	NS	m.root-servers.net
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;So there are &lt;em&gt;just&lt;/em&gt; 13 servers responsible for the whole internet? Well no, this does not mean there are 13 physical servers; each operator uses multiple servers distributed geographically to service the requests.&lt;/p&gt;

&lt;p&gt;Who operates these servers? These servers are collectively operated by universities, companies and government bodies. To know more about who operates which server see the entry on &lt;a href=&#34;https://en.wikipedia.org/wiki/Root_name_server#Root_server_addresses&#34;&gt;Wikipedia&lt;/a&gt;.&lt;/p&gt;

&lt;h3 id=&#34;dns-resolution&#34;&gt;DNS Resolution&lt;/h3&gt;

&lt;p&gt;Now that we know the overall architecture of the DNS lets see if we can figure out how a typical DNS resolution happens. Although the full cycle spans multiple steps, in practice heavy levels of caching at each step ensures that no typical name-server is inundated with requests. However, let&amp;rsquo;s assume that in this example all our caches are purged and hence our query goes via the root.&lt;/p&gt;

&lt;p&gt;Suppose the DNS query is for: &lt;a href=&#34;http://www.google.com/&#34;&gt;www.google.com&lt;/a&gt;&lt;/p&gt;

&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-raw&#34; data-lang=&#34;raw&#34;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&#34;err&#34;&gt;[subdomain] [domain]  [TLD] [root (implicit)]&lt;/span&gt;
&lt;span class=&#34;err&#34;&gt;    www    . google .  com   .&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;ol&gt;
&lt;li&gt;The DNS lookup starts from right to left.&lt;/li&gt;
&lt;li&gt;When the . is encountered, the client queries the root name server asking for what all name servers are available to service its request. The root server &lt;a href=&#34;http://www.digwebinterface.com/?hostnames=.&amp;amp;type=NS&amp;amp;showcommand=on&amp;amp;ns=resolver&amp;amp;useresolver=8.8.8.8&amp;amp;nameservers=&#34;&gt;responds&lt;/a&gt; with the list of 13 name servers.&lt;/li&gt;
&lt;li&gt;The client then picks a name server (lets assume d.root-servers.net) and asks the name server about the .com TLD (top-level domain). The root server &lt;a href=&#34;http://www.digwebinterface.com/?hostnames=com&amp;amp;type=NS&amp;amp;showcommand=on&amp;amp;useresolver=8.8.8.8&amp;amp;ns=self&amp;amp;nameservers=d.root-servers.net.&#34;&gt;responds&lt;/a&gt; with all the name-servers that serve this TLD (which interestingly is also 13)&lt;/li&gt;
&lt;li&gt;Following along, the client picks up a name server and asks it if it knows about the google domain. The server &lt;a href=&#34;http://www.digwebinterface.com/?hostnames=google.com&amp;amp;type=NS&amp;amp;showcommand=on&amp;amp;useresolver=8.8.8.8&amp;amp;ns=self&amp;amp;nameservers=h.gtld-servers.net.&#34;&gt;responds&lt;/a&gt; with the name servers that manage that namespace. In Google&amp;rsquo;s case, the company itself manages its name servers but for usual websites this typically resolves to the DNS provider (e.g. Godaddy / Namecheap etc.) with which the domain name is registered.&lt;/li&gt;
&lt;li&gt;In the last step &lt;a href=&#34;http://www.digwebinterface.com/?hostnames=www.google.com&amp;amp;type=A&amp;amp;showcommand=on&amp;amp;useresolver=8.8.8.8&amp;amp;ns=self&amp;amp;nameservers=ns1.google.com.&#34;&gt;the query&lt;/a&gt; for the subdomain (www / mail / maps etc.) is made and the name server accordingly maps to the IP of the server as specified in the A (Address) record. This typically for large websites is the IP of a load balancer that is responsible for forwarding requests to the correct webserver.&lt;/li&gt;
&lt;li&gt;After the DNS resolution is complete, the browser caches this mapping and uses it to directly for subsequent requests (e.g images / css / javascript etc.)&lt;/li&gt;
&lt;/ol&gt;

&lt;h3 id=&#34;conclusion&#34;&gt;Conclusion&lt;/h3&gt;

&lt;p&gt;Overall the paper give an interesting historical perspective on why the DNS came to be and the issues that came to light during its implementation and deployement. The paper, surprisingly, is also littered with good advice about software engineering and the human-side that forms the essential aspect of building software. I will end this writeup with my favorite sentence from the paper -&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;em&gt;Documentation should always be written with the assumption that only the examples are read.&lt;/em&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Till next time!&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Scalable Machine Learning</title>
      <link>https://prakhar.me/articles/scalable-machine-learning/</link>
      <pubDate>Fri, 31 Jul 2015 20:33:29 +0300</pubDate>
      
      <guid>https://prakhar.me/articles/scalable-machine-learning/</guid>
      <description>

&lt;p&gt;A couple of days ago I finally finished the last assignment of the &lt;a href=&#34;https://www.edx.org/course/scalable-machine-learning-uc-berkeleyx-cs190-1x&#34;&gt;Scalable Machine Learning&lt;/a&gt; course. The last few days in Kuwait were a bit tough as I had tons of stuff to do before leaving the country. It turns out leaving Kuwait is even harder than getting there in the first place. Additionally I had set a personal deadline of finishing the course a week before the actual deadline so that I when I reach home I could focus on other work.&lt;/p&gt;

&lt;p&gt;Enough of rambling - let&amp;rsquo;s get on with the review. Scalable Machine Learning or CS 190.1x is the second installment in the &lt;a href=&#34;https://www.edx.org/xseries&#34;&gt;Big Data series&lt;/a&gt; at EdX. The course picks up where the previous one - &lt;a href=&#34;https://www.edx.org/course/introduction-big-data-apache-spark-uc-berkeleyx-cs100-1x&#34;&gt;CS 100.1x&lt;/a&gt; left off and dives deeper into using Apache Spark&amp;rsquo;s &lt;a href=&#34;http://spark.apache.org/mllib/&#34;&gt;MLlib&lt;/a&gt; to solve machine learning problems.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;If you&amp;rsquo;re interested to know more about CS100.1x, go ahead and read the course review for it &lt;a href=&#34;https://prakhar.me/articles/big-data-with-spark/&#34;&gt;here&lt;/a&gt;.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Since MLlib runs on top of Apache Spark it comes with the same benefits as Spark i.e performance, scalability and easy-to-use APIs in Python,
Scala and Java. The library implements a host of popular machine learning algorithms out of the box such SVM, random forests and k-means clustering. In this course, however, the assignments ended up requiring only logisitic regression and linear regression with L&lt;sub&gt;1&lt;/sub&gt; and L&lt;sub&gt;2&lt;/sub&gt;-regularization.&lt;/p&gt;

&lt;h3 id=&#34;pedagogy&#34;&gt;Pedagogy&lt;/h3&gt;

&lt;p&gt;The coursework spans five weeks (six if you include the week for setup) thereby making it almost as long as CS 100.1x. The course starts off with an introduction to types of machine learning algorithms - supervised learning &amp;amp; unsupervised learning. The following week is same as the second week of CS 100.1x and hence can be skipped entirely.&lt;/p&gt;

&lt;p&gt;The three subsequent weeks cover three ML algorithms with one in each the lecture series and the accompanying labs -&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Linear Regression&lt;/li&gt;
&lt;li&gt;Logistic Regression&lt;/li&gt;
&lt;li&gt;Principal Component Analysis&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Each week there is roughly an hour long video content to be watched. At the end, lectures are followed by a quiz. Like the previous course, there are no final exams that need to be taken.&lt;/p&gt;

&lt;p&gt;In contrast to a typical ML class, the lectures are pretty light on the math which make it more approachable to newcomers. The course actually focuses more on the practical side of ML and less on theory. I wouldn’t call it a rigorous discussion of machine learning algorithms, however, they do teach the amount of theory a person will need for developing an intuition for the algorithm and solving the programming assignments. If you&amp;rsquo;ve done the ML course by Andrew Ng, then the pedagogy of this course will seem quite different.&lt;/p&gt;

&lt;h3 id=&#34;programming-assignments&#34;&gt;Programming Assignments&lt;/h3&gt;

&lt;p&gt;The programming assignments like the ones in CS 100.1x are the highlight of the course. The usage of IPython for exploratory data analysis is stellar. The notebooks follow the same incremental approach like the last course and are interspersed with beautiful graphs all of which help you in making sense of the data.&lt;/p&gt;

&lt;p&gt;The assignments deal with practical problems and thus are quite interesting. There&amp;rsquo;s one problem directly from a Kaggle competition and an assignment on PCA that deals with identifying patterns of brain activity in a larval &lt;a href=&#34;https://en.wikipedia.org/wiki/Zebrafish&#34;&gt;zebrafish&lt;/a&gt;. The end result generates a heat-map like figure which seems very gratifying after the laborious programming assignment.&lt;/p&gt;

&lt;p&gt;&lt;blockquote class=&#34;twitter-tweet tw-align-center&#34; lang=&#34;en&#34;&gt;&lt;p lang=&#34;en&#34; dir=&#34;ltr&#34;&gt;Oh look - I generated similar temporal regions in a larval zebrafish via principal component analysis. &lt;a href=&#34;https://twitter.com/hashtag/SparkMLcourse?src=hash&#34;&gt;#SparkMLcourse&lt;/a&gt; &lt;a href=&#34;http://t.co/tARm66Igrl&#34;&gt;pic.twitter.com/tARm66Igrl&lt;/a&gt;&lt;/p&gt;&amp;mdash; Prakhar Srivastav (@prakharsriv9) &lt;a href=&#34;https://twitter.com/prakharsriv9/status/625441171985297408&#34;&gt;July 26, 2015&lt;/a&gt;&lt;/blockquote&gt;
&lt;script async src=&#34;//platform.twitter.com/widgets.js&#34; charset=&#34;utf-8&#34;&gt;&lt;/script&gt;&lt;/p&gt;

&lt;p&gt;On the negative side, the datasets used in the assignments are rather small and in few assignments the model is only run against the test and validation data set to test for accuracy i.e. the model is not used to make predictions on new data (a minor but important point).&lt;/p&gt;

&lt;h3 id=&#34;scalable&#34;&gt;Scalable?&lt;/h3&gt;

&lt;p&gt;How does this course teach the &lt;em&gt;scalable&lt;/em&gt; aspect of machine learning? For one, the fact that you&amp;rsquo;re using Spark &lt;a href=&#34;http://www.thecloudavenue.com/2014/01/resilient-distributed-datasets-rdd.html&#34;&gt;RDDs&lt;/a&gt; to run on your workers itself gives you ability to run your code on a cluster for free of cost. The other aspect is that the professor sets aside a couple of videos each week to discuss cases where both &lt;code&gt;N&lt;/code&gt;(the number of data points) and &lt;code&gt;D&lt;/code&gt;(the number of features) are large - a key characteristic of big data.&lt;/p&gt;

&lt;p&gt;Unfortunately, none of these techniques are used in the programming assignments so it is quite possible that these lessons might not stick with you for long. Having said that, the treatment of these topics in the lectures is relatively interesting and sufficiently detailed.&lt;/p&gt;

&lt;h3 id=&#34;conclusion&#34;&gt;Conclusion&lt;/h3&gt;

&lt;p&gt;In summary, CS 190.1x is a reasonably good introduction to Machine Learning. If you&amp;rsquo;ve never done any ML before, then this course will surely whet your appetite and encourage you to dive deeper into other popular algorithms. If you&amp;rsquo;re reasonably proficient with ML, then this course will serve to be a primer to Spark&amp;rsquo;s MLlib library - although the low coverage might leave you a bit disappointed.&lt;/p&gt;

&lt;p&gt;The instructors indicated that they might come out with a second version of course that will cover more algorithms in detail. I, for one, will surely be waiting for more info on that one. Overall, in my opinion, time spent doing this course is time well spent.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Week 10 - Surveyman</title>
      <link>https://prakhar.me/articles/surveyman-week10/</link>
      <pubDate>Wed, 01 Jul 2015 15:50:49 +0300</pubDate>
      
      <guid>https://prakhar.me/articles/surveyman-week10/</guid>
      <description>&lt;p&gt;After a fun and productive two weeks at home, I have now rejoined office back in Kuwait. As all good things in life, the vacation has come to an end and it&amp;rsquo;s now time to head back to the usual humdrum of 9 to 5. What also didn&amp;rsquo;t help was the fact that since most of easy (low hanging fruits, if you will) features had already been built, I had no option but to start work on the feature which I most dreaded - &lt;a href=&#34;https://github.com/prakhar1989/react-surveyman/issues/3&#34;&gt;implementing sortable on blocks / questions&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The primary idea behind this feature is to allow users to reorder questions and blocks in the survey. Instead of manipulating the survey directly (which can be arbitrarily long) the user interacts with the minimap (or the treeview) and the changes are reflected in the survey when the drag-and-drop is over.&lt;/p&gt;

&lt;p&gt;Since this feature was particularly hard to tackle I decide to chalk out a rough plan on what are the &lt;em&gt;sub-problems&lt;/em&gt; that I need to solve in order to ship this feature. The &lt;a href=&#34;https://github.com/prakhar1989/react-surveyman/issues/3&#34;&gt;github issue&lt;/a&gt; lays this out quite clearly. To begin with, I thought long and hard on what this feature and why this is required. I then broke this down into tasks or &lt;code&gt;todos&lt;/code&gt; which I could tackle one by one. This was done primarily so that I stop feeling overwhelmed and just concentrate on the task at hand.&lt;/p&gt;

&lt;p&gt;I&amp;rsquo;ve been tagging commits very religiously in this project so the best way to understand the code would be to checkout the commits tagged in the issue above. Piece by piece it&amp;rsquo;ll start to make sense. In the remaining part of this blog post I quickly touch upon two key things that I struggled with and how I found a solution.&lt;/p&gt;

&lt;p&gt;The first issue that I spent quite a long time breaking my head had to do with drag-and-drop. The initial drag and drop that I implemented was not behaving at all like the &lt;a href=&#34;http://gaearon.github.io/react-dnd/examples-sortable-simple.html&#34;&gt;version&lt;/a&gt; done by Dan Abramov. However, after comparing the code and spending hours seeing the DOM behavior the issue seemed to be with non-index keys passed to the React component because of which React was unable to reconcile the virtual DOM.&lt;/p&gt;

&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-javascript&#34; data-lang=&#34;javascript&#34;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&#34;c1&#34;&gt;// changing the value of key from index i to&lt;/span&gt;
&lt;span class=&#34;o&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;BlockNode&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;key&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;i&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;id&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;block&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;get&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&amp;#39;id&amp;#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)}&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;gt;&lt;/span&gt;

&lt;span class=&#34;c1&#34;&gt;// below&lt;/span&gt;
&lt;span class=&#34;o&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;BlockNode&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;key&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;block&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;get&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&amp;#39;id&amp;#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)}&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;id&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;block&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;get&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&amp;#39;id&amp;#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)}&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;The second issue was with implementing the state propagation where the idea was to allow the treeview to be manipulated and &lt;em&gt;when&lt;/em&gt; the drag-and-drop operation finished &lt;em&gt;then&lt;/em&gt; propogate and update the actual survey. Since the treeview recieved the json survey as a &lt;code&gt;prop&lt;/code&gt; I had to create a replica of it in the local &lt;code&gt;state&lt;/code&gt; and let the component re-render when the props changed. After looking around the documentation I found that there&amp;rsquo;s indeed a lifecycle method that takes care of this  -&lt;/p&gt;

&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-javascript&#34; data-lang=&#34;javascript&#34;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;TreeView&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;React&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;createClass&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;({&lt;/span&gt;
    &lt;span class=&#34;nx&#34;&gt;propTypes&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
        &lt;span class=&#34;nx&#34;&gt;survey&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;React&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;PropTypes&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;instanceOf&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;List&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
    &lt;span class=&#34;p&#34;&gt;},&lt;/span&gt;
    &lt;span class=&#34;nx&#34;&gt;getInitialState&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
        &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
            &lt;span class=&#34;nx&#34;&gt;survey&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;this&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;props&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;survey&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;
            &lt;span class=&#34;nx&#34;&gt;finalIndex&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;-&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;
        &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
    &lt;span class=&#34;p&#34;&gt;},&lt;/span&gt;
    &lt;span class=&#34;nx&#34;&gt;componentWillReceiveProps&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;nextProps&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
        &lt;span class=&#34;k&#34;&gt;this&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;setState&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;({&lt;/span&gt;
            &lt;span class=&#34;nx&#34;&gt;survey&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;nextProps&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;survey&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;
            &lt;span class=&#34;nx&#34;&gt;finalIndex&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;-&lt;/span&gt;&lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;
        &lt;span class=&#34;p&#34;&gt;});&lt;/span&gt;
    &lt;span class=&#34;p&#34;&gt;},&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;...&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;The &lt;code&gt;finalIndex&lt;/code&gt; above is used to keep track of the final location of where the item is dropped. This value is then sent to the &lt;code&gt;store&lt;/code&gt; so that it can make the necessary changes. Below is the code for reordering &lt;code&gt;Blocks&lt;/code&gt; for example -&lt;/p&gt;

&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-javascript&#34; data-lang=&#34;javascript&#34;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;reorderBlock&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;draggedBlockId&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;overBlockId&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
    &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;survey&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;this&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;state&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;survey&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;
    &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;draggedBlockIndex&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;survey&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;findIndex&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;b&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;b&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;get&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&amp;#39;id&amp;#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;===&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;draggedBlockId&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;);&lt;/span&gt;
    &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;overBlockIndex&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;survey&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;findIndex&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;b&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;b&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;get&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&amp;#39;id&amp;#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;===&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;overBlockId&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;);&lt;/span&gt;

    &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;block&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;survey&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;get&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;draggedBlockIndex&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;);&lt;/span&gt;
    &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;newSurvey&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;survey&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;k&#34;&gt;delete&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;draggedBlockIndex&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;).&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;splice&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;overBlockIndex&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;block&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;);&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;this&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;setState&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;({&lt;/span&gt;
        &lt;span class=&#34;nx&#34;&gt;survey&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;newSurvey&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;
        &lt;span class=&#34;nx&#34;&gt;finalIndex&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;overBlockIndex&lt;/span&gt;
    &lt;span class=&#34;p&#34;&gt;});&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;So with that feature shipped the drag-and-drop sortable is now complete. Go and &lt;a href=&#34;http://prakhar.me/react-surveyman&#34;&gt;try it out&lt;/a&gt;!&lt;/p&gt;

&lt;p&gt;Until next time!&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Intro to Big Data with Apache Spark</title>
      <link>https://prakhar.me/articles/big-data-with-spark/</link>
      <pubDate>Sat, 27 Jun 2015 20:33:29 +0300</pubDate>
      
      <guid>https://prakhar.me/articles/big-data-with-spark/</guid>
      <description>

&lt;p&gt;Last night I finished the final assignment for the new course that I had been working on in the past week called &lt;a href=&#34;https://www.edx.org/course/introduction-big-data-apache-spark-uc-berkeleyx-cs100-1x&#34;&gt;Intro to Big Data with Apache Spark or CS100.1 x&lt;/a&gt;. With the course over, I decided to write down a quick review in the hope that it will help others get an idea of what they can expect by enrolling in this popular &lt;a href=&#34;https://en.wikipedia.org/wiki/Massive_open_online_course&#34;&gt;MOOC&lt;/a&gt; by UC Berkeley.&lt;/p&gt;

&lt;p&gt;I heard about the course after stumbling upon a laudatory &lt;a href=&#34;https://news.ycombinator.com/item?id=9702803&#34;&gt;review&lt;/a&gt; by an existing student on Hacker News. After learning that the course involves programming assignments that teach you the Spark API, I couldn&amp;rsquo;t help signing up.&lt;/p&gt;

&lt;p&gt;TL;DR - If you&amp;rsquo;re a programmer who wants to explore what the hype on Spark is all about and considers solving &lt;a href=&#34;http://codekata.com/&#34;&gt;code katas&lt;/a&gt; a great way to spend your evenings, then take this course - you will not be disappointed.&lt;/p&gt;

&lt;h3 id=&#34;apache-spark&#34;&gt;Apache Spark&lt;/h3&gt;

&lt;figure&gt;
    &lt;img data-action=&#34;zoom&#34; src=&#34;https://prakhar.me/images/spark.png&#34;&gt;&lt;/img&gt;
&lt;/figure&gt;

&lt;p&gt;So what is &lt;a href=&#34;https://spark.apache.org&#34;&gt;Apache Spark&lt;/a&gt;? Spark is a fast and general-purpose cluster computing system that is used for large-scale data processing. The project was developed by the &lt;a href=&#34;https://amplab.cs.berkeley.edu/&#34;&gt;AMPlab&lt;/a&gt; at UC Berkeley and has now evolved into a standalone company - &lt;a href=&#34;https://databricks.com/&#34;&gt;Databricks&lt;/a&gt; that, among other things, provides enterprise consulting and training for Apache Spark.&lt;/p&gt;

&lt;p&gt;Why should you care? Well, if you&amp;rsquo;re working as a data scientist or a data engineer you&amp;rsquo;re no stranger to the monumental growth of Spark in the bigdata field. Spark has been taking the big-data world by storm after showing consistent benchmarks that boast of it being upto 100x faster than Hadoop. Late last year, Spark also made waves by setting the &lt;a href=&#34;https://databricks.com/blog/2014/11/05/spark-officially-sets-a-new-record-in-large-scale-sorting.html&#34;&gt;world record&lt;/a&gt; for large-scale on-disk sorting.&lt;/p&gt;

&lt;p&gt;In order to evangelize Spark and help developers / data scientists learn about it, Databricks and UC Berkeley teamed up to start a Big Data &lt;a href=&#34;https://www.edx.org/xseries&#34;&gt;Xseries&lt;/a&gt; on EdX that consists of two MOOCs - CS 100.1x followed by Scalable Machine Learning.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The Big Data XSeries, created in partnership with Databricks, will articulate the expected output of Data Scientists and then teach students how to use PySpark (part of Apache Spark) to deliver against these expectations.  Students will gain hands-on experience applying big data principles using Apache Spark, a cluster computing system well-suited for large-scale machine learning tasks.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;h3 id=&#34;pedagogy&#34;&gt;Pedagogy&lt;/h3&gt;

&lt;p&gt;With the &lt;em&gt;elevator pitch&lt;/em&gt; out of the way, let&amp;rsquo;s get back to the review. The entire course spans 5 weeks (including week 0 which dealt only with the setup) with the material consisting of lecture videos, short quizzes and programming assignments. Unlike the typical technical MOOCs that I&amp;rsquo;ve done in the past, there are no exams and surprisingly the last week has no lecture videos to watch.&lt;/p&gt;

&lt;p&gt;The lecture videos, in my opinion, were pretty light in content. Even on a lethargic evening, it took me roughly somewhere around 30 mins to breeze through the videos and answer the quizzes. The first week started off with talking about Apache Spark and how it is different from Hadoop. The two subsequent weeks focused on issues with data management and talked about how a typical big data processing pipeline looks like. The penultimate video in week four spent roughly 5 mins talking about &lt;a href=&#34;https://en.wikipedia.org/wiki/Collaborative_filtering&#34;&gt;Collaborative Filtering&lt;/a&gt; and &lt;a href=&#34;https://spark.apache.org/docs/1.2.1/mllib-guide.html&#34;&gt;Spark MLib&lt;/a&gt; which somehow seemed enough to the teaching staff to justify a programming assignment on the topic.&lt;/p&gt;

&lt;p&gt;As far as forums and class interactions go, the instructors are quite active on the forums and the staff is quick to sort out any hiccups with the autograder. I got stuck a couple of times but was quickly able to find a way by searching for previous threads in the forums.&lt;/p&gt;

&lt;h3 id=&#34;programming-assignments&#34;&gt;Programming Assignments&lt;/h3&gt;

&lt;p&gt;Coming to my initial motivation for taking this course, if are you wondering whether this course delivered on its promise on teaching Spark then the answer is a resounding yes! The programming assignments were exceptionally well thought-out and interesting. Each week&amp;rsquo;s lab &lt;a href=&#34;https://github.com/spark-mooc/mooc-setup/blob/master/lab0_student.ipynb&#34;&gt;exercise&lt;/a&gt; was presented as an &lt;a href=&#34;http://ipython.org/notebook.html&#34;&gt;IPython notebook&lt;/a&gt; which provided the ideal environment for interacting with and exploring the Spark&amp;rsquo;s Python API.
(As an aside, if you haven&amp;rsquo;t checked out IPython notebooks yet, please do yourselves a favor and give it a go.)&lt;/p&gt;

&lt;p&gt;What the course lacked in the lectures was more than compensated for in the extremely detailed and well presented notebooks. The instructors leveraged the entire set of IPython capabilities wonderfully as the notebooks were interspersed with diagrams, LaTeX equations and even plots courtesy of &lt;a href=&#34;http://matplotlib.org/&#34;&gt;Matplotlib&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;The typical structure followed in the programming assignments was&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;Introduce a topic with theory and examples&lt;/li&gt;
&lt;li&gt;Give a small coding exercise on solving the problem&lt;/li&gt;
&lt;li&gt;Run your code against the test cases provided&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Rather than having a big problem statement for the students to solve in one go, the bite-sized problems and test cases provided instant feedback and the necessary encouragement to keep forging ahead. Lastly, much to the project&amp;rsquo;s credit the &lt;a href=&#34;https://spark.apache.org/docs/latest/api/python/pyspark.html&#34;&gt;Spark documentation&lt;/a&gt; was well written which made the process of finding a function that does X to using it quite gratifying.&lt;/p&gt;

&lt;p&gt;Even though Spark supports variety of languages R, Spark SQL, Java, Python and Scala, the instructors choice to go ahead with Python and IPython clearly shows how powerful of a teaching tool it can be.&lt;/p&gt;

&lt;h3 id=&#34;setup&#34;&gt;Setup&lt;/h3&gt;

&lt;p&gt;Before jumping on to the first assignment, I was reasonably certain that setting up a &lt;strong&gt;cluster computing system&lt;/strong&gt; was going to be a big pain - even on a single machine. The courseware&amp;rsquo;s long setup document and set of 5 videos also corroborated my fears. What turned out to be delight though that it took me less than 10mins to get the entire setup running.&lt;/p&gt;

&lt;p&gt;Showing great wisdom on their part, the course designers made the process extremely straightforward by providing a custom &lt;a href=&#34;https://www.vagrantup.com/&#34;&gt;Vagrant&lt;/a&gt; image which had everything installed. Since I use Vagrant on a daily basis, going from &lt;em&gt;never running Spark&lt;/em&gt; to a &lt;em&gt;functioning Spark environment&lt;/em&gt; was just a matter of few commands.&lt;/p&gt;

&lt;pre&gt;&lt;code&gt;$ wget &amp;lt;course_materails_url&amp;gt;
$ cd bigdata_course
$ vagrant up
# 5 mins later ... IPython is running on 8001
&lt;/code&gt;&lt;/pre&gt;

&lt;p&gt;The VM also came bundled with all the required IPython notebooks and data associated with the labs which meant that you needed to setup everything just once.&lt;/p&gt;

&lt;h3 id=&#34;conclusion&#34;&gt;Conclusion&lt;/h3&gt;

&lt;p&gt;CS 100.1x is probably the course where I&amp;rsquo;ve spent the least amount of time and had the most amount of fun (Steve Huffman&amp;rsquo;s web dev &lt;a href=&#34;https://www.udacity.com/course/web-development--cs253&#34;&gt;course&lt;/a&gt; comes a close second). I started a week later and within 3 weekends I was able to finish the material. While that could be attributed to my prior experience with machine learning and functional programming, from what I could see in the forums students were able to finish the assignments within reasonable time.&lt;/p&gt;

&lt;p&gt;What this goes to show is that the material is light, focused and can be done with quickly if you find it interesting - much like any other crash course. For someone who&amp;rsquo;s curious to understand how Spark works and wants to dip their toes in the water, I doubt there can be a anything more fruitful than spending a couple of weekends working through the programming assignments.&lt;/p&gt;

&lt;p&gt;Overall, I had a great time learning about the Spark computation model, its API and how it can be efficiently used to process large amounts of data. Although the path from completing this course to setting up a data processing pipeline on a cluster for your company is a tough one, I feel that the material provided me enough tools to go and explore further.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Week 9 - Surveyman</title>
      <link>https://prakhar.me/articles/surveyman-week9/</link>
      <pubDate>Sun, 21 Jun 2015 15:50:49 +0300</pubDate>
      
      <guid>https://prakhar.me/articles/surveyman-week9/</guid>
      <description>

&lt;p&gt;This is my last week at home (India) and the fact that I had a pretty productive week shows that I&amp;rsquo;m ensconced within the comforts of my home. After the hectic last week which involved traveling, meeting relatives and getting documentation ready for the Visa interview this week was just me sitting at home working on this project without any distractions.&lt;/p&gt;

&lt;p&gt;So there were three key features that I tackled this week, namely - TreeView, Option Lists and React transitions. Cumulatively, these changed the UI considerably. The app has finally started to look much more polished.&lt;/p&gt;

&lt;h3 id=&#34;treeview&#34;&gt;TreeView&lt;/h3&gt;

&lt;figure&gt;
    &lt;img data-action=&#34;zoom&#34; src=&#34;https://prakhar.me/images/treeview.png&#34;&gt;&lt;/img&gt;
    &lt;figcaption&gt;TreeView and OptionList in action&lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;TreeView makes it easy for users to navigate the survey easily. The idea is inspired by Sublime Text&amp;rsquo;s minimap that allows you to conveniently navigate the file. Having the ability to collapse and expand blocks and questions will be useful in getting a particular item in focus just by clicking on it.&lt;/p&gt;

&lt;p&gt;To get started with the feature, I surveyed the landscape to check if there already existing a customizable TreeView component. After checking out a few examples on &lt;a href=&#34;http://react.rocks&#34;&gt;react.rocks&lt;/a&gt; I didn&amp;rsquo;t find anything that met my requirements so I decided to build my own. More importantly, since the structure of the &lt;code&gt;JSON&lt;/code&gt; was simple building a tree view didn&amp;rsquo;t seem too time consuming.&lt;/p&gt;

&lt;p&gt;Before I started writing any code, I read through Cheng Lou&amp;rsquo;s &lt;a href=&#34;https://github.com/chenglou/react-treeview&#34;&gt;React Treeview&lt;/a&gt; to get an idea of how to approach the problem. So at the end, I just ended up creating two new components - one called &lt;code&gt;TreeNode&lt;/code&gt; and another called &lt;code&gt;TreeView&lt;/code&gt;. This is how the &lt;a href=&#34;https://github.com/prakhar1989/react-surveyman/blob/5e95b8dccb50324b75f3d4cf6c64d759eaa1dea6/js/components/TreeView.js&#34;&gt;render function&lt;/a&gt; of &lt;code&gt;TreeView&lt;/code&gt; looked like.&lt;/p&gt;

&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-javascript&#34; data-lang=&#34;javascript&#34;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;TreeView&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;React&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;createClass&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;({&lt;/span&gt;
  &lt;span class=&#34;nx&#34;&gt;render&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;kd&#34;&gt;function&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;
        &lt;span class=&#34;o&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;TreeNode&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;key&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;i&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
              &lt;span class=&#34;nx&#34;&gt;label&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;blockLabel&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
              &lt;span class=&#34;nx&#34;&gt;type&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;ItemTypes&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;BLOCK&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
              &lt;span class=&#34;nx&#34;&gt;handleClick&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;focusOnItem&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;bind&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;k&#34;&gt;this&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;block&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;get&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&amp;#39;id&amp;#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;))}&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;gt;&lt;/span&gt;

                 &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;questions&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;map&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;((&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;ques&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;j&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
                     &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;
                         &lt;span class=&#34;o&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;TreeNode&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;key&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;j&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
                             &lt;span class=&#34;nx&#34;&gt;label&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;label&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
                             &lt;span class=&#34;nx&#34;&gt;type&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;ItemTypes&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;QUESTION&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
                             &lt;span class=&#34;nx&#34;&gt;handleClick&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;focusOnItem&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;bind&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;k&#34;&gt;this&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;ques&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;get&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&amp;#39;id&amp;#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;))}&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;/&amp;gt;&lt;/span&gt;
                     &lt;span class=&#34;p&#34;&gt;)})}&lt;/span&gt;
        &lt;span class=&#34;o&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;err&#34;&gt;/TreeNode&amp;gt;&lt;/span&gt;
      &lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
    &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
  &lt;span class=&#34;p&#34;&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;To add the additional functionality of bringing the object in focus just required a simple change to &lt;code&gt;window.location&lt;/code&gt; like so -&lt;/p&gt;

&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-javascript&#34; data-lang=&#34;javascript&#34;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&#34;c1&#34;&gt;// inside the TreeView component&lt;/span&gt;
&lt;span class=&#34;nx&#34;&gt;focusOnItem&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;id&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
       &lt;span class=&#34;nb&#34;&gt;window&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;location&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;hash&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;id&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;h3 id=&#34;option-lists&#34;&gt;Option Lists&lt;/h3&gt;

&lt;p&gt;If you&amp;rsquo;ve ever built a survey before, you know that most of the questions will end up having the same set of options. For example &lt;a href=&#34;https://en.wikipedia.org/wiki/Likert_scale&#34;&gt;likert scales&lt;/a&gt; are very common, especially in market research surveys. Having to type all the options multiple times can get frustrating and hence providing an easy way to reuse a common set of options would be much better.&lt;/p&gt;

&lt;p&gt;That is where OptionLists come in. Options List make it very convenient to re-use an existing set of common options or add your own. In order to use them, you just to drag and drop them on your question.&lt;/p&gt;

&lt;p&gt;Technically, option lists were quite simple to build. At the &lt;code&gt;Store&lt;/code&gt; I maintain an array of option lists which can later be persisted in &lt;a href=&#34;http://diveintohtml5.info/storage.html&#34;&gt;LocalStorage&lt;/a&gt;. When the user selects an option list from the dropdown I remember that state in the &lt;code&gt;Store&lt;/code&gt;. Lastly, when an &lt;code&gt;DraggableOptionList&lt;/code&gt; is dropped on a &lt;code&gt;Question&lt;/code&gt; I read the index from the &lt;code&gt;Store&lt;/code&gt; and add the options to the questions. It&amp;rsquo;s just that simple.&lt;/p&gt;

&lt;h3 id=&#34;transitions&#34;&gt;Transitions&lt;/h3&gt;

&lt;p&gt;The last improvement that had been on my mind for a while was to add transitions in the application. If you interact with the items for example - try deleting an item you&amp;rsquo;ll notice that it just disappears out of the survey. The result can feel quite jarring. What I wanted to do was find a way to add subtle animations so that the UI feels much more pleasant.&lt;/p&gt;

&lt;p&gt;Unsurprisingly, for transitions I used &lt;code&gt;ReactCSSTransitionGroup&lt;/code&gt; which is explained quite clearly in the &lt;a href=&#34;https://facebook.github.io/react/docs/animation.html&#34;&gt;documentation&lt;/a&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;&lt;code&gt;ReactCSSTransitionGroup&lt;/code&gt; is based on &lt;code&gt;ReactTransitionGroup&lt;/code&gt; and is an easy way to perform CSS transitions and animations when a React component enters or leaves the DOM. It&amp;rsquo;s inspired by the excellent ng-animate library.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Basically, you just wrap your container in &lt;code&gt;ReactCSSTransitionGroup&lt;/code&gt; and provide the CSS class that you want to apply on entry and exit of any child component.&lt;/p&gt;

&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-javascript&#34; data-lang=&#34;javascript&#34;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;ReactCSSTransitionGroup&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;transitionName&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&amp;quot;itemTransition&amp;quot;&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;transitionEnter&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;kc&#34;&gt;false&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;gt;&lt;/span&gt;
    &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;blocks&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;span class=&#34;o&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;err&#34;&gt;/ReactCSSTransitionGroup&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;The best part of building this was when I learnt that there exists a CSS pseudo class called &lt;a href=&#34;https://developer.mozilla.org/en-US/docs/Web/CSS/%3Atarget&#34;&gt;&lt;code&gt;target&lt;/code&gt;&lt;/a&gt;.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;The :target pseudo-class represents the unique element, if any, with an id matching the fragment identifier of the URI of the document.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;So you want to animate a subtle effect when the browser focuses on an item? Try this -&lt;/p&gt;

&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-css&#34; data-lang=&#34;css&#34;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt;&lt;span class=&#34;nd&#34;&gt;target&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
  &lt;span class=&#34;k&#34;&gt;animation&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;n&#34;&gt;highlight&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;kt&#34;&gt;s&lt;/span&gt; &lt;span class=&#34;kc&#34;&gt;ease-in&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;@&lt;/span&gt;&lt;span class=&#34;k&#34;&gt;keyframes&lt;/span&gt; &lt;span class=&#34;nt&#34;&gt;highlight&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
  &lt;span class=&#34;nt&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;%&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;background-color&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;mh&#34;&gt;#fcf1bb&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
  &lt;span class=&#34;nt&#34;&gt;100&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;%&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;background-color&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;kc&#34;&gt;white&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Wicked cool, right?&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Week 8 - Surveyman</title>
      <link>https://prakhar.me/articles/surveyman-week8/</link>
      <pubDate>Mon, 15 Jun 2015 15:50:49 +0300</pubDate>
      
      <guid>https://prakhar.me/articles/surveyman-week8/</guid>
      <description>&lt;p&gt;If you&amp;rsquo;ve read the previous blog post, you would be aware that I&amp;rsquo;m right now back to my home country on a &lt;em&gt;vacation&lt;/em&gt;. This week has been quite hectic and filled with anxiety owing to the visa interview that I had scheduled during the start of the week. Since this was my first time appearing for a US Visa interview, I was quite in awe of how the smooth and planned the entire process was, especially compared to the other interview processes I&amp;rsquo;d been in (I&amp;rsquo;m looking at you Kuwait). Thankfully, my visa got approved and now I just can&amp;rsquo;t wait for grad school to start!&lt;/p&gt;

&lt;p&gt;So circling back to Surveyman, this week, I deployed a relatively straight forward but important &lt;a href=&#34;https://github.com/prakhar1989/react-surveyman/issues/21&#34;&gt;feature&lt;/a&gt; that allows users to clone or duplicate existing blocks / questions. While interacting with the survey I constantly felt that need to build similar blocks. For example, if you have a block having 3 questions and the next block that you want to add has the same number of questions in a similar configuration, building it incrementally can be exhausting.&lt;/p&gt;

&lt;p&gt;In cases like these, which I am hoping would be quite common, a simple click that clones the entire block or question would be helpful. That was the primary motivation for building this feature. As far as writing the code goes, this feature was quite simple (at least in theory). The idea was simply deep clone the question or block object and update the survey &lt;code&gt;JSON&lt;/code&gt; when required. What you would also need to ensure is to keep the two maps i.e. &lt;code&gt;OptionMap&lt;/code&gt; and &lt;code&gt;QuestionMap&lt;/code&gt; aware of the new items so that the mappings are in sync with the app state&lt;/p&gt;

&lt;p&gt;The most fun I had while building this feature was working with the Immutable API. Having a brief stint with ClojureScript before, the idea of returning new collections after every mutable operation and subsequently then chaining operations was not new to me. This is how, for example, the code for cloning the &lt;code&gt;Question&lt;/code&gt; looked like -&lt;/p&gt;

&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-javascript&#34; data-lang=&#34;javascript&#34;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&#34;cm&#34;&gt;/**&lt;/span&gt;
&lt;span class=&#34;cm&#34;&gt; * Returns a clone of Question with new ID of itself&lt;/span&gt;
&lt;span class=&#34;cm&#34;&gt; * and all options.&lt;/span&gt;
&lt;span class=&#34;cm&#34;&gt; * @param question - type of Immutable.Map. The question to be cloned&lt;/span&gt;
&lt;span class=&#34;cm&#34;&gt; */&lt;/span&gt;
&lt;span class=&#34;nx&#34;&gt;cloneQuestion&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;question&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
    &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;self&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;this&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;question&lt;/span&gt;
            &lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;set&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&amp;#39;id&amp;#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;getNewId&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;ItemTypes&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;QUESTION&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;))&lt;/span&gt; &lt;span class=&#34;c1&#34;&gt;// generate new ID&lt;/span&gt;
            &lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;update&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&amp;#39;options&amp;#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;list&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&amp;gt;&lt;/span&gt;                  &lt;span class=&#34;c1&#34;&gt;// do the same for all options&lt;/span&gt;
                        &lt;span class=&#34;nx&#34;&gt;list&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;map&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;
                            &lt;span class=&#34;nx&#34;&gt;o&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;Immutable&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;Map&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;({&lt;/span&gt;
                                &lt;span class=&#34;nx&#34;&gt;id&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;self&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;getNewId&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;ItemTypes&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;OPTION&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;),&lt;/span&gt;
                                &lt;span class=&#34;nx&#34;&gt;otext&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;o&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;get&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&amp;#39;otext&amp;#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
                            &lt;span class=&#34;p&#34;&gt;})&lt;/span&gt;
                        &lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
                    &lt;span class=&#34;p&#34;&gt;);&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;},&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;After getting the new &lt;code&gt;clonedQuestion&lt;/code&gt;, I just needed to append it to the parent container and update the relevant &lt;code&gt;OptionMap&lt;/code&gt; and &lt;code&gt;QuestionMap&lt;/code&gt;.&lt;/p&gt;

&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-javascript&#34; data-lang=&#34;javascript&#34;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;newQuestion&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;this&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;cloneQuestion&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;question&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;);&lt;/span&gt;
&lt;span class=&#34;kd&#34;&gt;let&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;newSurvey&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;survey&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;updateIn&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;([&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;blockIndex&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;s1&#34;&gt;&amp;#39;questions&amp;#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;],&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;list&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&amp;gt;&lt;/span&gt;
    &lt;span class=&#34;nx&#34;&gt;list&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;splice&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;questionIndex&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;+&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;1&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;0&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;newQuestion&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;);&lt;/span&gt;
&lt;span class=&#34;c1&#34;&gt;// update the maps&lt;/span&gt;
&lt;span class=&#34;nx&#34;&gt;_questionMap&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;_questionMap&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;set&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;qId&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;block&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;get&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&amp;#39;id&amp;#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;));&lt;/span&gt;
&lt;span class=&#34;nx&#34;&gt;newQuestion&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;get&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&amp;#39;options&amp;#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;).&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;forEach&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;o&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&amp;gt;&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
    &lt;span class=&#34;nx&#34;&gt;_optionMap&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;_optionMap&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;set&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;o&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;get&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&amp;#39;id&amp;#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;),&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;qId&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Similarly, the code for adding blocks was also pretty simple. However, since it was one level up the chain what required was all child questions should be also be updated with the mappings. If you&amp;rsquo;re looking at diving deeper, feel free to browse through the &lt;a href=&#34;https://github.com/prakhar1989/react-surveyman/issues/21&#34;&gt;commits&lt;/a&gt; tagged with this issue.&lt;/p&gt;

&lt;p&gt;Until next time!&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Week 7 - Surveyman</title>
      <link>https://prakhar.me/articles/surveyman-week7/</link>
      <pubDate>Mon, 08 Jun 2015 15:50:49 +0300</pubDate>
      
      <guid>https://prakhar.me/articles/surveyman-week7/</guid>
      <description>&lt;p&gt;Guess what, I&amp;rsquo;m writing this blog post from my home in Delhi! I&amp;rsquo;ll be in India for full 2 weeks where apart from visiting my family, cousins and friends I&amp;rsquo;ll also be giving my visa interview - which is the main reason why I&amp;rsquo;ve taken a vacation.&lt;/p&gt;

&lt;p&gt;As I had been traveling the progress this week has been rather minimal. Keeping the travel schedule in mind, I had kept the goal to be reasonable which was upgrading the react-surveyman project to use React-DnD&amp;rsquo;s latest 1.x version.&lt;/p&gt;

&lt;p&gt;If you&amp;rsquo;ve been following the progress of the project closely then you&amp;rsquo;re probably aware the that &lt;a href=&#34;https://github.com/gaearon/react-dnd&#34;&gt;React-DnD&lt;/a&gt; is the drag-and-drop library for React that I use in my projects. Authored by the super productive and super smart, &lt;a href=&#34;https://twitter.com/dan_abramov&#34;&gt;Dan Abramov&lt;/a&gt;, React DND abstracts out the various HTML5 drag-and-drop events and gives you a nice way to customize the drag and drop behavior of your React components.&lt;/p&gt;

&lt;p&gt;Earlier last month, Dan published the 1.0 release of the library where he made considerable changes in the API. Keeping in the mind the best practices in the React world, the most significant change was replacing mixins with decorators. To understand the motivation behind this change, you can read the &lt;a href=&#34;https://medium.com/@dan_abramov/mixins-are-dead-long-live-higher-order-components-94a0d2f9e750&#34;&gt;blog post&lt;/a&gt; written by the man himself.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;To be clear, mixins is an escape hatch to work around reusability limitations in the system. It’s not idiomatic React. Making composition easier is a higher priority than making arbitrary mixins work. I’ll focus on making composition easier so we can get rid of mixins. - Sebastian Markbage (React core team)&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;All in all, the change was quite straightforward, the credit for which goes to the excellent &lt;a href=&#34;http://gaearon.github.io/react-dnd/&#34;&gt;documentation&lt;/a&gt; on the website. Dan has worked really hard to make sure that his library is approachable and it clearly shows. The &lt;a href=&#34;http://gaearon.github.io/react-dnd/docs-tutorial.html&#34;&gt;tutorial&lt;/a&gt; itself, is so well explained that I&amp;rsquo;d go to the extent to say that it&amp;rsquo;s one of the best React tutorials out there.&lt;/p&gt;

&lt;p&gt;&lt;blockquote class=&#34;twitter-tweet tw-align-center&#34; lang=&#34;en&#34;&gt;&lt;p lang=&#34;en&#34; dir=&#34;ltr&#34;&gt;Attention library authors: &lt;a href=&#34;https://twitter.com/dan_abramov&#34;&gt;@dan_abramov&lt;/a&gt;&amp;#39;s tutorial on React-DND is the new gold standard for API documentation. &lt;a href=&#34;http://t.co/AeLugT5Iov&#34;&gt;http://t.co/AeLugT5Iov&lt;/a&gt;&lt;/p&gt;&amp;mdash; Prakhar Srivastav (@prakharsriv9) &lt;a href=&#34;https://twitter.com/prakharsriv9/status/606153755340382208&#34;&gt;June 3, 2015&lt;/a&gt;&lt;/blockquote&gt;
&lt;script async src=&#34;//platform.twitter.com/widgets.js&#34; charset=&#34;utf-8&#34;&gt;&lt;/script&gt;&lt;/p&gt;

&lt;p&gt;So after working through the tutorial and the browsing thorough the examples, I was able to successfully port both react-surveyman and the &lt;a href=&#34;http://github.com/prakhar1989/react-tags&#34;&gt;react-tags&lt;/a&gt; project to use the latest React-DnD library.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Week 6 - Surveyman</title>
      <link>https://prakhar.me/articles/surveyman-week6/</link>
      <pubDate>Mon, 01 Jun 2015 15:50:49 +0300</pubDate>
      
      <guid>https://prakhar.me/articles/surveyman-week6/</guid>
      <description>

&lt;p&gt;So I started this week with the primary goal of re-writing my stores and migrating them to use Immutable collections. In the last week&amp;rsquo;s post, I laid out the motivation on why this is a good idea and how it will speed up the React application.&lt;/p&gt;

&lt;p&gt;While the benefits were clear, I was still not sure on how I could implement and exploit the collections provided by &lt;a href=&#34;http://facebook.github.io/immutable-js/&#34;&gt;Immutable&lt;/a&gt; to my advantage. After numerous posts on &lt;a href=&#34;https://discuss.reactjs.org/t/benefits-of-immutable-js/435&#34;&gt;forums&lt;/a&gt;, &lt;a href=&#34;https://github.com/jurassix/react-immutable-render-mixin/issues/6&#34;&gt;Github issues&lt;/a&gt; and IRC discussions I realized that I was looking at it from the wrong direction.&lt;/p&gt;

&lt;p&gt;The key idea was explained to by &lt;a href=&#34;http://www.mattgreer.org/&#34;&gt;Matt Greer&lt;/a&gt; whose extremely long and clear email helped me really understand the issue. With that out of the way, slowly and steadily I was able to migrate the store piece by piece to Immutable.&lt;/p&gt;

&lt;p&gt;Initially it was a bit frustrating to keep updating nested structures using &lt;code&gt;set&lt;/code&gt; but after finding the &lt;code&gt;updateIn&lt;/code&gt; method, things got really good.&lt;/p&gt;

&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-javascript&#34; data-lang=&#34;javascript&#34;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&#34;c1&#34;&gt;// on question dropped&lt;/span&gt;
&lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;index&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;this&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;getBlockIndex&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;questionObj&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;parentID&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;);&lt;/span&gt;
&lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;newSurvey&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;this&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;data&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;surveyData&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;updateIn&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;([&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;index&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;s1&#34;&gt;&amp;#39;questions&amp;#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;],&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;list&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;=&amp;gt;&lt;/span&gt;
    &lt;span class=&#34;nx&#34;&gt;list&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;push&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;newQuestion&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;);&lt;/span&gt;
&lt;span class=&#34;k&#34;&gt;this&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;updateSurveyData&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;newSurvey&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;kc&#34;&gt;true&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;);&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;Throughout the store, I also keep track of two maps namely, &lt;code&gt;_optionMap&lt;/code&gt; and &lt;code&gt;_questionMap&lt;/code&gt; which are basically mappings from the option ID to the question ID and from the question ID to the block ID respectively.&lt;/p&gt;

&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-javascript&#34; data-lang=&#34;javascript&#34;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&#34;c1&#34;&gt;// on question dropped - update question map with new question&lt;/span&gt;
&lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;block&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;this&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;data&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;surveyData&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;get&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;index&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;);&lt;/span&gt;
&lt;span class=&#34;nx&#34;&gt;_questionMap&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;_questionMap&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;set&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;newQuestion&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;get&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&amp;#39;id&amp;#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;),&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;block&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;get&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&amp;#39;id&amp;#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;));&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;h3 id=&#34;adding-undo&#34;&gt;Adding Undo&lt;/h3&gt;

&lt;p&gt;After finishing up integrating Immutable and adding &lt;code&gt;PureRenderMixin&lt;/code&gt; in all my components, my next goal was to quickly add a &lt;code&gt;undo&lt;/code&gt; feature that would allow users to undo a destructive action.&lt;/p&gt;

&lt;p&gt;The idea here is simple - at each stage cache the application state in a &lt;code&gt;history&lt;/code&gt; object and use that to go back when the user clicks on &lt;code&gt;undo&lt;/code&gt;&lt;/p&gt;

&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-javascript&#34; data-lang=&#34;javascript&#34;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&#34;c1&#34;&gt;// mananging history&lt;/span&gt;
&lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;_history&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;[];&lt;/span&gt;

&lt;span class=&#34;c1&#34;&gt;// in the store&lt;/span&gt;
&lt;span class=&#34;nx&#34;&gt;updateSurveyData&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;data&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;cache&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;kc&#34;&gt;false&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;cache&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
        &lt;span class=&#34;nx&#34;&gt;_history&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;push&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;({&lt;/span&gt;
            &lt;span class=&#34;nx&#34;&gt;data&lt;/span&gt;        &lt;span class=&#34;o&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;this&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;data&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;surveyData&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt;
            &lt;span class=&#34;nx&#34;&gt;optionMap&lt;/span&gt;   &lt;span class=&#34;o&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;_optionMap&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;toJS&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(),&lt;/span&gt;
            &lt;span class=&#34;nx&#34;&gt;questionMap&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;_questionMap&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;toJS&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt;
        &lt;span class=&#34;p&#34;&gt;});&lt;/span&gt;
    &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;this&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;data&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;surveyData&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;data&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;this&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;trigger&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;k&#34;&gt;this&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;data&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;);&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;},&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;The other side benefit that this allowed me was to add an &lt;em&gt;undo&lt;/em&gt; feature which made sure users are allowed to go back any destructive action they carry out in the UI.
To make sure only &lt;strong&gt;destructive&lt;/strong&gt; actions are cached, the &lt;code&gt;updateSurveyData&lt;/code&gt; takes an extra &lt;code&gt;cache&lt;/code&gt; param which when &lt;code&gt;true&lt;/code&gt; pushes the data into the history. After this, the undo operation is simple to implement.&lt;/p&gt;

&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-javascript&#34; data-lang=&#34;javascript&#34;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;onUndoSurvey&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
    &lt;span class=&#34;c1&#34;&gt;// retrieve cached data&lt;/span&gt;
    &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;data&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;optionMap&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;questionMap&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;_history&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;pop&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;();&lt;/span&gt;
    &lt;span class=&#34;nx&#34;&gt;_questionMap&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;Immutable&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;Map&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;questionMap&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;);&lt;/span&gt;
    &lt;span class=&#34;nx&#34;&gt;_optionMap&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;Immutable&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;Map&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;optionMap&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;);&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;this&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;updateSurveyData&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;data&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;);&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;That finally sums up the work that went in this week to finish two key features. With the architecture now stable, I feel much more confident in adding more features without impacting the performance. However, now that the codebase has grown to roughly ~1500 lines of code, it is high time to add a test suite so that refactoring becomes more managable and less scary.&lt;/p&gt;

&lt;p&gt;The plan for this week, then, is to add tests and make the codebase more robust. Till next time.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Week 5 - Surveyman</title>
      <link>https://prakhar.me/articles/surveyman-week5/</link>
      <pubDate>Sun, 24 May 2015 15:50:49 +0300</pubDate>
      
      <guid>https://prakhar.me/articles/surveyman-week5/</guid>
      <description>&lt;p&gt;I signed off the last post mentioning that I&amp;rsquo;ll be integrating the React tags component into Surveyman which did not turn out to be difficult. As the API of the component was simple, it just meant adding the component and providing the necessary event handlers.&lt;/p&gt;

&lt;p&gt;The suggestions are loaded as soon as a &lt;code&gt;Question&lt;/code&gt; component is dropped, which after careful thought, sounds like a practical tradeoff. Suggestions are maintained as a &lt;a href=&#34;https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Set&#34;&gt;set&lt;/a&gt; and whenever is new &lt;code&gt;Option&lt;/code&gt; is added by the user, it gets appended to the set.&lt;/p&gt;

&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-javascript&#34; data-lang=&#34;javascript&#34;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&#34;c1&#34;&gt;// In the store:&lt;/span&gt;
&lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;_optionsSet&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;Immutable&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;OrderedSet&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;();&lt;/span&gt;

&lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;SurveyStore&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;Reflux&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;createStore&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;({&lt;/span&gt;
    &lt;span class=&#34;nx&#34;&gt;onOptionAdded&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;otext&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
        &lt;span class=&#34;c1&#34;&gt;// ..&lt;/span&gt;
        &lt;span class=&#34;nx&#34;&gt;_optionsSet&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;_optionsSet&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;add&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;otext&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;);&lt;/span&gt;
    &lt;span class=&#34;p&#34;&gt;},&lt;/span&gt;
    &lt;span class=&#34;nx&#34;&gt;getOptionsSet&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
        &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;_optionSet&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;
    &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;On the component side, the react tags live in an overlying &lt;code&gt;Options&lt;/code&gt; component.&lt;/p&gt;

&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-javascript&#34; data-lang=&#34;javascript&#34;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;Options&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;React&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;createClass&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;({&lt;/span&gt;
    &lt;span class=&#34;nx&#34;&gt;getInitialState&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
        &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
            &lt;span class=&#34;nx&#34;&gt;suggestions&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;[]&lt;/span&gt;
        &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
    &lt;span class=&#34;p&#34;&gt;},&lt;/span&gt;
    &lt;span class=&#34;nx&#34;&gt;componentDidMount&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
        &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;optionSet&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;SurveyStore&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;getOptionsSet&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;();&lt;/span&gt;
        &lt;span class=&#34;k&#34;&gt;this&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;setState&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;({&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;suggestions&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;Array&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;from&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;optionSet&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;});&lt;/span&gt;
    &lt;span class=&#34;p&#34;&gt;},&lt;/span&gt;
    &lt;span class=&#34;nx&#34;&gt;render&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
        &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;
            &lt;span class=&#34;o&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;ReactTags&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;tags&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;options&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
                &lt;span class=&#34;nx&#34;&gt;suggestions&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;k&#34;&gt;this&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;state&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;suggestions&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
                &lt;span class=&#34;nx&#34;&gt;handleAddition&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;k&#34;&gt;this&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;handleAddition&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
                &lt;span class=&#34;nx&#34;&gt;handleDelete&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;k&#34;&gt;this&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;handleDeletion&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
                &lt;span class=&#34;nx&#34;&gt;handleDrag&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;k&#34;&gt;this&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;handleDrag&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
                &lt;span class=&#34;nx&#34;&gt;labelField&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;s1&#34;&gt;&amp;#39;otext&amp;#39;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
                &lt;span class=&#34;nx&#34;&gt;placeholder&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&amp;quot;Add new option&amp;quot;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;/&amp;gt;&lt;/span&gt;
        &lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
    &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
    &lt;span class=&#34;c1&#34;&gt;// ...&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;You can view the final code &lt;a href=&#34;https://github.com/prakhar1989/react-surveyman/blob/daf4a192c952c2253634b49f015b872ba0dfa17c/js/components/Options.js&#34;&gt;here&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;After finishing up this by mid week, I was deciding on what issue to work on next. Although I should have run tests to prove this but it felt that rendering the entire application whenever a new option was added or removed was leading to performance bottlenecks. Since most of the data remainded unchanged most of the re-renders were useless and could be pre-emptively cancelled by the handly lifecycle function - &lt;code&gt;shouldComponentUpdate&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;On reading up further, I found that React already provides a &lt;a href=&#34;https://facebook.github.io/react/docs/pure-render-mixin.html&#34;&gt;mixin&lt;/a&gt; called &lt;code&gt;PureRenderMixin&lt;/code&gt; that does exactly does.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Under the hood, the mixin implements shouldComponentUpdate, in which it compares the current props and state with the next ones and returns false if the equalities pass.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;This pattern is indeed a common way to improve performance of your React applications. The only catch though was that for this to work correctly, we needed some way to quickly do shallow checks in order to determine equality. Since all our data i.e. &lt;code&gt;questions&lt;/code&gt; and &lt;code&gt;blocks&lt;/code&gt; is nested this meant we could not use the mixin as it is. As recommended by the docs, this is where using Immutable collections with a library like &lt;a href=&#34;http://facebook.github.io/immutable-js/&#34;&gt;Immutable&lt;/a&gt; comes in handy.&lt;/p&gt;

&lt;p&gt;So for next week, my goal will be convert all my collections into Immutable so that I can harness the power of PureRenderMixin in my app! Stay tuned for next week.&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Week 4 - Surveyman</title>
      <link>https://prakhar.me/articles/surveyman-week4/</link>
      <pubDate>Sun, 17 May 2015 15:50:49 +0300</pubDate>
      
      <guid>https://prakhar.me/articles/surveyman-week4/</guid>
      <description>&lt;p&gt;I closed last week&amp;rsquo;s &lt;a href=&#34;https://prakhar.me/articles/surveyman-week3&#34;&gt;update&lt;/a&gt; giving an indication that I started work on a standalone &lt;a href=&#34;http://prakhar.me/react-tags/example/&#34;&gt;React component&lt;/a&gt;. After working on the component this week, I was finally able to finish it and publish it on &lt;a href=&#34;http://npmjs.org/package/react-tag-input&#34;&gt;NPM&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;So before I go into what React-tag-input does, I&amp;rsquo;ll try and explain the motivation for building it. If you&amp;rsquo;ve been following closely, the primary way of building the survey in Surveyman is via Drag and Drop. On the plus side, this makes it easy for non-technical users to interact but on the negative side drag and drop can become cumbersome especially if you&amp;rsquo;re relying it on to repeatedly add items.&lt;/p&gt;

&lt;p&gt;Last week, after adding an alternative way to add a question and block I was in the lookout for making a convenient way for adding Options. Luckily, after multiple conversations with a &lt;a href=&#34;http://vickychijwani.me/&#34;&gt;friend&lt;/a&gt; and some &lt;a href=&#34;http://mail.google.com&#34;&gt;inspiration&lt;/a&gt; I decided to use a tag like component where users could add options in one go. Since, the order of options is important, I also wanted the options to be order able. Lastly, since most of options in a typical survey would be re-used, especially the ones that are a part of &lt;a href=&#34;http://en.wikipedia.org/wiki/Likert_scale&#34;&gt;Likert scale&lt;/a&gt;, the ability to autosuggest would keep the errors to a minimum and save on typing.&lt;/p&gt;

&lt;p&gt;After failing to find a React component that could satisfy the above requirement, I decided that it would be a good idea to build one myself and then open-source in the community so that others could also benefit. Apart from deriving satisfaction from dog-fooding a component in my project, the idea of publishing a complete, useable react component in the wild sounded too interesting to let go.&lt;/p&gt;

&lt;p&gt;Overall the entire exercise was a lot of fun and I definitely learnt a lot. The fact that I was able to build a working prototype is less than 150 lines of code (initially) speaks volumes about how powerful React is. The paradigm of thinking in terms of re-rendering the entire DOM makes it extremely easy for a developer to reason about the UI and the enclosed state. It is definitely one of the most exciting JS libraries I&amp;rsquo;ve ever worked with.&lt;/p&gt;

&lt;p&gt;Not all is rosy though. When you are using React, you are inundated with lots of jargon and tools that form a vital part of the React ecosystem such as ES6, Webpack, Immutability etc. Initially it does feel that its a lot to take in. Hence, the toughest part for me was following the best practices on how to build a React component that could be used easily via CommonJS and also the browser (via a simple script tag). Thankfully, libraries like &lt;a href=&#34;https://github.com/gaearon/react-dnd&#34;&gt;ReactDND&lt;/a&gt; helped me a lot and I was finally able to a come up with a satisfactory &lt;code&gt;dev&lt;/code&gt; and &lt;code&gt;build&lt;/code&gt; system in Webpack.&lt;/p&gt;

&lt;p&gt;A quick demo of the component can be viewed &lt;a href=&#34;http://prakhar.me/react-tags/example/&#34;&gt;here&lt;/a&gt; and the code is available on &lt;a href=&#34;https://github.com/prakhar1989/react-tags&#34;&gt;Github&lt;/a&gt; for you to fork and point out issues!&lt;/p&gt;

&lt;p&gt;My plan for this week is to integrate this component with Surveyman and finish out the CRUD functionalities. Unfortunately, a key project at my workplace is sucking in all my time resulting in drowsy days and sleep deprived nights so I&amp;rsquo;ll have to balance my time really well to stay productive on open-source this week. Let&amp;rsquo;s hope I am able to meet this milestone!&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Week 3 - Surveyman</title>
      <link>https://prakhar.me/articles/surveyman-week3/</link>
      <pubDate>Sun, 10 May 2015 15:50:49 +0300</pubDate>
      
      <guid>https://prakhar.me/articles/surveyman-week3/</guid>
      <description>&lt;p&gt;This update is coming in a bit late, especially after the week 1 and week 2 updates posted earlier. After progressing a bit on the initial setup and features I decided to take a week off Surveyman and instead wait for the GSOC results to arrive so that I could get feedback from &lt;a href=&#34;http://people.cs.umass.edu/~etosch/&#34;&gt;Emma&lt;/a&gt;, my mentor for this project and see if I were on the right track.&lt;/p&gt;

&lt;p&gt;Much to my delight I got selected in GSOC to work for Surveyman! To top it all,
I was the &lt;a href=&#34;http://google-opensource.blogspot.com/2015/05/gsoc-2015-stats-part-1-all-about.html&#34;&gt;first ever student&lt;/a&gt; to represent Kuwait in Google Summer of Code!&lt;/p&gt;

&lt;figure&gt;&lt;img data-action=&#34;zoom&#34; src=&#34;https://prakhar.me/images/gsoc_kuwait.png&#34;&gt;&lt;/img&gt;
    &lt;figcaption&gt; Kuwait&#39;s first time in GSOC! &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;Starting last week, I decided to organize tasks as Github issues so it&amp;rsquo;s easier to track progress. Having specific issues for each milestone helps me write down ideas to revisit later - this could be blog posts, github repos or other random tidbits that strike my head. Secondly, it allows me to earmark commits for that specific issue which would be useful later on. See the issue on &lt;a href=&#34;https://github.com/prakhar1989/react-surveyman/issues/9&#34;&gt;Immutability&lt;/a&gt; for example.&lt;/p&gt;

&lt;p&gt;With that out of the way, let me quickly summarize the progress I made on the project. Specifically, I knocked out issues &lt;a href=&#34;https://github.com/prakhar1989/react-surveyman/issues/14&#34;&gt;#14&lt;/a&gt;, &lt;a href=&#34;https://github.com/prakhar1989/react-surveyman/issues/13&#34;&gt;#13&lt;/a&gt; and &lt;a href=&#34;https://github.com/prakhar1989/react-surveyman/issues/6&#34;&gt;#6&lt;/a&gt; which primarily deal with adding the update and delete operations for all types. What that means is that Blocks and Questions can now be edited and deleted once they are created.&lt;/p&gt;

&lt;p&gt;The only challenging part, I felt, was factoring in all these controls in the UI without adding any visual clutter. Filling up the blocks / questions area with various icons didn&amp;rsquo;t feel like a good solution. As a result I spent some time talking to friends and looking for inspiration in other applications. Although I did go ahead with a simple &lt;em&gt;Trash&lt;/em&gt; icon for delete, the solution for editing question text turned out to be much more interesting.&lt;/p&gt;

&lt;figure&gt;&lt;img data-action=&#34;zoom&#34; src=&#34;https://prakhar.me/images/delete_edit.png&#34;&gt;&lt;/img&gt;
    &lt;figcaption&gt; In-place editing and deletion for Questions! &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;p&gt;I started off working on &lt;a href=&#34;html5demos.com/contenteditable&#34;&gt;HTML5 ContentEditable&lt;/a&gt; implementation but after diving deeper and looking at some of the &lt;a href=&#34;https://github.com/facebook/react/issues/2477&#34;&gt;issues&lt;/a&gt; I decided to just replace the &lt;code&gt;&amp;lt;span&amp;gt;&lt;/code&gt; tag with an &lt;code&gt;input&lt;/code&gt; when clicked. With some React awesomeness, I was able to build this in just a few lines of code -&lt;/p&gt;

&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-javascript&#34; data-lang=&#34;javascript&#34;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&#34;c1&#34;&gt;// file - js/components/Question.js&lt;/span&gt;

&lt;span class=&#34;nx&#34;&gt;render&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
    &lt;span class=&#34;c1&#34;&gt;// ...&lt;/span&gt;
    &lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;input&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;input&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;type&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&amp;quot;text&amp;quot;&lt;/span&gt;
            &lt;span class=&#34;nx&#34;&gt;defaultValue&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;k&#34;&gt;this&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;props&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;qtext&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
            &lt;span class=&#34;nx&#34;&gt;style&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;{{&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;width&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;mi&#34;&gt;200&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;}}&lt;/span&gt;
            &lt;span class=&#34;nx&#34;&gt;onBlur&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;k&#34;&gt;this&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;handleEdit&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
            &lt;span class=&#34;nx&#34;&gt;onKeyPress&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;k&#34;&gt;this&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;handleEdit&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;/&amp;gt;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt;
            
    &lt;span class=&#34;o&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;div&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;className&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&amp;quot;qtext-area&amp;quot;&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;gt;&lt;/span&gt;
        &lt;span class=&#34;o&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;span&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;className&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&amp;quot;qtext&amp;quot;&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;onClick&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;k&#34;&gt;this&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;toggleInput&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;gt;&lt;/span&gt;
            &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;this&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;state&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;editing&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;?&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;input&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;this&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;props&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;qtext&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
        &lt;span class=&#34;o&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;err&#34;&gt;/span&amp;gt;&lt;/span&gt;
        &lt;span class=&#34;o&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;i&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;className&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&amp;quot;ion-trash-b&amp;quot;&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;onClick&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;k&#34;&gt;this&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;handleDelete&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;err&#34;&gt;/i&amp;gt;&lt;/span&gt;
    &lt;span class=&#34;o&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;err&#34;&gt;/div&amp;gt;&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;},&lt;/span&gt; 
&lt;span class=&#34;nx&#34;&gt;toggleInput&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;this&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;setState&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;({&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;editing&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;kc&#34;&gt;true&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;});&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;},&lt;/span&gt;
&lt;span class=&#34;nx&#34;&gt;handleEdit&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;e&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
    &lt;span class=&#34;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;e&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;type&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;===&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;quot;blur&amp;quot;&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;||&lt;/span&gt; 
       &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;e&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;type&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;===&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;quot;keypress&amp;quot;&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;amp;&amp;amp;&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;e&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;key&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;===&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;quot;Enter&amp;quot;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;))&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;

        &lt;span class=&#34;k&#34;&gt;this&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;setState&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;({&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;editing&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;kc&#34;&gt;false&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;});&lt;/span&gt;

        &lt;span class=&#34;c1&#34;&gt;// if the new value is blank do nothing&lt;/span&gt;
        &lt;span class=&#34;k&#34;&gt;if&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;e&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;target&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;value&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;trim&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;===&lt;/span&gt; &lt;span class=&#34;s2&#34;&gt;&amp;quot;&amp;quot;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;)&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
            &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;;&lt;/span&gt; 
        &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
        &lt;span class=&#34;nx&#34;&gt;SurveyActions&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;saveEditText&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;e&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;target&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;value&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;,&lt;/span&gt; &lt;span class=&#34;k&#34;&gt;this&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;props&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;id&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;);&lt;/span&gt;
    &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;For a more complete breakdown, feel free to the browse the issues linked above and view the source code.&lt;/p&gt;

&lt;p&gt;If you&amp;rsquo;ve been following closely, then you might have noticed that Options are still not editable. Well, the short answer is that while looking around for ways to factor in edit / delete in the small-sized element I came across a neat idea which eventually lead into me building a standalone &lt;a href=&#34;http://github.com/prakhar1989/react-tags&#34;&gt;react component&lt;/a&gt;. For the long answer, wait till next week!&lt;/p&gt;
</description>
    </item>
    
    <item>
      <title>Week 2 - Surveyman</title>
      <link>https://prakhar.me/articles/surveyman-week2/</link>
      <pubDate>Mon, 13 Apr 2015 06:58:24 +0300</pubDate>
      
      <guid>https://prakhar.me/articles/surveyman-week2/</guid>
      <description>

&lt;p&gt;After the drop targets and drag sources were configured last week, this week my primary motive was to develop a better UI for adding &lt;code&gt;Question&lt;/code&gt; types. After mulling over the UI a bit, I decided to replace the existing Javascript &lt;code&gt;prompt&lt;/code&gt; with a &lt;code&gt;modal&lt;/code&gt; which would allow text entry and multiple radio buttons / checkboxes to configure various parts of a &lt;code&gt;Question&lt;/code&gt; type.&lt;/p&gt;

&lt;figure&gt;&lt;img src=&#34;https://prakhar.me/images/surveyman-modal.png&#34;&gt;&lt;/img&gt;
    &lt;figcaption&gt; Adding a Bootstrap Modal &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;h3 id=&#34;ui&#34;&gt;UI&lt;/h3&gt;

&lt;blockquote&gt;
&lt;p&gt;The key idea behind Surveyman is that by giving survey authors a way to write their surveys that steers them away from unnecessary ordering constraints, we can apply static analysis, randomization, and statistical dynamic analysis to locate survey errors and ensure the quality of responses.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;The extra configuration parameters allow survey authors to modify the behavior of Surveyman. For example, if you are adding a question where you don&amp;rsquo;t want your options to be displayed in a random order, checking the &lt;code&gt;ordered&lt;/code&gt; checkbox ensures that Surveyman shows the options in the order in which they were added.&lt;/p&gt;

&lt;p&gt;The other half of this is displaying these parameters in a clear way to the users. For this, I decided on using the combo of meaningful icons and tooltips to communicate this information.&lt;/p&gt;

&lt;figure&gt;&lt;img src=&#34;https://prakhar.me/images/surveyman-icons.png&#34;&gt;&lt;/img&gt;
    &lt;figcaption&gt; Icons for showing configuration &lt;/figcaption&gt;
&lt;/figure&gt;

&lt;h3 id=&#34;architecture&#34;&gt;Architecture&lt;/h3&gt;

&lt;p&gt;To build this, I started off with migrating from &lt;a href=&#34;https://getskeleton.com&#34;&gt;Skeleton&lt;/a&gt; to &lt;a href=&#34;https://getbootstrap.com&#34;&gt;Bootstrap&lt;/a&gt; so that I could use the &lt;a href=&#34;http://react-bootstrap.github.io&#34;&gt;React-Bootstrap&lt;/a&gt; library. The motivation here was to save time by using a set of prebuilt components that can be used right off the shelf. Apart from the modal, I knew that I&amp;rsquo;d be needing an &lt;code&gt;Alert&lt;/code&gt; and &lt;code&gt;Tooltip&lt;/code&gt; very soon.&lt;/p&gt;

&lt;p&gt;With that done, the next part was laying down the architecture for Modals. The key idea here was to setup an action that would be called when the question is dropped on a Block. This would then cause the store to change a piece of data, which in our case would be the &lt;code&gt;isOpen&lt;/code&gt; property on the modal. The triggering of this change would then finally re-render the views.&lt;/p&gt;

&lt;p&gt;All this sounds simple, especially once you have a look at the &lt;a href=&#34;http://react-bootstrap.github.io/components.html#modals&#34;&gt;sample code&lt;/a&gt; for custom triggering a modal. But things start to get interesting once you start building the form inside the modal. How do you tag DOM elements so that you can retrieve their value when an event is called? Use &lt;a href=&#34;https://facebook.github.io/react/docs/more-about-refs.html&#34;&gt;refs&lt;/a&gt; silly!&lt;/p&gt;

&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-html&#34; data-lang=&#34;html&#34;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;div&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;className&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;quot;form-group&amp;quot;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
   &lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;label&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;htmlFor&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;quot;qtext&amp;quot;&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;Question Text&lt;span class=&#34;p&#34;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;label&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
   &lt;span class=&#34;p&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;input&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;type&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;quot;text&amp;quot;&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;placeholder&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;quot;What is value of 4 + 5?&amp;quot;&lt;/span&gt; 
      &lt;span class=&#34;na&#34;&gt;className&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;quot;form-control&amp;quot;&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;id&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;quot;qtext&amp;quot;&lt;/span&gt; &lt;span class=&#34;na&#34;&gt;ref&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s&#34;&gt;&amp;quot;qtext&amp;quot;&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;/&amp;gt;&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;&amp;lt;/&lt;/span&gt;&lt;span class=&#34;nt&#34;&gt;div&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;So I built a few form elements and tagged with them using refs and when I ran the application I got a cryptic error&lt;/p&gt;

&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-javascript&#34; data-lang=&#34;javascript&#34;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;Uncaught&lt;/span&gt; &lt;span class=&#34;nb&#34;&gt;Error&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;Invariant&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;Violation&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;addComponentAsRefTo&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;(...)&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;Only&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;a&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;ReactOwner&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;can&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;have&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;refs&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;You can go dive deeper into the &lt;a href=&#34;https://github.com/react-bootstrap/react-bootstrap/issues/223&#34;&gt;issue&lt;/a&gt; but the main idea here was with the way &lt;code&gt;renderOverlay&lt;/code&gt; was rendering the DOM in a different DOM Tree. As a fix, I had to create a &lt;code&gt;BaseModal&lt;/code&gt; component that would then rendered inside a &lt;code&gt;renderOverlay&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;The final &lt;a href=&#34;https://github.com/prakhar1989/react-surveyman/blob/202dbd8520347e32322910834750942c43fb46e5/js/components/QuestionModal.js&#34;&gt;component&lt;/a&gt; looked like this -&lt;/p&gt;

&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-javascript&#34; data-lang=&#34;javascript&#34;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;BaseModal&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;React&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;createClass&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;({&lt;/span&gt;
   &lt;span class=&#34;c1&#34;&gt;// custom handler code&lt;/span&gt;
   &lt;span class=&#34;nx&#34;&gt;render&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
      &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;Modal&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;/&amp;gt;&lt;/span&gt;
   &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;});&lt;/span&gt;

&lt;span class=&#34;kd&#34;&gt;var&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;QuestionModal&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;=&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;React&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;createClass&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;({&lt;/span&gt;
   &lt;span class=&#34;nx&#34;&gt;mixins&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;[&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;OverlayMixin&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;],&lt;/span&gt;
   &lt;span class=&#34;nx&#34;&gt;render&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
      &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;div&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;className&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;s2&#34;&gt;&amp;quot;static-modal&amp;quot;&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;err&#34;&gt;/div&amp;gt;&lt;/span&gt;
   &lt;span class=&#34;p&#34;&gt;},&lt;/span&gt;
   &lt;span class=&#34;nx&#34;&gt;renderOverlay&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
      &lt;span class=&#34;k&#34;&gt;return&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;(&lt;/span&gt;
         &lt;span class=&#34;k&#34;&gt;this&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;props&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;open&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;?&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;BaseModal&lt;/span&gt; &lt;span class=&#34;nx&#34;&gt;parentId&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;k&#34;&gt;this&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;props&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;parentID&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;/&amp;gt;&lt;/span&gt; 
                         &lt;span class=&#34;o&#34;&gt;:&lt;/span&gt; &lt;span class=&#34;o&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;div&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;&amp;gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;err&#34;&gt;/div&amp;gt;&lt;/span&gt;
      &lt;span class=&#34;p&#34;&gt;)&lt;/span&gt;
   &lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;});&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;The last step was simply rendering the &lt;code&gt;QuestionModal&lt;/code&gt; in the &lt;code&gt;Application&lt;/code&gt; component.&lt;/p&gt;

&lt;div class=&#34;highlight&#34;&gt;&lt;pre&gt;&lt;code class=&#34;language-javascript&#34; data-lang=&#34;javascript&#34;&gt;&lt;span&gt;&lt;/span&gt;&lt;span class=&#34;c1&#34;&gt;// in Application component&lt;/span&gt;
&lt;span class=&#34;nx&#34;&gt;render&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;()&lt;/span&gt; &lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;
   &lt;span class=&#34;o&#34;&gt;&amp;lt;&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;QuestionModal&lt;/span&gt;
   &lt;span class=&#34;nx&#34;&gt;isOpen&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;modalState&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;question&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
   &lt;span class=&#34;nx&#34;&gt;parentID&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;=&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;{&lt;/span&gt;&lt;span class=&#34;k&#34;&gt;this&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;state&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;.&lt;/span&gt;&lt;span class=&#34;nx&#34;&gt;dropTargetID&lt;/span&gt;&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;&lt;span class=&#34;o&#34;&gt;/&amp;gt;&lt;/span&gt;
&lt;span class=&#34;p&#34;&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;&lt;/div&gt;


&lt;p&gt;The other parts of the application were quite straightforward and got done rather quickly. I did get stuck with building a self-dismissing alert box but the React IRC channel helped out quite a bit.&lt;/p&gt;

&lt;p&gt;If you&amp;rsquo;ve read this far, go ahead and take the app for a &lt;a href=&#34;http://prakhar.me/react-surveyman&#34;&gt;spin&lt;/a&gt;. For the complete code, feel free to browse the &lt;a href=&#34;https://github.com/prakhar1989/react-surveyman&#34;&gt;Github repository&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;This week my plan is to finish off in-place editing Block, Options and Questions. Stay tuned for the next post!&lt;/p&gt;
</description>
    </item>
    
  </channel>
</rss>