<?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>Salsita Software</title>
	<atom:link href="http://www.salsitasoft.com/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.salsitasoft.com</link>
	<description>Browser Plugins &#38; JavaScript Development</description>
	<lastBuildDate>Thu, 09 May 2013 08:00:49 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.4.2</generator>
		<item>
		<title>Programming Brain Teaser #6: Fixed-point principle</title>
		<link>http://www.salsitasoft.com/2013/05/09/programming-brain-teaser-6-fixed-point-principle/</link>
		<comments>http://www.salsitasoft.com/2013/05/09/programming-brain-teaser-6-fixed-point-principle/#comments</comments>
		<pubDate>Thu, 09 May 2013 08:00:49 +0000</pubDate>
		<dc:creator>Roman Kaspar</dc:creator>
				<category><![CDATA[Puzzles]]></category>

		<guid isPermaLink="false">http://www.salsitasoft.com/?p=2749</guid>
		<description><![CDATA[<p>As promised last time, let&#8217;s do something more serious today&#8230; Let a be any expression. We will call x the fixed point of expression a if x names ax. (Reminder: lower-case letters in our programming language represent a combination of upper-case letters.) The fixed-point principle says that for each expression a there exists its fixed point x. [...]</p><p>The post <a href="http://www.salsitasoft.com/2013/05/09/programming-brain-teaser-6-fixed-point-principle/">Programming Brain Teaser #6: Fixed-point principle</a> appeared first on <a href="http://www.salsitasoft.com">Salsita Software</a>.</p>]]></description>
			<content:encoded><![CDATA[<p>As promised last time, let&#8217;s do something more serious today&#8230;</p>
<p>Let <strong><em>a</em></strong> be any expression.<strong><strong> We will call <strong><em>x</em></strong> the fixed point of expression <strong><em>a</em></strong> if <strong><em>x</em></strong> names <strong><em>ax</em></strong>. </strong></strong>(Reminder: lower-case letters in our programming language represent a combination of upper-case letters.)</p>
<p>The fixed-point principle says that for each expression <strong><em>a</em></strong> there exists its fixed point <strong><em>x</em></strong>. Furthermore there is a (quite simple) &#8220;manual&#8221; for constructing the fixed point <strong><em>x</em></strong> of any expression <strong><em>a</em><strong>.</strong></strong></p>
<p>So the question of the day is: <span style="background-color: #ddf;">can you find that &#8220;manual&#8221; proving fixed-point principle?</span><br />
(E.g. a program <strong><em>x</em></strong> that names <strong><em>ABCx</em></strong>, for <strong><em>a = ABC</em><strong>.)</strong></strong></p>
<p>&nbsp;</p>
<h2>Solution</h2>

<div class='easySpoilerWrapper' style=''>
<table class='easySpoilerTable' border='0' style='text-align:center;' align='center' bgcolor='FFFFFF' >

<tr style='white-space:normal;'>
<th class='easySpoilerTitleA' style='white-space:normal;font-weight:normal;text-align:left;vertical-align:middle;font-size:120%;color:#000000;'>To see the correct solution, click 'Show' button...</th>
<th class='easySpoilerTitleB'style='text-align:right;vertical-align:middle;font-size:100%; white-space:nowrap;'><a href='' onclick='wpSpoilerSelect("spoilerDiv51138001"); return false;' class='easySpoilerButtonOther' style='font-size:100%;color:#000000;background-color:#fcfcfc;background-image:none;border: 1px inset;border-style:solid;border-color:#cccccc; margin: 3px 0px 3px; padding: 4px; ' align='right'>Select</a><a href='' onclick='wpSpoilerToggle("spoilerDiv51138001",true,"Show","Hide","fast",false); return false;' id='spoilerDiv51138001_action' class='easySpoilerButton' value="Show" align='right' style='font-size:100%;color:#000000;background-color:#fcfcfc;background-image:none;border: 1px inset;border-style:solid;border-color:#cccccc; margin: 3px 0px 3px 5px; padding: 4px;'>Show</></th>
</tr>
<tr><td class='easySpoilerRow' colspan='2' style=''><div id='spoilerDiv51138001' class='easySpoilerSpoils'  style='display:none; white-space:wrap; overflow:auto; vertical-align:middle;'>
</p>
<p>Answer: <span style="background-color: #ddf;"><strong><em>x = RQaRQ</em></strong></span><br />
Why? Because that names <strong><em>aRQaRQ</em></strong>, which is <strong><em>ax</em></strong>, which is exactly what we wanted <img src='http://www.salsitasoft.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> .</p>
<p>
</div></td></tr>
</table>
<div class='easySpoilerConclude' style=''><table class='easySpoilerTable' border='0' style='text-align:center;' frame='box' align='center' bgcolor='FFFFFF'><tr><th class='easySpoilerEnd' style='width:100%;'></th><td class='easySpoilerEnd' style='white-space:nowrap;' colspan='2'></td></tr><tr><td class='easySpoilerGroupWrapperLastRow' colspan='2' style=''></td></tr></table></div>
</div>

<p>The post <a href="http://www.salsitasoft.com/2013/05/09/programming-brain-teaser-6-fixed-point-principle/">Programming Brain Teaser #6: Fixed-point principle</a> appeared first on <a href="http://www.salsitasoft.com">Salsita Software</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.salsitasoft.com/2013/05/09/programming-brain-teaser-6-fixed-point-principle/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Programming Brain Teaser #5: More curiosities</title>
		<link>http://www.salsitasoft.com/2013/04/23/programming-brain-teaser-5-more-curiosities/</link>
		<comments>http://www.salsitasoft.com/2013/04/23/programming-brain-teaser-5-more-curiosities/#comments</comments>
		<pubDate>Tue, 23 Apr 2013 12:34:21 +0000</pubDate>
		<dc:creator>Roman Kaspar</dc:creator>
				<category><![CDATA[Puzzles]]></category>

		<guid isPermaLink="false">http://www.salsitasoft.com/?p=2740</guid>
		<description><![CDATA[<p>Next time, we&#8217;ll explore more serious aspect of our programming language, but let&#8217;s have some fun for today finding expressions (programs) with strange properties, similar to what we did last time (but a bit more difficult today). As usual, we&#8217;ll use only Q and R rules (described here). Question 1: Find expressions x so that [...]</p><p>The post <a href="http://www.salsitasoft.com/2013/04/23/programming-brain-teaser-5-more-curiosities/">Programming Brain Teaser #5: More curiosities</a> appeared first on <a href="http://www.salsitasoft.com">Salsita Software</a>.</p>]]></description>
			<content:encoded><![CDATA[<p>Next time, we&#8217;ll explore more serious aspect of our programming language, but let&#8217;s have some fun for today finding expressions (programs) with strange properties, similar to what we did last time (but a bit more difficult today).</p>
<p>As usual, we&#8217;ll use only <strong><em>Q</em></strong> and <em><strong>R</strong></em> rules (described <a href="http://www.salsitasoft.com/2013/02/25/programming-brain-teaser-1-getting-started/">here</a>).</p>
<p>Question 1: Find expressions <strong><em>x</em></strong> so that <span style="background-color: #ddf;"><em><strong>RQx</strong></em> names (resolves to) <em><strong>QRx</strong></em></span>.</p>
<p>Question 2: &#8230; <span style="background-color: #ddf;"><em><strong>RRx &#8211;&gt; QQx</strong></em></span></p>
<p>Question 3: &#8230; <span style="background-color: #ddf;"><em><strong>RQx &#8211;&gt; RRx</strong></em></span></p>
<p>(You can check how previous riddles were solved. This could help a lot in case you don&#8217;t know how to approach solving this.)</p>
<p>&nbsp;</p>
<h2>Solution</h2>

<div class='easySpoilerWrapper' style=''>
<table class='easySpoilerTable' border='0' style='text-align:center;' align='center' bgcolor='FFFFFF' >

<tr style='white-space:normal;'>
<th class='easySpoilerTitleA' style='white-space:normal;font-weight:normal;text-align:left;vertical-align:middle;font-size:120%;color:#000000;'>To see the correct solution, click 'Show' button...</th>
<th class='easySpoilerTitleB'style='text-align:right;vertical-align:middle;font-size:100%; white-space:nowrap;'><a href='' onclick='wpSpoilerSelect("spoilerDiv1b0f8002"); return false;' class='easySpoilerButtonOther' style='font-size:100%;color:#000000;background-color:#fcfcfc;background-image:none;border: 1px inset;border-style:solid;border-color:#cccccc; margin: 3px 0px 3px; padding: 4px; ' align='right'>Select</a><a href='' onclick='wpSpoilerToggle("spoilerDiv1b0f8002",true,"Show","Hide","fast",false); return false;' id='spoilerDiv1b0f8002_action' class='easySpoilerButton' value="Show" align='right' style='font-size:100%;color:#000000;background-color:#fcfcfc;background-image:none;border: 1px inset;border-style:solid;border-color:#cccccc; margin: 3px 0px 3px 5px; padding: 4px;'>Show</></th>
</tr>
<tr><td class='easySpoilerRow' colspan='2' style=''><div id='spoilerDiv1b0f8002' class='easySpoilerSpoils'  style='display:none; white-space:wrap; overflow:auto; vertical-align:middle;'>
</p>
<p><span style="background-color: #ddf;"><em><strong>Q1: x=QR</strong></em></span></p>
<p><span style="background-color: #ddf;"><em><strong>Q2: x=QQ</strong></em></span></p>
<p><span style="background-color: #ddf;"><em><strong>Q3: x=RR</strong></em></span></p>
<p>By now you should be familiar with the programming language enough to verify the answers yourselves. <img src='http://www.salsitasoft.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>
</div></td></tr>
</table>
<div class='easySpoilerConclude' style=''><table class='easySpoilerTable' border='0' style='text-align:center;' frame='box' align='center' bgcolor='FFFFFF'><tr><th class='easySpoilerEnd' style='width:100%;'></th><td class='easySpoilerEnd' style='white-space:nowrap;' colspan='2'></td></tr><tr><td class='easySpoilerGroupWrapperLastRow' colspan='2' style=''></td></tr></table></div>
</div>

<p>The post <a href="http://www.salsitasoft.com/2013/04/23/programming-brain-teaser-5-more-curiosities/">Programming Brain Teaser #5: More curiosities</a> appeared first on <a href="http://www.salsitasoft.com">Salsita Software</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.salsitasoft.com/2013/04/23/programming-brain-teaser-5-more-curiosities/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Programming Brain Teaser #4: More Rx riddles</title>
		<link>http://www.salsitasoft.com/2013/04/09/programming-brain-teaser-4-more-rx-riddles/</link>
		<comments>http://www.salsitasoft.com/2013/04/09/programming-brain-teaser-4-more-rx-riddles/#comments</comments>
		<pubDate>Tue, 09 Apr 2013 07:45:57 +0000</pubDate>
		<dc:creator>Roman Kaspar</dc:creator>
				<category><![CDATA[Puzzles]]></category>

		<guid isPermaLink="false">http://www.salsitasoft.com/?p=2726</guid>
		<description><![CDATA[<p>Today, we&#8217;ll do something easier for change (but you&#8217;ll need to answer two questions). Again, we&#8217;ll use only two rules R and Q described here.. Question 1: For what statement x the following is true: Rx = x Question 2: For what statement x the following is true: Rx = Qx &#160; Solution</p><p>The post <a href="http://www.salsitasoft.com/2013/04/09/programming-brain-teaser-4-more-rx-riddles/">Programming Brain Teaser #4: More Rx riddles</a> appeared first on <a href="http://www.salsitasoft.com">Salsita Software</a>.</p>]]></description>
			<content:encoded><![CDATA[<p>Today, we&#8217;ll do something easier for change (but you&#8217;ll need to answer two questions). Again, we&#8217;ll use only two rules <em><strong>R</strong></em> and <em><strong>Q</strong></em> described <a href="http://www.salsitasoft.com/2013/02/25/programming-brain-teaser-1-getting-started/">here</a>..</p>
<p>Question 1:<br />
For what statement <strong><em>x</em></strong> the following is true:<br />
<span style="background-color: #ddf;"><em><strong>Rx = x</strong></em></span></p>
<p>Question 2:<br />
For what statement <strong><em>x</em></strong> the following is true:<br />
<span style="background-color: #ddf;"><em><strong>Rx = Qx</strong></em></span></p>
<p>&nbsp;</p>
<h2>Solution</h2>

<div class='easySpoilerWrapper' style=''>
<table class='easySpoilerTable' border='0' style='text-align:center;' align='center' bgcolor='FFFFFF' >

<tr style='white-space:normal;'>
<th class='easySpoilerTitleA' style='white-space:normal;font-weight:normal;text-align:left;vertical-align:middle;font-size:120%;color:#000000;'>To see the correct solution, click 'Show' button...</th>
<th class='easySpoilerTitleB'style='text-align:right;vertical-align:middle;font-size:100%; white-space:nowrap;'><a href='' onclick='wpSpoilerSelect("spoilerDiv1ca08003"); return false;' class='easySpoilerButtonOther' style='font-size:100%;color:#000000;background-color:#fcfcfc;background-image:none;border: 1px inset;border-style:solid;border-color:#cccccc; margin: 3px 0px 3px; padding: 4px; ' align='right'>Select</a><a href='' onclick='wpSpoilerToggle("spoilerDiv1ca08003",true,"Show","Hide","fast",false); return false;' id='spoilerDiv1ca08003_action' class='easySpoilerButton' value="Show" align='right' style='font-size:100%;color:#000000;background-color:#fcfcfc;background-image:none;border: 1px inset;border-style:solid;border-color:#cccccc; margin: 3px 0px 3px 5px; padding: 4px;'>Show</></th>
</tr>
<tr><td class='easySpoilerRow' colspan='2' style=''><div id='spoilerDiv1ca08003' class='easySpoilerSpoils'  style='display:none; white-space:wrap; overflow:auto; vertical-align:middle;'>
</p>
<p><span style="background-color: #ddf;"><em><strong>Rx = x</strong></em></span> works for <span style="background-color: #ddf;"><em><strong>x = QQ</strong></em></span></p>
<p>How to figure this out?</p>
<p>As usual, take all possible programs in our programming language (i.e. <em><strong>Qx, RQx, RRQx, R&#8230;RQx</strong></em>) and see if some pattern works.</p>
<p>Let&#8217;s start with first pattern: <em><strong>x = Qy</strong></em><br />
<em><strong>Rx = RQy</strong></em> (using <em><strong>x = Qy</strong></em>)<br />
<em><strong>RQy = yy</strong></em> (from definition of rules <em><strong>Q</strong></em> and <em><strong>R</strong></em> of the language, works for any <em><strong>y</strong></em>)<br />
<em><strong>yy = Qy</strong></em> (we want <em><strong>Rx = x</strong></em>, and <em><strong>x = Qy</strong></em>)<br />
&#8230; since <em><strong>y = y</strong></em> for any <em><strong>y</strong></em>, it is sufficient now to say that <em><strong>y = Q</strong></em>.<br />
And the <em><strong>x</strong></em> we were asking for is then <em><strong>Qy</strong></em>, so we get <em><strong>x = QQ</strong></em>.</p>
<p><span style="background-color: #ddf;"><em><strong>Rx = Qx</strong></em></span> is true for <span style="background-color: #ddf;"><em><strong>x = QQQ</strong></em></span></p>
<p>How to figure this one out?<br />
&#8230; the same way as the one before <img src='http://www.salsitasoft.com/wp-includes/images/smilies/icon_smile.gif' alt=':-)' class='wp-smiley' /> </p>
<p>
</div></td></tr>
</table>
<div class='easySpoilerConclude' style=''><table class='easySpoilerTable' border='0' style='text-align:center;' frame='box' align='center' bgcolor='FFFFFF'><tr><th class='easySpoilerEnd' style='width:100%;'></th><td class='easySpoilerEnd' style='white-space:nowrap;' colspan='2'></td></tr><tr><td class='easySpoilerGroupWrapperLastRow' colspan='2' style=''></td></tr></table></div>
</div>

<p>The post <a href="http://www.salsitasoft.com/2013/04/09/programming-brain-teaser-4-more-rx-riddles/">Programming Brain Teaser #4: More Rx riddles</a> appeared first on <a href="http://www.salsitasoft.com">Salsita Software</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.salsitasoft.com/2013/04/09/programming-brain-teaser-4-more-rx-riddles/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Programming Brain Teaser #3: Repetition riddle</title>
		<link>http://www.salsitasoft.com/2013/03/26/programming-brain-teaser-3-repetition-riddle/</link>
		<comments>http://www.salsitasoft.com/2013/03/26/programming-brain-teaser-3-repetition-riddle/#comments</comments>
		<pubDate>Tue, 26 Mar 2013 08:58:28 +0000</pubDate>
		<dc:creator>Roman Kaspar</dc:creator>
				<category><![CDATA[Puzzles]]></category>

		<guid isPermaLink="false">http://www.salsitasoft.com/?p=2720</guid>
		<description><![CDATA[<p>Today, we&#8217;ll explore one interesting aspect of the repetition rule of our programming language. To solve the puzzle, we&#8217;ll use the same two rules as last time, the Q rule (quotation) and the R rule (repetition). In case you need to refresh your memory, the rules are described here. From the rules definition you know [...]</p><p>The post <a href="http://www.salsitasoft.com/2013/03/26/programming-brain-teaser-3-repetition-riddle/">Programming Brain Teaser #3: Repetition riddle</a> appeared first on <a href="http://www.salsitasoft.com">Salsita Software</a>.</p>]]></description>
			<content:encoded><![CDATA[<p>Today, we&#8217;ll explore one interesting aspect of the repetition rule of our programming language. To solve the puzzle, we&#8217;ll use the same two rules as last time, the <em><strong>Q</strong></em> rule (quotation) and the <em><strong>R</strong></em> rule (repetition). In case you need to refresh your memory, the rules are described <a href="http://www.salsitasoft.com/2013/02/25/programming-brain-teaser-1-getting-started/">here</a>.</p>
<p>From the rules definition you know that for any statement <strong><em>x</em></strong> the following is true:<br />
<em><strong>RQx = xx</strong></em>.</p>
<p>But is there such a statement <em><strong>x</strong></em> for which the following is true?:<br />
<span style="background-color: #ddf;"><em><strong>Rx = xx</strong></em></span>.</p>
<p>&nbsp;</p>
<h2>Solution</h2>

<div class='easySpoilerWrapper' style=''>
<table class='easySpoilerTable' border='0' style='text-align:center;' align='center' bgcolor='FFFFFF' >

<tr style='white-space:normal;'>
<th class='easySpoilerTitleA' style='white-space:normal;font-weight:normal;text-align:left;vertical-align:middle;font-size:120%;color:#000000;'>To see the correct solution, click 'Show' button...</th>
<th class='easySpoilerTitleB'style='text-align:right;vertical-align:middle;font-size:100%; white-space:nowrap;'><a href='' onclick='wpSpoilerSelect("spoilerDiv737e8004"); return false;' class='easySpoilerButtonOther' style='font-size:100%;color:#000000;background-color:#fcfcfc;background-image:none;border: 1px inset;border-style:solid;border-color:#cccccc; margin: 3px 0px 3px; padding: 4px; ' align='right'>Select</a><a href='' onclick='wpSpoilerToggle("spoilerDiv737e8004",true,"Show","Hide","fast",false); return false;' id='spoilerDiv737e8004_action' class='easySpoilerButton' value="Show" align='right' style='font-size:100%;color:#000000;background-color:#fcfcfc;background-image:none;border: 1px inset;border-style:solid;border-color:#cccccc; margin: 3px 0px 3px 5px; padding: 4px;'>Show</></th>
</tr>
<tr><td class='easySpoilerRow' colspan='2' style=''><div id='spoilerDiv737e8004' class='easySpoilerSpoils'  style='display:none; white-space:wrap; overflow:auto; vertical-align:middle;'>
</p>
<p>The correct answer is: <span style="background-color: #ddf;"><em><strong>RQRQ</strong></em></span></p>
<p>To verify: <em><strong>Rx = RRQRQ = RR(Q(RQ)) = 2*2*(RQ) = 2*(RQRQ) = RQRQRQRQ = (RQRQ)(RQRQ) = xx</strong></em></p>
<p>&#8230; the parentheses and numeral 2 and multiplication sign (*) are <em>not</em> part of the language, they are just used for clarity.</p>
<p>OK, now the question: how shall I figure this out (well, guessing is also a method, but let&#8217;s be more deterministic)?</p>
<p>So think about <em>x</em> and how it should look like.</p>
<p>Let&#8217;s start (as always), with idea of <em><strong>x = Qy</strong></em><br />
Then <em><strong>Rx = RQy = yy</strong></em>.<br />
This doesn&#8217;t work since <em><strong>x</strong></em> is not equal to <em><strong>y</strong></em> (<em><strong>x</strong></em> is always one letter &#8212; the letter &#8220;Q&#8221; &#8212; longer).</p>
<p>OK, let&#8217;s explore the idea of <em><strong>x = RQy</strong></em><br />
Now <em><strong>Rx = RRQy = yyyy</strong></em>.<br />
Since we need <em><strong>Rx = xx</strong></em>, and we get <em><strong>Rx = yyyy</strong></em>, this can actually work if <em><strong>x = yy</strong></em>.<br />
We started with <em><strong>x = RQy</strong></em>, and need <em><strong>x = yy</strong></em>.<br />
Therefore <em><strong>RQy = yy</strong></em>.<br />
Looks familiar, right?<br />
This works for <em><strong>y = RQ</strong></em>.</p>
<p>So the <em><strong>x</strong></em> we were looking for is <em><strong>RQy = RQRQ</strong></em>.</p>
<p>
</div></td></tr>
</table>
<div class='easySpoilerConclude' style=''><table class='easySpoilerTable' border='0' style='text-align:center;' frame='box' align='center' bgcolor='FFFFFF'><tr><th class='easySpoilerEnd' style='width:100%;'></th><td class='easySpoilerEnd' style='white-space:nowrap;' colspan='2'></td></tr><tr><td class='easySpoilerGroupWrapperLastRow' colspan='2' style=''></td></tr></table></div>
</div>

<p>The post <a href="http://www.salsitasoft.com/2013/03/26/programming-brain-teaser-3-repetition-riddle/">Programming Brain Teaser #3: Repetition riddle</a> appeared first on <a href="http://www.salsitasoft.com">Salsita Software</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.salsitasoft.com/2013/03/26/programming-brain-teaser-3-repetition-riddle/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Programming Brain Teaser #2: Self-reference</title>
		<link>http://www.salsitasoft.com/2013/03/12/programming-brain-teaser-2-self-reference/</link>
		<comments>http://www.salsitasoft.com/2013/03/12/programming-brain-teaser-2-self-reference/#comments</comments>
		<pubDate>Tue, 12 Mar 2013 17:19:51 +0000</pubDate>
		<dc:creator>Roman Kaspar</dc:creator>
				<category><![CDATA[Puzzles]]></category>

		<guid isPermaLink="false">http://www.salsitasoft.com/?p=2703</guid>
		<description><![CDATA[<p>Let&#8217;s continue where we stopped last time with our robot programming language. We&#8217;ll need only two rules today, the Q rule (quotation) and the R rule (repetition). Should you need to refresh your memory, the rules are described here. The brain-teaser of the day is: can you find statement x that names itself (i.e. &#8220;resolves [...]</p><p>The post <a href="http://www.salsitasoft.com/2013/03/12/programming-brain-teaser-2-self-reference/">Programming Brain Teaser #2: Self-reference</a> appeared first on <a href="http://www.salsitasoft.com">Salsita Software</a>.</p>]]></description>
			<content:encoded><![CDATA[<p>Let&#8217;s continue where we stopped last time with our robot programming language. We&#8217;ll need only two rules today, the <em><strong>Q</strong></em> rule (quotation) and the <em><strong>R</strong></em> rule (repetition). Should you need to refresh your memory, the rules are described <a href="http://www.salsitasoft.com/2013/02/25/programming-brain-teaser-1-getting-started/">here</a>.</p>
<p>The brain-teaser of the day is: <span style="background-color: #ddf;">can you find statement <em><strong>x</strong></em> that names itself (i.e. &#8220;resolves to string&#8221; <em><strong>x</strong></em>)?</span></p>
<p>Are there more of them or just one? Find the self-reference statement pattern or explain why there is no such pattern and only one self-referencing statement exists in this programming language.</p>
<p>Side note: this question is similar to what is in computer science known as quine. It is a program that has no input and prints its own source code on its output. After solving the brain-teaser here, you can apply the same idea and create a quine in your favorite programming language.</p>
<p>&nbsp;</p>
<h2>Solution</h2>

<div class='easySpoilerWrapper' style=''>
<table class='easySpoilerTable' border='0' style='text-align:center;' align='center' bgcolor='FFFFFF' >

<tr style='white-space:normal;'>
<th class='easySpoilerTitleA' style='white-space:normal;font-weight:normal;text-align:left;vertical-align:middle;font-size:120%;color:#000000;'>To see the correct solution, click 'Show' button...</th>
<th class='easySpoilerTitleB'style='text-align:right;vertical-align:middle;font-size:100%; white-space:nowrap;'><a href='' onclick='wpSpoilerSelect("spoilerDiv79838005"); return false;' class='easySpoilerButtonOther' style='font-size:100%;color:#000000;background-color:#fcfcfc;background-image:none;border: 1px inset;border-style:solid;border-color:#cccccc; margin: 3px 0px 3px; padding: 4px; ' align='right'>Select</a><a href='' onclick='wpSpoilerToggle("spoilerDiv79838005",true,"Show","Hide","fast",false); return false;' id='spoilerDiv79838005_action' class='easySpoilerButton' value="Show" align='right' style='font-size:100%;color:#000000;background-color:#fcfcfc;background-image:none;border: 1px inset;border-style:solid;border-color:#cccccc; margin: 3px 0px 3px 5px; padding: 4px;'>Show</></th>
</tr>
<tr><td class='easySpoilerRow' colspan='2' style=''><div id='spoilerDiv79838005' class='easySpoilerSpoils'  style='display:none; white-space:wrap; overflow:auto; vertical-align:middle;'>
</p>
<p>The correct answer is <span style="background-color: #ddf;"><em><strong>RQRQ</strong></em></span>.</p>
<p>Also, it is the only self-referencing statement in our programming language.</p>
<p><strong>Why?</strong><br />
Any possible statement that names something must use one of the following patterns:</p>
<ul>
<li><em><strong>Qx</strong></em></li>
<li><em><strong>RQx</strong></em></li>
<li><em><strong>RRQx</strong></em></li>
<li><em><strong>RRRQx</strong></em></li>
<li><em><strong>R&#8230;RQx</strong></em></li>
</ul>
<p>First of all, self-referencing statement cannot be of form <em><strong>Qx</strong></em>, because it names <em><strong>x</strong></em>, and these two statements differ in length (by 1, the length of <em><strong>Q</strong></em>). I.e.   | <em><strong>Qx</strong></em> |   !=   | <em><strong>x</strong></em> |.</p>
<p>Let&#8217;s take <em><strong>RQx</strong></em> then.<br />
If | <em><strong>x</strong></em> | = 1 , the result will be of length 2, but | <em><strong>RQx</strong></em> | = 3 . And since 3 != 2, this won&#8217;t work.<br />
If | <em><strong>x</strong></em> | = 2 , the result will be of length 4, and | <em><strong>RQx</strong></em> | will be also of length 4. So then <em><strong>RQx = xx</strong></em>. This works for <em><strong>x = RQ</strong></em>, and we get our self-referencing statement <em><strong>RQRQ</strong></em>.<br />
If | <em><strong>x</strong></em> | &gt; 2 , the result will always be longer than | <em><strong>RQx</strong></em> |.</p>
<p>Now, let&#8217;s take <em><strong>RRQx</strong></em> in turn.<br />
If | <em><strong>x</strong></em> | = 1 , the result will be of length 4, and | <em><strong>RRQx</strong></em> | = 4 . Here we get <em><strong>RRQx = xxxx</strong></em>, and for this to work, <em><strong>x</strong></em> would have to be <em><strong>R</strong></em> and <em><strong>Q</strong></em> at the same time. Fails.<br />
If | <em><strong>x</strong></em> | &gt; 1, the result will always be longer than | <em><strong>RRQx</strong></em> |.</p>
<p>For <em><strong>R&#8230;RQx</strong></em> and statements with three or more <em><strong>R&#8217;s</strong></em> at the beginning, the resulting statement is always longer than the length of &#8220;program&#8221; creating that particular statement.</p>
<p>All in all, <span style="background-color: #ddf;">there is just one self-referencing statement</span>, and that statement is <em><strong>RQRQ</strong></em>.</p>
<p>Side note: and yes, the idea behind quine program in your favorite programing language is the same: take part of your program, quote it (make it a string out of it), and print it twice.</p>
<p>
</div></td></tr>
</table>
<div class='easySpoilerConclude' style=''><table class='easySpoilerTable' border='0' style='text-align:center;' frame='box' align='center' bgcolor='FFFFFF'><tr><th class='easySpoilerEnd' style='width:100%;'></th><td class='easySpoilerEnd' style='white-space:nowrap;' colspan='2'></td></tr><tr><td class='easySpoilerGroupWrapperLastRow' colspan='2' style=''></td></tr></table></div>
</div>

<p>The post <a href="http://www.salsitasoft.com/2013/03/12/programming-brain-teaser-2-self-reference/">Programming Brain Teaser #2: Self-reference</a> appeared first on <a href="http://www.salsitasoft.com">Salsita Software</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.salsitasoft.com/2013/03/12/programming-brain-teaser-2-self-reference/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Programming Brain Teaser #1: Getting Started</title>
		<link>http://www.salsitasoft.com/2013/02/25/programming-brain-teaser-1-getting-started/</link>
		<comments>http://www.salsitasoft.com/2013/02/25/programming-brain-teaser-1-getting-started/#comments</comments>
		<pubDate>Mon, 25 Feb 2013 08:53:25 +0000</pubDate>
		<dc:creator>Roman Kaspar</dc:creator>
				<category><![CDATA[Puzzles]]></category>

		<guid isPermaLink="false">http://www.salsitasoft.com/?p=2622</guid>
		<description><![CDATA[<p>With this post we start a series of brain teasers about logical programming. The stories are taken from Raymond Smullyan&#8217;s book Satan, Cantor and Infinity (an excellent book, by the way, worth reading for sure!). The correct solution to each riddle is posted here directly, so you can verify your answer right away. The estimated [...]</p><p>The post <a href="http://www.salsitasoft.com/2013/02/25/programming-brain-teaser-1-getting-started/">Programming Brain Teaser #1: Getting Started</a> appeared first on <a href="http://www.salsitasoft.com">Salsita Software</a>.</p>]]></description>
			<content:encoded><![CDATA[<p>With this post we start a series of brain teasers about logical programming. The stories are taken from Raymond Smullyan&#8217;s book <a href="http://www.amazon.com/Satan-Cantor-Infinity-Mind-Boggling-Puzzles/dp/0679406883"><em>Satan, Cantor and Infinity</em></a> (an excellent book, by the way, worth reading for sure!). The correct solution to each riddle is posted here directly, so you can verify your answer right away. The estimated time needed to solve each puzzle is about 15 to 30 minutes at the most.</p>
<p>So if you are interested, take your seat and let&#8217;s get started with the first story.</p>
<h2>The isle of robots, cloning program</h2>
<p>Once upon a time, there was an island, which was very noisy. There were plenty of robots buzzing among piles of their own spare parts, busy with creating new robots, fighting each other, meeting their &#8220;friends&#8221;, or planning conspiracies against their &#8220;enemies&#8221;.</p>
<p>Each robot had a label written on its chest. At first glance, one could say that the string was some sort of identification, but it turned out that it actually was a program telling the robot what exactly to do.</p>
<p>There were many different kinds of robots (very likely as many as programming languages), we&#8217;ll check a few of them.</p>
<p>Let&#8217;s start the discussion about the first programming system with the following definitions:</p>
<ul>
<li>A <em>term</em> is a sequence of (any number of) CAPITAL letters, e.g. <em><strong>LLAZBPWS</strong></em>.</li>
<li>Lower case letters (e.g. <em><strong>x</strong></em>, <em><strong>y</strong></em>, <em><strong>z</strong></em>, <em><strong>a</strong></em>, <em><strong>b</strong></em>, <em><strong>c</strong></em>, &#8230;) are used as <em>shortcuts</em> for terms, e.g. we can say that <em><strong>x</strong></em> represents <em><strong>ABC</strong></em>, or that <em><strong>y</strong></em> stands for <em><strong>DEFGHI</strong></em>.</li>
</ul>
<p>We can combine terms with shortcuts, to get the final term, we substitute all lower case letters with what they represent. For example, using the definitions for <em><strong>x</strong></em> and <em><strong>y</strong></em> above, the statement <em><strong>AxC</strong></em> corresponds to term <em><strong>AABCC</strong></em>, and statement <em><strong>LxQy</strong></em> evaluates to term <em><strong>LABCQDEFGHI</strong></em>.</p>
<p>In the programming system we are about to explore now, certain selected statements are used as names for other statements. The system contains two rules for naming the terms:</p>
<ul>
<li><em><strong><span style="background-color: #fdd;">Rule Q (quotation)</span></strong></em>: For every statement <em><strong>x</strong></em>, the statement <em><strong>Qx</strong></em> names <em><strong>x</strong></em>.</li>
<li><em><strong><span style="background-color: #fdd;">Rule R (repetition)</span></strong></em>: For every statement <em><strong>x</strong></em> that names <em><strong>y</strong></em>, the statement <em><strong>Rx</strong></em> names <em><strong>yy</strong></em>.</li>
</ul>
<p>You can imagine that operation <em><strong>Q</strong></em> takes the rest of the statement and creates a quoted string out of it. E.g. <em><strong>QABC &#8211;&gt; &#8220;ABC&#8221;</strong></em>.<br />
The rule <em><strong>R</strong></em> can be thought of as a function that takes a string and creates another string: <em><strong>R &#8220;ABCD&#8221; &#8211;&gt; &#8220;ABCDABCD&#8221;</strong></em>.</p>
<p>But let&#8217;s see some examples in the syntax of the explored programming language now, where the quotation marks are NOT part of the syntax: <em><strong>RQB</strong></em> names <em><strong>BB</strong></em> (because <em><strong>QB</strong></em> names <strong>B</strong>). Similarly, <em><strong>RQBR</strong></em> names <em><strong>BRBR</strong></em> (because <em><strong>QBR</strong></em> names <em><strong>BR</strong></em>). Remember: the terms are (as usual programs in any other programming languages) interpretted from left to right.  That means that the first <em><strong>Q</strong></em> in the term makes the rest of the term a string / an argument for function &#8220;calls&#8221; of the operators to the left.</p>
<p>To complete the initial set of programming rules, let&#8217;s add one rule for creating new robots:</p>
<ul>
<li><em><strong><span style="background-color: #fdd;">Rule C (creation)</span></strong></em>: If <em><strong>x</strong></em> names <em><strong>y</strong></em>, then <em><strong>Cx</strong></em> constructs robot <em><strong>y</strong></em>.</li>
</ul>
<p>Examples: For any statement <em><strong>y</strong></em> we know that robot <em><strong>CQy</strong></em> creates robot <em><strong>y</strong></em> (simply because <em><strong>Qy</strong></em> names <em><strong>y</strong></em>).  And robot <em><strong>CRQA</strong></em> will create a new robot with program <em><strong>AA</strong></em>.</p>
<p>OK, enough definitions and rules, now something for you to solve (using the programming system described above):</p>
<p><span style="background-color: #ddf;">Describe a program of the robot creating its own clone. That is: the resulting program of the newly created robot must be the same as is its creator&#8217;s program.</span></p>
<p>&nbsp;</p>
<h2>Solution</h2>

<div class='easySpoilerWrapper' style=''>
<table class='easySpoilerTable' border='0' style='text-align:center;' align='center' bgcolor='FFFFFF' >

<tr style='white-space:normal;'>
<th class='easySpoilerTitleA' style='white-space:normal;font-weight:normal;text-align:left;vertical-align:middle;font-size:120%;color:#000000;'>To see the correct solution, click 'Show' button...</th>
<th class='easySpoilerTitleB'style='text-align:right;vertical-align:middle;font-size:100%; white-space:nowrap;'><a href='' onclick='wpSpoilerSelect("spoilerDiv2ede8006"); return false;' class='easySpoilerButtonOther' style='font-size:100%;color:#000000;background-color:#fcfcfc;background-image:none;border: 1px inset;border-style:solid;border-color:#cccccc; margin: 3px 0px 3px; padding: 4px; ' align='right'>Select</a><a href='' onclick='wpSpoilerToggle("spoilerDiv2ede8006",true,"Show","Hide","fast",false); return false;' id='spoilerDiv2ede8006_action' class='easySpoilerButton' value="Show" align='right' style='font-size:100%;color:#000000;background-color:#fcfcfc;background-image:none;border: 1px inset;border-style:solid;border-color:#cccccc; margin: 3px 0px 3px 5px; padding: 4px;'>Show</></th>
</tr>
<tr><td class='easySpoilerRow' colspan='2' style=''><div id='spoilerDiv2ede8006' class='easySpoilerSpoils'  style='display:none; white-space:wrap; overflow:auto; vertical-align:middle;'>
</p>
<p>The correct answer is <span style="background-color: #ddf;"><em><strong>CRQCRQ</strong></em></span>.</p>
<p>Why? Let&#8217;s interpret the program from left to right:</p>
<p>First instruction of the program is <em><strong>C</strong></em>.  That means that <em>if</em> the argument is a name, then the program will create a robot with that name as the program for new robot.</p>
<p>Next instruction of the program is <em><strong>R</strong></em>.  This means that <em>if</em> its argument is a name, the <em><strong>R</strong></em> operation returns two copies of the name, as a new name (that would then serve as an argument to previous <em><strong>C</strong></em> operation).</p>
<p>Finally we hit the <strong><em>Q</em></strong> instruction, which names its argument (i.e. to &#8220;CRQ&#8221;).</p>
<p>Now let&#8217;s go backwards from there: the preceding <em><strong>R</strong></em> operation really got a name (&#8220;CRQ&#8221;) as its argument.  That means that it could create two copies of it and &#8220;return&#8221; that as a new name (i.e. as &#8220;CRQCRQ&#8221;) to the very top operator <em><strong>C</strong></em>.  Which then created a new robot with &#8220;CRQCRQ&#8221; as its program.</p>
<p>When you see what the program of the new robot is, and what the original program is, you&#8217;ll find out that they are the same.</p>
<p>So the robot created its own clone.  And when the new robot starts execution of the program, it will create its own clone again. And the robots will be cloning themselves till the end of their days&#8230;</p>
<p>
</div></td></tr>
</table>
<div class='easySpoilerConclude' style=''><table class='easySpoilerTable' border='0' style='text-align:center;' frame='box' align='center' bgcolor='FFFFFF'><tr><th class='easySpoilerEnd' style='width:100%;'></th><td class='easySpoilerEnd' style='white-space:nowrap;' colspan='2'></td></tr><tr><td class='easySpoilerGroupWrapperLastRow' colspan='2' style=''></td></tr></table></div>
</div>

<p>The post <a href="http://www.salsitasoft.com/2013/02/25/programming-brain-teaser-1-getting-started/">Programming Brain Teaser #1: Getting Started</a> appeared first on <a href="http://www.salsitasoft.com">Salsita Software</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.salsitasoft.com/2013/02/25/programming-brain-teaser-1-getting-started/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Going Functional &#8211; Reduce</title>
		<link>http://www.salsitasoft.com/2013/01/12/going-functional-reduce/</link>
		<comments>http://www.salsitasoft.com/2013/01/12/going-functional-reduce/#comments</comments>
		<pubDate>Sat, 12 Jan 2013 17:04:05 +0000</pubDate>
		<dc:creator>Tomas Brambora</dc:creator>
				<category><![CDATA[Technical Articles]]></category>

		<guid isPermaLink="false">http://www.salsitasoft.com/?p=2580</guid>
		<description><![CDATA[<p>Reduce (a.k.a. foldl) is one of my all-time favorite higher-order functions. It&#8217;s kinda sorta the crane stance of the functional style kung-fu: you don&#8217;t want to use it a lot but when you do it can save the day. Every now and then you come across an array and you want to combine its items somehow [...]</p><p>The post <a href="http://www.salsitasoft.com/2013/01/12/going-functional-reduce/">Going Functional &#8211; Reduce</a> appeared first on <a href="http://www.salsitasoft.com">Salsita Software</a>.</p>]]></description>
			<content:encoded><![CDATA[<p>Reduce (a.k.a. foldl) is one of my all-time favorite higher-order functions. It&#8217;s kinda sorta the <a href="http://www.youtube.com/watch?v=pYE4fNQKTs4">crane stance</a> of the functional style kung-fu: you don&#8217;t want to use it a lot but when you do it can save the day.</p>
<p>Every now and then you come across an array and you want to combine its items somehow to compute a value. But the final result depends on the intermediate results of each step so you need to use an acumulator that holds those. And things get messy.</p>
<p>That&#8217;s where reduce shines.</p>
<p>You give it an array, a function and an initial value and it builds up a return value for you by recursively applying the function to each of the items while passing in the intermediate result.</p>
<h6>Gee, that sounds complicated</h6>
<p>But it&#8217;s not, really. All that happens is you take the initial value and combine it with the first item of the array to get an intermediate result (let&#8217;s call it <em>result1</em>). Then you you combine <em>result1</em> with the second item of the array and get <em>result2</em>. And so on and so forth until you combine with the last item N and get <em>resultN</em> (which is the final one). That&#8217;s it, no rocket science here.</p>
<div style="text-align: center"><img class=" " style="border: 1px solid black" src="http://www.railsaverpro.com/wp-content/uploads/2012/06/Reuse-Reduce-Recycle.png" alt="http://www.railsaverpro.com/wp-content/uploads/2012/06/Reuse-Reduce-Recycle.png" width="350" height="263" /></p>
<div><em>Also: good for the planet.</em></div>
</div>
<p>&nbsp;</p>
<p>A few real-world examples should make this clear.</p>
<p><strong>Compute a sum </strong>of an array of integers:</p>
<p><!-- HTML generated using hilite.me --></p>
<div>
<pre style="margin: 0;line-height: 125%"><span style="color: #906030">sum = </span>_.reduce [<span style="color: #0000d0;font-weight: bold">1</span>, <span style="color: #0000d0;font-weight: bold">3</span>, <span style="color: #0000d0;font-weight: bold">5</span>, <span style="color: #0000d0;font-weight: bold">7</span>, <span style="color: #0000d0;font-weight: bold">9</span>], (<span style="color: #0060b0;font-weight: bold">(memo, i) -&gt;</span> memo <span style="color: #303030">+</span> i), <span style="color: #0000d0;font-weight: bold">0</span></pre>
</div>
<p>&#8230;which basically resolves into:</p>
<pre>( ( ( ( ( 0 + 1 ) + 3 ) + 5 ) + 7 ) + 9 )</pre>
<p>The third parameter to reduce is the initial value used to &#8216;seed&#8217; the computation; the iterator function is always called with an intermediate result &#8211; in the case of the first call the intermediate result is simply the initial value. In this case, we want a sum of ints so the initial value should be the additive identity (i.e. zero).</p>
<p><strong>Flatten an array</strong> of arrays:<br />
<!-- HTML generated using hilite.me --></p>
<div>
<pre style="margin: 0;line-height: 125%"><span style="color: #906030">flat = </span>_.reduce [[<span style="color: #0000d0;font-weight: bold">1</span>, <span style="color: #0000d0;font-weight: bold">2</span>], [<span style="color: #0000d0;font-weight: bold">3</span>, <span style="color: #0000d0;font-weight: bold">4</span>], [<span style="color: #0000d0;font-weight: bold">5</span>, <span style="color: #0000d0;font-weight: bold">6</span>]], (<span style="color: #0060b0;font-weight: bold">(memo, arr) -&gt;</span> memo.concat arr), []</pre>
</div>
<p>&#8230;which is an equivalent of:</p>
<pre>( ( [].concat( [1, 2] ) ).concat( [3, 4] ) ).concat( [5, 6] )</pre>
<p><strong>Recursively access </strong>a property of an object:<br />
<!-- HTML generated using hilite.me --></p>
<div>
<pre style="margin: 0;line-height: 125%"><span style="color: #906030">item = </span>{<span style="color: #906030">post: author: id: </span><span style="background-color: #fff0f0">'42'</span>}
_.reduce <span style="background-color: #fff0f0">'post.author.id'</span>.split(<span style="background-color: #fff0f0">'.'</span>), (<span style="color: #0060b0;font-weight: bold">(obj, part) -&gt;</span> obj[part]), item</pre>
</div>
<p>&#8230;which is effectively the same as:</p>
<pre>( ( ( item['post'] )['author'] )['id'] )</pre>
<h3>foldl vs. foldr</h3>
<p>If you take a look at the examples above you&#8217;ll probably notice they&#8217;re all left-associative &#8211; the combinations always start with the first item in the array.</p>
<p>What if you wanted right-associativity (i.e., start with the last item)? Well, in such a case you&#8217;re looking for a function conveniently named foldr or reduceRight (in <a href="http://underscorejs.org/#reduceRight">Underscore.js</a>). Unfortunately, foldr is more useful in languages with lazy evaluation such as <a href="http://www.haskell.org/haskellwiki/Haskell">Haskell</a> (foldr recurses into an argument and hence can be used on infinite lists).</p>
<p>Next in the series we&#8217;ll cover a handful of useful little utility functions like &#8216;all&#8217; and &#8216;any&#8217;.</p>
<p>The post <a href="http://www.salsitasoft.com/2013/01/12/going-functional-reduce/">Going Functional &#8211; Reduce</a> appeared first on <a href="http://www.salsitasoft.com">Salsita Software</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.salsitasoft.com/2013/01/12/going-functional-reduce/feed/</wfw:commentRss>
		<slash:comments>0</slash:comments>
		</item>
		<item>
		<title>Going Functional: Map</title>
		<link>http://www.salsitasoft.com/2013/01/07/going-functional-map/</link>
		<comments>http://www.salsitasoft.com/2013/01/07/going-functional-map/#comments</comments>
		<pubDate>Mon, 07 Jan 2013 09:19:44 +0000</pubDate>
		<dc:creator>Tomas Brambora</dc:creator>
				<category><![CDATA[Technical Articles]]></category>

		<guid isPermaLink="false">http://www.salsitasoft.com/?p=2509</guid>
		<description><![CDATA[<p>Let&#8217;s face it: the &#8216;map&#8217; function (a.k.a. &#8216;apply-to-all&#8217;) is pretty simple. You just pass it an array and a function and it returns an array of results of that function applied to each item. What&#8217;s it all about then? Here comes the important bit: the new array will have the same size as the original one. Always. [...]</p><p>The post <a href="http://www.salsitasoft.com/2013/01/07/going-functional-map/">Going Functional: Map</a> appeared first on <a href="http://www.salsitasoft.com">Salsita Software</a>.</p>]]></description>
			<content:encoded><![CDATA[<p style="text-align: justify;">Let&#8217;s face it: the &#8216;map&#8217; function (a.k.a. &#8216;apply-to-all&#8217;) is pretty simple. You just pass it an array and a function and it returns an array of results of that function applied to each item.</p>
<p style="text-align: justify;">What&#8217;s it all about then? Here comes the important bit: the new array will have the same size as the original one. Always. In other words, the map function defines an &#8216;N -&gt; N&#8217; mapping. If you see it used, you immediately know the author wants a <a href="http://en.wikipedia.org/wiki/Homomorphism">homomorphism</a>.</p>
<div style="text-align: justify;">
<p>Let&#8217;s see an example, removing trailing whitespace from strings in a list:</p>
<p><!-- HTML generated using hilite.me --></p>
<div>
<pre style="margin: 0; line-height: 125%;"><span style="color: #906030;">cleanStrings = </span>_.map dirtyStrings, <span style="color: #0060b0; font-weight: bold;">(str) -&gt;</span> str.trim()</pre>
</div>
<p>Simple enough, right? Still, there are two things about &#8216;map&#8217; I&#8217;d like to point out.</p>
<h5 style="text-align: left;">&#8216;map&#8217; vs &#8216;each&#8217;</h5>
<p style="text-align: left;">Occasionally, you might see code like this:</p>
<p><!-- HTML generated using hilite.me --></p>
<div>
<pre style="margin: 0; line-height: 125%;">_.map list, <span style="color: #0060b0; font-weight: bold;">(obj) -&gt;</span> obj.doStuffWithSideEffectsAndNoReturnValue()</pre>
</div>
<p>&nbsp;</p>
<div style="text-align: center;">
<p><img class=" " style="border: 1px solid black;" src="http://www.deshow.net/d/file/animal/2009-07/cute-kitten-631-2.jpg" alt="http://www.deshow.net/d/file/animal/2009-07/cute-kitten-631-2.jpg" width="350" height="263" /></p>
<div><em>Adorable kitten.</em></div>
<p>&nbsp;</p>
</div>
<p>In this case, map is used for the side effect only. This is no good. Seriously. You&#8217;re killing an adorable kitten (see above) when you use map like this. Don&#8217;t do it. Also, you&#8217;re depriving yourself (and anyone who&#8217;s ever going to read your code) of one of the biggest advantages of the functional style: clear expression of intent.</p>
<p>If you see a &#8216;map&#8217; you should immediately know the programmer wants to transform an array into another array of the same size. If you need to call a function for each item in a list just to trigger its side effect, use &#8216;each&#8217; instead (just remember to <a href="http://programmers.stackexchange.com/questions/15269/why-are-side-effects-considered-evil-in-functional-programming">be careful with side effects</a>).</p>
<h5>map vs list comprehensions</h5>
<p>If you&#8217;re lucky enough to write in a language that sports list comprehensions, you can use those to achieve the same result as map. <!-- HTML generated using hilite.me --></p>
<div>
<pre style="margin: 0; line-height: 125%;"><span style="color: #906030;">cleanStrings = </span><span style="line-height: 125%;">(str.trim() </span><span style="color: #008000; font-weight: bold;">for</span><span style="line-height: 125%;"> str </span><span style="color: #008000; font-weight: bold;">in</span><span style="line-height: 125%;"> dirtyStrings)</span></pre>
</div>
<p>This is pretty neat too. However, there&#8217;s a caveat. Comprehensions are more powerful than that, in that they also allow you to filter out some of the items.</p>
<p>Example:<br />
<!-- HTML generated using hilite.me --></p>
<div>
<pre style="margin: 0; line-height: 125%;"><span style="color: #906030;">cleanStrings = </span>(s.trim() <span style="color: #008000; font-weight: bold;">for</span> s <span style="color: #008000; font-weight: bold;">in</span> dirtyStrings <span style="color: #008000; font-weight: bold;">when</span> s.length <span style="color: #303030;">&gt;</span> <span style="color: #0000d0; font-weight: bold;">2</span>)</pre>
</div>
<p>So when to use comprehensions and when map?</p>
<p>Well, that is a tough one. When I code in Python I use comprehensions all the time (mainly because of the way anonymous functions are defined in Python, which makes map kind of awkward to use). JavaScript doesn&#8217;t have them at all, so &#8216;map&#8217; it is. In CoffeeScript, which has both, I tend to favor map if I&#8217;m doing a N -&gt; N transformation and comprehensions when I need to filter some items out. But as always, use what feels natural to you.</p>
<p>Next in the series comes one of my favorite higher-order functions: reduce. Stay tuned.</p>
</div>
<p>The post <a href="http://www.salsitasoft.com/2013/01/07/going-functional-map/">Going Functional: Map</a> appeared first on <a href="http://www.salsitasoft.com">Salsita Software</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.salsitasoft.com/2013/01/07/going-functional-map/feed/</wfw:commentRss>
		<slash:comments>8</slash:comments>
		</item>
		<item>
		<title>Adding a Toolbar Button in a Bootstrapped Firefox Extension</title>
		<link>http://www.salsitasoft.com/2012/12/14/adding-a-toolbar-button-in-a-bootstrapped-firefox-extension/</link>
		<comments>http://www.salsitasoft.com/2012/12/14/adding-a-toolbar-button-in-a-bootstrapped-firefox-extension/#comments</comments>
		<pubDate>Fri, 14 Dec 2012 08:55:37 +0000</pubDate>
		<dc:creator>Matthew Gertner</dc:creator>
				<category><![CDATA[Technical Articles]]></category>

		<guid isPermaLink="false">http://www.salsitasoft.com/?p=2493</guid>
		<description><![CDATA[<p>Adding a toolbar button in a Firefox extension is simple. Since the Firefox user interface is described using XUL, it is enough to inject a snippet of XUL into the &#8220;toolbar palette&#8221; in browser.xul. Traditionally this would be done with a XUL overlay. Experienced XUL developers know that a button added like this won&#8217;t show [...]</p><p>The post <a href="http://www.salsitasoft.com/2012/12/14/adding-a-toolbar-button-in-a-bootstrapped-firefox-extension/">Adding a Toolbar Button in a Bootstrapped Firefox Extension</a> appeared first on <a href="http://www.salsitasoft.com">Salsita Software</a>.</p>]]></description>
			<content:encoded><![CDATA[<p>Adding a toolbar button in a Firefox extension is simple. Since the Firefox user interface is described using XUL, it is enough to <a href="https://developer.mozilla.org/en-US/docs/XUL/Toolbars/Creating_toolbar_buttons#Adding_the_toolbar_button">inject a snippet of XUL</a> into the &#8220;toolbar palette&#8221; in browser.xul. Traditionally this would be done with a XUL overlay. Experienced XUL developers know that a button added like this won&#8217;t show up in the toolbar unless it is added explicitly by the user (using View/Toolbars/Customize&#8230;). This can be solved by <a href="https://developer.mozilla.org/en-US/docs/Code_snippets/Toolbar#Adding_button_by_default">adding the button to the toolbar manually</a> and saving the toolbar&#8217;s &#8220;currentset&#8221;, which contains the list of buttons it contains. This should be done on first run only to ensure that the user&#8217;s settings are preserved if they remove the button later.</p>
<p>But what if you are writing a <a href="https://developer.mozilla.org/en-US/docs/Extensions/Bootstrapped_extensions">bootstrapped extension</a>? XUL overlays are not supported, so you have to add the button to the toolbar palette by hand. Coordinating the toolbar palette, the visible toolbar and the toolbar&#8217;s persistent current set isn&#8217;t rocket science, but it is a bit tricky. You want to make sure that the button is added to the toolbar on first run and is visible on subsequent runs. At the same time, it should be possible to remove it and add it back using the toolbar customization feature.</p>
<p>After some trial and error, as well as plundering <a href="https://github.com/dgutov">Dmitry Gutov</a>&#8216;s nicely written <a href="https://addons.mozilla.org/en-US/firefox/addon/bmreplace/">Replace Bookmark</a> extension for ideas, I came up with the following function:<br />
<script src="https://gist.github.com/4285679.js"></script><br />
This function ensures that the button is always added to the palette (i.e. what the overlay would normally do). On first run, it adds it to the far-right of the toolbar. On subsequent runs, it inserts the button manually into the correct position if it is in the currentset. This is the only bit that is substantially different from the behavior in a traditional extension. I suspect this is necessary because the button is not in the palette yet at the point when Firefox processes the currentset and adds the buttons automatically.</p>
<p>Remember that you should remove the button from the palette and toolbar manually when the extension is disabled or uninstalled to avoid memory leaks.</p>
<p>The post <a href="http://www.salsitasoft.com/2012/12/14/adding-a-toolbar-button-in-a-bootstrapped-firefox-extension/">Adding a Toolbar Button in a Bootstrapped Firefox Extension</a> appeared first on <a href="http://www.salsitasoft.com">Salsita Software</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.salsitasoft.com/2012/12/14/adding-a-toolbar-button-in-a-bootstrapped-firefox-extension/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
		<item>
		<title>Going Functional</title>
		<link>http://www.salsitasoft.com/2012/12/07/going-functional/</link>
		<comments>http://www.salsitasoft.com/2012/12/07/going-functional/#comments</comments>
		<pubDate>Fri, 07 Dec 2012 17:53:48 +0000</pubDate>
		<dc:creator>Tomas Brambora</dc:creator>
				<category><![CDATA[Technical Articles]]></category>
		<category><![CDATA[coffeescript]]></category>
		<category><![CDATA[functional programming]]></category>
		<category><![CDATA[underscore]]></category>

		<guid isPermaLink="false">http://www.salsitasoft.com/?p=2450</guid>
		<description><![CDATA[<p>Introduction Take a look at the following piece of code. What&#8217;s wrong with it? myAwesomeFunction = (objects) -&#62; isGreat = false for item in objects if not item.isImportant continue if item.isGreat isGreat = true break return isGreat Does it go through all the items? No but, man, is that break on the last line easy [...]</p><p>The post <a href="http://www.salsitasoft.com/2012/12/07/going-functional/">Going Functional</a> appeared first on <a href="http://www.salsitasoft.com">Salsita Software</a>.</p>]]></description>
			<content:encoded><![CDATA[<h3>Introduction</h3>
<p>Take a look at the following piece of code. What&#8217;s wrong with it?</p>
<p><!-- HTML generated using hilite.me --></p>
<div>
<pre style="margin: 0;line-height: 125%"><span style="color: #906030">myAwesomeFunction = </span><span style="color: #0060b0;font-weight: bold">(objects) -&gt;</span>
   <span style="color: #906030">isGreat = </span><span style="color: #008000;font-weight: bold">false</span>
   <span style="color: #008000;font-weight: bold">for</span> item <span style="color: #008000;font-weight: bold">in</span> objects
     <span style="color: #008000;font-weight: bold">if</span> <span style="color: #303030">not</span> item.isImportant
       <span style="color: #008000;font-weight: bold">continue</span>
     <span style="color: #008000;font-weight: bold">if</span> item.isGreat
       <span style="color: #906030">isGreat = </span><span style="color: #008000;font-weight: bold">true</span>
       <span style="color: #008000;font-weight: bold">break</span>
  <span style="color: #008000;font-weight: bold">return</span> isGreat</pre>
</div>
<p>Does it go through all the items? No but, man, is that break on the last line easy to miss.<br />
What does it do? Too bad a for loop has no return value. In order to get any idea you need to look into the loop code.</p>
<p>Now compare it with the following version:</p>
<p><!-- HTML generated using hilite.me --></p>
<div>
<pre style="margin: 0;line-height: 125%"><span style="color: #906030">myAwesomeFunction = </span><span style="color: #0060b0;font-weight: bold">(objects) -&gt;</span>
  <span style="color: #008000;font-weight: bold">return</span> _.any objects, <span style="color: #0060b0;font-weight: bold">(o) -&gt;</span> o.isImportant <span style="color: #303030">&amp;&amp;</span> o.isGreat</pre>
</div>
<p>Even if you&#8217;re not familiar with the (wonderful, by the way) <a title="Underscore.js" href="http://underscorejs.org/" target="_blank">Underscore.js</a> library, you can pretty much guess that myAwesomeFunction returns true iff objects contain a great important object.</p>
<p>Now, you could make a (valid) point that the original function is not a shining example of super-ninja programming skills. But that&#8217;s just another thing to point out: using for loops doesn&#8217;t push us, programmers, to be any clearer. Actually quite the opposite &#8211; it stands in our way by forcing us to remember we&#8217;re in a loop and we&#8217;ve to deal with it.</p>
<div style="text-align: center">
<p><a href="http://www.flickr.com/photos/sethw/381321976/sizes/m/in/photostream/" target="_blank"><img class=" " style="border: 1px solid black" src="http://farm1.staticflickr.com/170/381321976_620b11019a.jpg" alt="http://www.flickr.com/photos/sethw/381321976/sizes/m/in/photostream/" width="350" height="263" /></a></p>
<div><em>This guy may or may not have written it.</em></div>
</div>
<p>When it comes to expressing our intentions, a for cycle is a low level construct; unless you read the code thoroughly and really understand, you can&#8217;t know what the purpose of the loop is. It can filter something out or transform all the items in the list. It can signal whether a certain item is present. Frankly, for all I know it can brush your teeth and sleep with your wife.</p>
<p>It&#8217;s just too powerful. Too universal.</p>
<p>Now, in case CoffeeScript (or JavaScript) is your language of choice, chances are you&#8217;re not writing the type of software where speed is essential. Which means (or should mean, anyway) readability beats raw speed. Hands down. And that&#8217;s when functional programming saves the day.</p>
<p>In the next article of this series, we&#8217;ll take a look at the &#8216;map&#8217; function.</p>
<p>The post <a href="http://www.salsitasoft.com/2012/12/07/going-functional/">Going Functional</a> appeared first on <a href="http://www.salsitasoft.com">Salsita Software</a>.</p>]]></content:encoded>
			<wfw:commentRss>http://www.salsitasoft.com/2012/12/07/going-functional/feed/</wfw:commentRss>
		<slash:comments>5</slash:comments>
		</item>
	</channel>
</rss>
