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

<channel>
	<title>the brook &#187; programming</title>
	<atom:link href="http://yavin4.anshul.info/category/tech/programming/feed/" rel="self" type="application/rss+xml" />
	<link>http://yavin4.anshul.info</link>
	<description></description>
	<lastBuildDate>Fri, 20 Jan 2012 10:12:15 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.2.1</generator>
		<item>
		<title>Spin locks and the vicious multi-threading cycle</title>
		<link>http://yavin4.anshul.info/2011/01/27/spin-locks-and-the-vicious-multi-threading-cycle/</link>
		<comments>http://yavin4.anshul.info/2011/01/27/spin-locks-and-the-vicious-multi-threading-cycle/#comments</comments>
		<pubDate>Thu, 27 Jan 2011 07:54:43 +0000</pubDate>
		<dc:creator>Anshul</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[tech]]></category>

		<guid isPermaLink="false">http://yavin4.anshul.info/?p=530</guid>
		<description><![CDATA[Multithreading is a subject that is really hard for me to keep straight in my head. This is a typical timeline of my neuron activity in any multi-threading topic that&#8217;s above absolutely basic. Time X: Awareness of a sore lack of understanding about topic. Time X+1: Read the wikipedia article and top few Google search [...]]]></description>
			<content:encoded><![CDATA[<p>Multithreading is a subject that is really hard for me to keep straight in my head. This is a typical timeline of my neuron activity in any multi-threading topic that&#8217;s above absolutely basic.</p>
<p>Time X: Awareness of a sore lack of understanding about topic.</p>
<p>Time X+1: Read the wikipedia article and top few Google search results about the topic. Convince myself that I kinda-sorta understand what it all means.</p>
<p>Time X+2: Promise myself to read up more thoroughly on the topic. After a hurried stack overflow or Google search of how to learn multi-threading, try to bookmark various excellent resources on multi-threading like C# threading, the little book of semaphores or the Java concurrency book. Fail on realizing that all of them are already filed under &#8216;toread&#8217; in delicious / pinboard.</p>
<p>Time X+3: Berate myself for wasting time.</p>
<p>Time X+4: With 33% probability, read the first chapter of one of the materials above. With exponentially decreasing probability, read the next chapter(s), never getting past the third. In any case, promise myself that when the C++ concurrency book is finally out, I will buy and read it.</p>
<p>Time X+(5&#8230;N-2): Real life interrupt.</p>
<p>Time X+(N-1): (Read inspiring but confusing blog post about how company X increased performance by multithreading) OR (come across a tough interview question on multithreading) OR (have a discussion with the significant other about spin locks) OR (&#8230;)</p>
<p>Time X+N: Time = Time &#8211; N</p>
<p>As you can probably guess, I&#8217;m in the penultimate step of that cycle. In an effort to take a little break from it without committing the time needed for reading a book, I&#8217;ve decided to write a bit about my understanding of spin locks, hoping it at least helps make N larger.</p>
<p>We need locks to guard against concurrent access to a resource or a value. If an ATM reads a value M for your someone&#8217;s bank balance while an account transfer request reads the same value, because if that happens, both the ATM and transfer code would let M dollars be withdrawn, resulting in 2*M dollars where there were only M before. That&#8217;s a bug. Except if you&#8217;re the Federal Reserve, but that&#8217;s another story.</p>
<p>Implicit in the need for locks is the fact that the code we&#8217;re running is multi-threaded. That doesn&#8217;t imply parallel processing &#8212; even on a single-core CPU, multiple threads are being switched in to and out of fast enough to give the illusion of parallel processing. The switching is done either by the kernel, or a thread library, in accordance with some scheduling algorithm.</p>
<p>So when thread X is running some code and requests a resource that is locked by some other thread Y, thread X then has to wait for thread Y to release the lock. Waiting can take two different forms, depending on the locking mechanism. If the resource to be acquired has a spin-lock, thread X must continuously poll a boolean guard variable or flag (e.g. is_locked) until it can acquire the lock, a process known as busy-waiting. Conceptually, this is like the following:<br />
<code><br />
while (true) {<br />
&nbsp;if (!is_locked) {<br />
&nbsp;&nbsp;  is_locked = true; // acquire lock<br />
&nbsp;&nbsp;  break;<br />
&nbsp; }<br />
}</p>
<p>// work with lock</p>
<p>is_locked = false; // release lock<br />
</code></p>
<p>In reality the code above won&#8217;t work, since the check and subsequent lock acquisition isn&#8217;t atomic. Proper busy-waiting usually involves an atomic test-and-set operation which has to be supported in hardware. The upshot, however, is that the thread doesn&#8217;t really stop, it keeps looping until the resource in question gets released.</p>
<p>The other option is for thread X to sleep on encountering a locked resource and for the kernel or thread library to resume (wake-up) the thread once the resource is available. This is how most multi-threading primitives like mutexes or condition variables operate. Unlike spin-locks, in this case, the thread is not scheduled to run until there is a change in the status of the resource which it was requesting.</p>
<p>Which one is better depends primarily on how long the expected wait time is. On the face of it, spin-locking seems more expensive as the thread keeps running (and therefore consuming CPU cycles) even while waiting; whereas a thread encountering a mutex-locked resource goes into the background until it can do useful work, freeing up the CPU to work on other tasks. There are costs associated with the latter, though; the cost of putting the thread on the suspend queue and bringing it back, and the cost of the kernel keeping track of the resource state in order to wake up the suspended thread. If your thread expects to have an extremely short waiting time, then, a spin-lock may be more efficient.</p>
<p>I don&#8217;t have a very good idea of the numbers involved, but a good lower bound for a thread state change is the time it takes to do a context-switch, which is on the order of a few hundred CPU cycles; a really short period of time, about the same as RAM latency. A disk seek, by comparison, has a latency measured in millions of cycles (see this <a href="http://duartes.org/gustavo/blog/post/what-your-computer-does-while-you-wait">excellent article</a> for more info). The bottom line &#8212; you shouldn&#8217;t be using spin-locks unless you&#8217;re writing very low level code and know exactly what you&#8217;re doing; but in that case you don&#8217;t need me to be telling you these things!</p>
]]></content:encoded>
			<wfw:commentRss>http://yavin4.anshul.info/2011/01/27/spin-locks-and-the-vicious-multi-threading-cycle/feed/</wfw:commentRss>
		<slash:comments>6</slash:comments>
		</item>
		<item>
		<title>A useful C++ interview question</title>
		<link>http://yavin4.anshul.info/2010/12/07/a-useful-c-interview-question/</link>
		<comments>http://yavin4.anshul.info/2010/12/07/a-useful-c-interview-question/#comments</comments>
		<pubDate>Tue, 07 Dec 2010 06:51:33 +0000</pubDate>
		<dc:creator>Anshul</dc:creator>
				<category><![CDATA[programming]]></category>

		<guid isPermaLink="false">http://yavin4.anshul.info/?p=507</guid>
		<description><![CDATA[It&#8217;s surprising how few people who say they know C++ really know C++. This is my favourite basic C++ interview question. Lets say you want to write your own, simple fixed-size vector class for integers. The specification is simple &#8212; construct the vector with one argument, the size of the vector. Construction creates a vector [...]]]></description>
			<content:encoded><![CDATA[<p>It&#8217;s surprising how few people who say they know C++ really know C++. This is my favourite basic C++ interview question.</p>
<p>Lets say you want to write your own, simple fixed-size vector class for integers. The specification is simple &#8212; construct the vector with one argument, the size of the vector. Construction creates a vector of the given size, all initialized to zero. Support accessors via indexing.</p>
<p>Here&#8217;s the first try (which I write on the whiteboard for the candidate):</p>
<pre style='color:#000000;background:#ffffff;'><span style='color:#004a43; '>#</span><span style='color:#004a43; '>include </span><span style='color:#800000; '>"</span><span style='color:#40015a; '>stdio.h</span><span style='color:#800000; '>"</span>
<span style='color:#004a43; '>#</span><span style='color:#004a43; '>define</span><span style='color:#004a43; '> OUT_OF_RANGE 1</span>

<span style='color:#800000; font-weight:bold; '>class</span> Vec <span style='color:#800080; '>{</span>
<span style='color:#e34adc; '>&#xa0;&#xa0;</span><span style='color:#800000; font-weight:bold; '>public</span><span style='color:#e34adc; '>:</span>
    Vec<span style='color:#808030; '>(</span><span style='color:#800000; font-weight:bold; '>int</span> sz<span style='color:#808030; '>)</span> <span style='color:#800080; '>:</span> sz_<span style='color:#808030; '>(</span>sz<span style='color:#808030; '>)</span> <span style='color:#800080; '>{</span>
      data_ <span style='color:#808030; '>=</span> <span style='color:#800000; font-weight:bold; '>new</span> <span style='color:#800000; font-weight:bold; '>int</span><span style='color:#808030; '>[</span>sz_<span style='color:#808030; '>]</span><span style='color:#800080; '>;</span>
      <span style='color:#800000; font-weight:bold; '>for</span> <span style='color:#808030; '>(</span><span style='color:#800000; font-weight:bold; '>int</span> i<span style='color:#808030; '>=</span><span style='color:#008c00; '>0</span><span style='color:#800080; '>;</span> i <span style='color:#808030; '>!</span><span style='color:#808030; '>=</span> sz_<span style='color:#800080; '>;</span> <span style='color:#808030; '>+</span><span style='color:#808030; '>+</span>i<span style='color:#808030; '>)</span> <span style='color:#800080; '>{</span> data_<span style='color:#808030; '>[</span>i<span style='color:#808030; '>]</span> <span style='color:#808030; '>=</span> <span style='color:#008c00; '>0</span><span style='color:#800080; '>;</span> <span style='color:#800080; '>}</span>
    <span style='color:#800080; '>}</span>

    <span style='color:#808030; '>~</span>Vec<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span> <span style='color:#800080; '>{</span> <span style='color:#800000; font-weight:bold; '>delete</span> data_<span style='color:#800080; '>;</span> <span style='color:#800080; '>}</span>

    <span style='color:#800000; font-weight:bold; '>int</span> size<span style='color:#808030; '>(</span><span style='color:#808030; '>)</span> <span style='color:#800080; '>{</span> <span style='color:#800000; font-weight:bold; '>return</span> sz_<span style='color:#800080; '>;</span> <span style='color:#800080; '>}</span>

    <span style='color:#800000; font-weight:bold; '>int</span><span style='color:#808030; '>&amp;</span> <span style='color:#800000; font-weight:bold; '>operator</span><span style='color:#808030; '>[</span><span style='color:#808030; '>]</span><span style='color:#808030; '>(</span><span style='color:#800000; font-weight:bold; '>int</span> i<span style='color:#808030; '>)</span> <span style='color:#800080; '>{</span>
      <span style='color:#800000; font-weight:bold; '>if</span> <span style='color:#808030; '>(</span>i <span style='color:#808030; '>&lt;</span> sz_<span style='color:#808030; '>)</span> <span style='color:#800000; font-weight:bold; '>return</span> data_<span style='color:#808030; '>[</span>i<span style='color:#808030; '>]</span><span style='color:#800080; '>;</span>
      <span style='color:#800000; font-weight:bold; '>else</span> <span style='color:#800000; font-weight:bold; '>throw</span> OUT_OF_RANGE<span style='color:#800080; '>;</span>
    <span style='color:#800080; '>}</span>

    <span style='color:#800000; font-weight:bold; '>int</span> sz_<span style='color:#800080; '>;</span>
    <span style='color:#800000; font-weight:bold; '>int</span><span style='color:#808030; '>*</span> data_<span style='color:#800080; '>;</span>
<span style='color:#800080; '>}</span><span style='color:#800080; '>;</span>

<span style='color:#800000; font-weight:bold; '>int</span> <span style='color:#400000; '>main</span><span style='color:#808030; '>(</span><span style='color:#808030; '>)</span> <span style='color:#800080; '>{</span>
  Vec v<span style='color:#808030; '>(</span><span style='color:#008c00; '>10</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
  <span style='color:#800000; font-weight:bold; '>int</span> i<span style='color:#800080; '>;</span>
  <span style='color:#800000; font-weight:bold; '>for</span> <span style='color:#808030; '>(</span>i <span style='color:#808030; '>=</span> <span style='color:#008c00; '>0</span><span style='color:#800080; '>;</span> i <span style='color:#808030; '>!</span><span style='color:#808030; '>=</span> <span style='color:#008c00; '>10</span><span style='color:#800080; '>;</span> <span style='color:#808030; '>+</span><span style='color:#808030; '>+</span>i<span style='color:#808030; '>)</span> v<span style='color:#808030; '>[</span>i<span style='color:#808030; '>]</span> <span style='color:#808030; '>=</span> i<span style='color:#808030; '>+</span><span style='color:#008c00; '>1</span><span style='color:#800080; '>;</span>

  <span style='color:#696969; '>// this prints "1 2 3 4 5 6 7 8 9 10"</span>
  <span style='color:#800000; font-weight:bold; '>for</span> <span style='color:#808030; '>(</span>i <span style='color:#808030; '>=</span> <span style='color:#008c00; '>0</span><span style='color:#800080; '>;</span> i <span style='color:#808030; '>!</span><span style='color:#808030; '>=</span> <span style='color:#008c00; '>10</span><span style='color:#800080; '>;</span> <span style='color:#808030; '>+</span><span style='color:#808030; '>+</span>i<span style='color:#808030; '>)</span> printf<span style='color:#808030; '>(</span><span style='color:#800000; '>"</span><span style='color:#0f69ff; '>%d</span><span style='color:#0000e6; '> </span><span style='color:#800000; '>"</span><span style='color:#808030; '>,</span> v<span style='color:#808030; '>[</span>i<span style='color:#808030; '>]</span><span style='color:#808030; '>)</span><span style='color:#800080; '>;</span>
<span style='color:#800080; '>}</span>
</pre>
<p>The question is this. The code, with a basic test, compiles and works as you might expect. Is there anything wrong with this code?</p>
<p>If your candidate stares at this and says &#8220;looks OK to me&#8221;, it&#8217;s probably time to end the interview and send them home. The correct answer, of course, is &#8220;many, many things.&#8221;</p>
<p><b>No encapsulation.</b> The members <code>sz_</code> and <code>data_</code> should be declared private, otherwise clients could corrupt the data, intentionally or otherwise.</p>
<p><b>The destructor won&#8217;t delete the allocated array.</b> It should say <code>delete [] data_;</code>. If they don&#8217;t know this, they have no idea of C++&#8217;s memory model.</p>
<p><b>It&#8217;s a mistake to use an integral size,</b> since integers can be negative. In the given code, if you give a negative integer parameter to the constructor, new will throw an exception, or even if it doesn&#8217;t the for loop will be infinite. If you call an accessor with a negative index (<code>v[-2]</code>), it&#8217;ll access illegal memory. The size parameter should ideally be <code>std::size_t</code>, or at least <code>unsigned</code>.</p>
<p><b>The compiler will auto-generate crash-inducing code for copy construction and assignment,</b> since these haven&#8217;t been defined. For example, the auto-generated assignment operator will look like this:</p>
<pre style='color:#000000;background:#ffffff;'>Vec<span style='color:#808030; '>&amp;</span> <span style='color:#800000; font-weight:bold; '>operator</span><span style='color:#808030; '>=</span> <span style='color:#808030; '>(</span><span style='color:#800000; font-weight:bold; '>const</span> Vec<span style='color:#808030; '>&amp;</span> rhs<span style='color:#808030; '>)</span> <span style='color:#800080; '>{</span>
  <span style='color:#800000; font-weight:bold; '>this</span><span style='color:#808030; '>.</span>sz_ <span style='color:#808030; '>=</span> rhs<span style='color:#808030; '>.</span>sz_<span style='color:#800080; '>;</span>
  <span style='color:#800000; font-weight:bold; '>this</span><span style='color:#808030; '>.</span>data_ <span style='color:#808030; '>=</span> rhs<span style='color:#808030; '>.</span>data_<span style='color:#800080; '>;</span>
  <span style='color:#800000; font-weight:bold; '>return</span> <span style='color:#808030; '>*</span><span style='color:#800000; font-weight:bold; '>this</span><span style='color:#800080; '>;</span>
<span style='color:#800080; '>}</span>
</pre>
<p>Note that this copies the pointer to the array and not the array. The simple act of adding <code>Vec v2(10); v2 = v1;</code> at the end of the main function above will cause delete to be called twice on the same pointer, when v1 and v2 are destructed, leading to potential disaster. The candidate should be able to code both functions correctly.</p>
<p><b>You can&#8217;t use a const Vec object or reference for read-only operations,</b> which basically cripples the class. If you want to pass a Vec to a function that only reads it, <code>func(Vec v)</code> results in an expensive copy operation, and <code>func(Vec&#038; v)</code> or <code>func(Vec* v)</code> doesn&#8217;t guarantee that the vector won&#8217;t be mutated. Whats needed is to mark the <code>size()</code> function as const and add a const accessor:</p>
<pre style='color:#000000;background:#ffffff;'><span style='color:#800000; font-weight:bold; '>const</span> <span style='color:#800000; font-weight:bold; '>int</span><span style='color:#808030; '>&amp;</span> <span style='color:#800000; font-weight:bold; '>operator</span><span style='color:#808030; '>[</span><span style='color:#808030; '>]</span><span style='color:#808030; '>(</span><span style='color:#666616; '>std</span><span style='color:#800080; '>::</span><span style='color:#603000; '>size_t</span> i<span style='color:#808030; '>)</span> <span style='color:#800000; font-weight:bold; '>const</span> <span style='color:#800080; '>{</span>
  <span style='color:#696969; '>/* same code as non-const */</span>
<span style='color:#800080; '>}</span>
</pre>
<p><b>An implicit constructor could lead to uncaught bugs at compile time.</b> For example, a function with the signature <code>myFunc(Vec v)</code> could be called as <code>myFunc(5)</code> with an implicit Vec object being constructed as the argument. In almost all cases, this is unintentional and erroneous, and the constructor should be marked with the <code>explicit</code> keyword.</p>
<p>What I like about this question is that the quality of the candidate&#8217;s answers tell you a lot. Some candidates will surprise you and go beyond the basic issues listed above. If a candidate gets through these quickly, that&#8217;s a very good sign and you can go on to further improving the design, using templates, dynamic re-allocations etc.</p>
]]></content:encoded>
			<wfw:commentRss>http://yavin4.anshul.info/2010/12/07/a-useful-c-interview-question/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Introducing write-only</title>
		<link>http://yavin4.anshul.info/2010/11/29/introducing-write-only/</link>
		<comments>http://yavin4.anshul.info/2010/11/29/introducing-write-only/#comments</comments>
		<pubDate>Mon, 29 Nov 2010 07:11:42 +0000</pubDate>
		<dc:creator>Anshul</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[tech]]></category>

		<guid isPermaLink="false">http://yavin4.anshul.info/?p=503</guid>
		<description><![CDATA[Writing has become harder for me over time, evidenced by the dropping frequency of posting here over the last few years. Every now and then I resolve to fix the situation, which usually results in nothing more than a short-lived spurt of words. I think the only strategy that has remotely been successful is my [...]]]></description>
			<content:encoded><![CDATA[<p>Writing has become harder for me over time, evidenced by the dropping frequency of posting here over the last few years. Every now and then I resolve to fix the situation, which usually results in nothing more than a short-lived spurt of words. I think the only strategy that has remotely been successful is my decision to write reviews about the books that I read here.</p>
<p>As time has passed, I see an increasing need to reduce distractions while writing. At one point, I would happily compose a post in the Blogger or WordPress native web interface. For the past year or two, however, I&#8217;ve noticed that I could only write in a plain text editing tool with no interface clutter, like good old Notepad.</p>
<p>Recently, I started using Writeroom (or its equivalent, Darkroom on Windows) for writing. A full-screen text-only editor suddenly made a lot of sense. I was even more excited when I found out about <a href="http://textarea.org">textarea</a>, a really clean writeroom-like interface on the web. It&#8217;s a basic HTML/js file that I thought was the perfect interface for writing. Unfortunately, it was client-side only.</p>
<p>Thankfully, I had a week of unemployment between jobs, so I wrote a web app that adds a back-end to the textarea interface. It&#8217;s at <a href="http://write-only.appspot.com">http://write-only.appspot.com</a> and I&#8217;m using it to write this very post. It&#8217;s open to anyone: you&#8217;ll need a google account to sign in, since I used Google&#8217;s App Engine for making it.</p>
<p>It&#8217;s striking how much easier web programming has become. The last time I did any of this stuff was during my graduate school days, and then I was just interested in getting some user input over the web to use as parameters for a long computation on the back-end. I could&#8217;ve nominated my design for the worst web user-interface design ever award!</p>
<p><small>P.S. This is a purely personal project and doesn&#8217;t have the endorsement of my employers in any way, shape or form.</small></p>
]]></content:encoded>
			<wfw:commentRss>http://yavin4.anshul.info/2010/11/29/introducing-write-only/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Moronic beyond belief</title>
		<link>http://yavin4.anshul.info/2007/09/08/moronic-beyond-belief/</link>
		<comments>http://yavin4.anshul.info/2007/09/08/moronic-beyond-belief/#comments</comments>
		<pubDate>Sat, 08 Sep 2007 07:18:35 +0000</pubDate>
		<dc:creator>Anshul</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[tech]]></category>

		<guid isPermaLink="false">http://yavin4.anshul.info/2007/09/08/moronic-beyond-belief/</guid>
		<description><![CDATA[That&#8217;s me, in case you didn&#8217;t already know. While a lifetime of evidence exists, in the interests of brevity, I will only present its latest manifestation. Warning: this is a work-related (read: geeky) rant. if ( A or B ) // A and B are some valid conditions { if ( A ) return x; [...]]]></description>
			<content:encoded><![CDATA[<p>That&#8217;s me, in case you didn&#8217;t already know. While a lifetime of evidence exists, in the interests of brevity, I will only present its latest manifestation. Warning: this is a work-related (read: geeky) rant.</p>
<p><code>if ( A or B ) // A and B are some valid conditions<br />
{<br />
if ( A ) return x; // x is some valid return value<br />
if ( B ) return y; // y is some valid return value<br />
throw error; // should never reach here, right?<br />
}</code></p>
<p>There should be absolutely no reason I&#8217;d get the error. Yet, I was. And once I got the error, I praised myself for having kept an &#8220;open mind&#8221; and actually anticipating that the impossible could happen. I enchanted myself with the discovery I was about to make which allowed this execution flow and rewarded myself with a subway cookie.</p>
<p>A while later, after banging my head around trying the complex conditions A and B by hand, I realized that it was impossible for this impossible thing to happen &#8211; as any first year undergrad could tell you. I&#8217;m doing a PhD and have to keep an open mind and let my brains leak out. That&#8217;s my excuse.</p>
<p>What happened was pretty simple. Master Yoda would put it this way: <em>&#8220;Putting way too many error messages leads to lost time. Lost time leads to hurrying up your code. Hurrying up your code leads to copying and pasting aforementioned error messages. That leads to confusion about which error message you&#8217;re seeing. Confusion leads to <strong>suffering</strong>. Mmmm?&#8221;</em> I sure found that out, after trying for an hour to debug code that was completely and obviously correct.</p>
<p>After this episode, which was actually the least of my worries (ever heard of segmentation faults?), I was wondering why it felt like I&#8217;d gone around in a circle a hundred or so times. And that&#8217;s when the universe, in a cruel stroke of brilliance, responded through subversion:</p>
<p><code>$ svn commit<br />
Committed revision 314.</code></p>
<p>Just one of those days.</p>
]]></content:encoded>
			<wfw:commentRss>http://yavin4.anshul.info/2007/09/08/moronic-beyond-belief/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>TiddlyWiki hack &#8211; blog plugin</title>
		<link>http://yavin4.anshul.info/2007/01/24/tiddlywiki-hack-blog-plugin/</link>
		<comments>http://yavin4.anshul.info/2007/01/24/tiddlywiki-hack-blog-plugin/#comments</comments>
		<pubDate>Wed, 24 Jan 2007 13:30:41 +0000</pubDate>
		<dc:creator>Anshul</dc:creator>
				<category><![CDATA[blogs]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[tech]]></category>

		<guid isPermaLink="false">http://yavin4.anshul.info/2007/01/24/tiddlywiki-hack-blog-plugin/</guid>
		<description><![CDATA[I use TiddlyWiki to maintain a journal about my daily research work. It serves very well because all the journal data is kept in a single html file, which is viewable on any platform using Firefox &#8211; hence it&#8217;s far easier to put in version control. Anyhow, one thing was lacking &#8211; I wanted a [...]]]></description>
			<content:encoded><![CDATA[<p>I use <a href="http://www.tiddlywiki.com/">TiddlyWiki</a> to maintain a journal about my daily research work. It serves very well because all the journal data is kept in a single html file, which is viewable on any platform using Firefox &#8211; hence it&#8217;s far easier to put in version control.</p>
<p>Anyhow, one thing was lacking &#8211; I wanted a blog-like (time-based) view when I opened my tiddlywiki file, for easy parsing of previous posts (usually, the previous day&#8217;s work). A kind soul had written a plugin for it (<a href="http://www.checkettsweb.com/#%5B%5BWeblog%20plugin%5D%5D">here</a>) &#8211; but it unfortunately it doesn&#8217;t work with the current version. So I decided to see how rusty my Javascript was (<em>very</em> rusty, as it turns out), and after an hour or so of hair pulling, I finally fixed it to get what I believe is a working plugin. <a href="http://www.anshul.info/blogwiki.html">Grab it from here</a>.<br />
<strong><br />
[Update 09-Jan-2008]</strong> <a href="http://www.sics.se/~emmanuel/">Emmanuel</a> has been kind enough to add restricted tag support, so you can, for example, only apply the blog-like behavior to entries that have a certain tag. Thanks!</p>
]]></content:encoded>
			<wfw:commentRss>http://yavin4.anshul.info/2007/01/24/tiddlywiki-hack-blog-plugin/feed/</wfw:commentRss>
		<slash:comments>10</slash:comments>
		</item>
		<item>
		<title>Thought for the day</title>
		<link>http://yavin4.anshul.info/2006/11/10/thought-for-the-day/</link>
		<comments>http://yavin4.anshul.info/2006/11/10/thought-for-the-day/#comments</comments>
		<pubDate>Thu, 09 Nov 2006 19:11:08 +0000</pubDate>
		<dc:creator>Anshul</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[research]]></category>

		<guid isPermaLink="false">http://yavin4.anshul.info/2006/11/10/thought-for-the-day/</guid>
		<description><![CDATA[Software bugs cause you to believe in a higher power&#8230; that likes to mess with your life.]]></description>
			<content:encoded><![CDATA[<p>Software bugs cause you to believe in a higher power&#8230; that likes to mess with your life.</p>
]]></content:encoded>
			<wfw:commentRss>http://yavin4.anshul.info/2006/11/10/thought-for-the-day/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
		<item>
		<title>Caught, time and again</title>
		<link>http://yavin4.anshul.info/2006/10/18/caught-time-and-again/</link>
		<comments>http://yavin4.anshul.info/2006/10/18/caught-time-and-again/#comments</comments>
		<pubDate>Wed, 18 Oct 2006 10:57:51 +0000</pubDate>
		<dc:creator>Anshul</dc:creator>
				<category><![CDATA[programming]]></category>
		<category><![CDATA[tech]]></category>

		<guid isPermaLink="false">http://yavin4.anshul.info/2006/10/18/caught-time-and-again/</guid>
		<description><![CDATA[Is the solution to this apparent to you? Given this piece of C++ code: // definitions (a and b are primitive types) a = b+1; if (a == b+1) cout]]></description>
			<content:encoded><![CDATA[<p>Is the solution to this apparent to you?</p>
<p>Given this piece of C++ code:<br />
<code>
<pre>  // definitions (a and b are primitive types)
  a = b+1;
  if (a == b+1) cout<<"True";
  else cout<<"False";
</pre>
<p></code></p>
<p>Under what conditions would the code print "False"?</p>
<p>The answer, when I finally get it, is obvious, but I manage to forget it every time and repeatedly fall into this trap.</p>
]]></content:encoded>
			<wfw:commentRss>http://yavin4.anshul.info/2006/10/18/caught-time-and-again/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
		<item>
		<title>Playing God with Scheme</title>
		<link>http://yavin4.anshul.info/2006/08/10/playing-god-with-scheme/</link>
		<comments>http://yavin4.anshul.info/2006/08/10/playing-god-with-scheme/#comments</comments>
		<pubDate>Thu, 10 Aug 2006 10:02:39 +0000</pubDate>
		<dc:creator>Anshul</dc:creator>
				<category><![CDATA[fun]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[Uncategorized]]></category>

		<guid isPermaLink="false">http://yavin4.anshul.info/2006/08/10/playing-god-with-scheme/</guid>
		<description><![CDATA[Pretty sensitive issue this &#8211; whether man should be allowed to play God or not. I was a debater once, and I&#8217;ve pretty much debated this with dozens of arguments from each side. One thing you can&#8217;t argue with, though: playing God is fun. I was playing around with Scheme today (for the uninitiated and/or [...]]]></description>
			<content:encoded><![CDATA[<p>Pretty sensitive issue this &#8211; whether man should be allowed to play God or not. I was a debater once, and I&#8217;ve pretty much debated this with dozens of arguments from each side. One thing you can&#8217;t argue with, though: playing God is fun.</p>
<p>I was playing around with Scheme today (for the uninitiated and/or the young, that is a programming language). I was pretty uninitiated too, which is why I had never bothered to try out Scheme before&#8230; but this is the closest to playing God that I have come. Here&#8217;s why.</p>
<p><strong>Weird but cool syntax</strong>: Almost everything in scheme feels like it must have been created around the beginning of time. Scheme is to C++ what Sanskrit is to Hindi. For instance, here&#8217;s how you add two numbers in Scheme:</p>
<p><code>(+ 2 3)<br />
5</code></p>
<p>This seems simple enough to follow at first &#8211; but then you realize everything in Scheme follows this rule &#8211; and so if you want to write an absolute value function, this is how you do it:</p>
<p><code>(define (abs x) (if (< x 0) (- x) x))</code></p>
<p>It all goes downhill from there.</p>
<p><strong>Warp the world</strong>: The scheme book I was reading unwisely told me that anything and everything in Scheme was changeable. I did not read a sentence further and went ahead and wrote this out:</p>
<p><code>(define (+ a b) (* a b))<br />
Setting compiled read-only variable+ can yield to incoherent state<br />
+</code></p>
<p>This basically tells the interpreter that I want addition to mean multiplication. Despite the warning that I was entering a demented world, I was overjoyed that this appeared to work. And, sure enough:</p>
<p><code>(+ 5 10)<br />
50</code></p>
<p><strong>Bending the fabric of reality</strong>: Upward to enlightenment, I realized that + was just a name, and could really point to anything. We often hear these days that "everything is an object". But really, in Scheme, "everything is what you want it to be". And so, after turning my world upside-down, I turned it inside out.</p>
<p><code>(define + 2)<br />
Setting compiled read-only variable+ can yield to incoherent state<br />
+</code></p>
<p>Which says I want + to be a variable with value 2. For an ideal God, of course, the world would just carry out orders without cursing, but hey, I was just playing God! And was indeed bending the world to my will. And so I can now enter this, which looks positively alien, but is perfectly logical and returns a nice round figure:</p>
<p><code>(* + (- + +) (/ (* + +) +))<br />
0</code></p>
<p>By this time, of course, subjects of the now-warped-and-bent-beyond-recognition world were, as you can imagine, feeling like Neanderthal man presented with the supernova of the Crab Nebula (or bash users presented with the Windows command line), and I thought it was about time to un-warp the world again. Even without restarting the interpreter, this is quite easily done:</p>
<p><code>(define (+ a b) (- a (- b)))</code></p>
<p>...and there was light :D (though of course the original + procedure can take any number of arguments, I'll figure that out someday)</p>
<p>If you're feeling down, I heartily recommend an episode of playing God to get you feeling on top of the world.</p>
]]></content:encoded>
			<wfw:commentRss>http://yavin4.anshul.info/2006/08/10/playing-god-with-scheme/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>How great minds really think</title>
		<link>http://yavin4.anshul.info/2006/08/02/how-great-minds-really-think/</link>
		<comments>http://yavin4.anshul.info/2006/08/02/how-great-minds-really-think/#comments</comments>
		<pubDate>Wed, 02 Aug 2006 03:36:51 +0000</pubDate>
		<dc:creator>Anshul</dc:creator>
				<category><![CDATA[philosophy]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[tech]]></category>

		<guid isPermaLink="false">http://yavin4.anshul.info/2006/08/02/how-great-minds-really-think/</guid>
		<description><![CDATA[One blogger out there had the amazing idea of e-mailing a few great programmers and asking questions related to coding. He&#8217;s posted the answers, and they&#8217;re definitely worth a read, and probably a lot of thought as well.]]></description>
			<content:encoded><![CDATA[<p>One blogger out there had the amazing idea of e-mailing a few great programmers and asking questions related to coding. He&#8217;s <a href="http://sztywny.titaniumhosting.com/2006/07/23/stiff-asks-great-programmers-answers/">posted the answers</a>, and they&#8217;re definitely worth a read, and probably a lot of thought as well.</p>
]]></content:encoded>
			<wfw:commentRss>http://yavin4.anshul.info/2006/08/02/how-great-minds-really-think/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Thunderbird, etc.</title>
		<link>http://yavin4.anshul.info/2006/04/21/thunderbird-etc/</link>
		<comments>http://yavin4.anshul.info/2006/04/21/thunderbird-etc/#comments</comments>
		<pubDate>Fri, 21 Apr 2006 06:52:12 +0000</pubDate>
		<dc:creator>Anshul</dc:creator>
				<category><![CDATA[fun]]></category>
		<category><![CDATA[mac]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[tech]]></category>

		<guid isPermaLink="false">http://yavin4.anshul.info/2006/04/21/thunderbird-etc/</guid>
		<description><![CDATA[I have been looking to a way to link Thunderbird and Growl for showing me mail notification messages for a while now. Thanks to this post, I was able to get it working today. One issue I faced that I have four mail accounts in Thunderbird and I don&#8217;t care about three of them, but [...]]]></description>
			<content:encoded><![CDATA[<p>I have been looking to a way to link Thunderbird and Growl for showing me mail notification messages for a while now. Thanks to <a href="http://www.neilturner.me.uk/2005/Nov/04/growl_and_thunderbird.html">this post</a>, I was able to get it working today.</p>
<p><a href="http://www.flickr.com/photos/anshul/132259889/" title="Photo Sharing"><img src="http://static.flickr.com/43/132259889_fbafaad5aa_o.png" width="348" height="159" alt="Growl for thunderbird" /></a></p>
<p>One issue I faced that I have four mail accounts in Thunderbird and I don&#8217;t care about three of them, but by default the above hack gives a notification for all messages. Thankfully, the back end controlling the notification is a good old bash shell script. After a little hacking, all was well.</p>
<p>To digress a bit, I would like to propose the following theorem.</p>
<blockquote><p>You will always need to code a shell script right after you&#8217;ve forgotten the syntax of the if and for statements.</p>
<p>Proof: Emprical story of my life since I wrote my first shell script.</p></blockquote>
<p>Thankfully, there is a corollary:</p>
<blockquote><p>Every possible shell script in bash has already been written (and indexed by Google).</p>
<p>Proof: See above.</p></blockquote>
<p>Back to Thunderbird. Thunderbird has had Junk mail filtering for a while now, and occasionally it will interpret some messages as &#8220;Junk&#8221; and give you a button to indicate that it&#8217;s &#8220;Not Junk&#8221;. Today, I saw this:</p>
<p><a href="http://www.flickr.com/photos/anshul/132259890/" title="Photo Sharing"><img src="http://static.flickr.com/45/132259890_9e91a15812.jpg" width="500" height="301" alt="Thunderbird scam protection?" /></a></p>
<p>Interesting to note that there&#8217;s a difference between &#8220;scam&#8221; and &#8220;junk&#8221; mail detection. Even more interesting to know that Thunderbird doesn&#8217;t particularly care much about NUS Business School!</p>
]]></content:encoded>
			<wfw:commentRss>http://yavin4.anshul.info/2006/04/21/thunderbird-etc/feed/</wfw:commentRss>
		<slash:comments>2</slash:comments>
		</item>
	</channel>
</rss>

