<?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>Mark Needham &#187; J</title>
	<atom:link href="http://www.markhneedham.com/blog/category/j/feed/" rel="self" type="application/rss+xml" />
	<link>http://www.markhneedham.com/blog</link>
	<description>Thoughts on Software Development</description>
	<lastBuildDate>Mon, 13 Feb 2012 21:25:48 +0000</lastBuildDate>
	<language>en</language>
	<sy:updatePeriod>hourly</sy:updatePeriod>
	<sy:updateFrequency>1</sy:updateFrequency>
	<generator>http://wordpress.org/?v=3.3.1</generator>
		<item>
		<title>J: Tacit Programming</title>
		<link>http://www.markhneedham.com/blog/2010/07/13/j-tacit-programming/</link>
		<comments>http://www.markhneedham.com/blog/2010/07/13/j-tacit-programming/#comments</comments>
		<pubDate>Tue, 13 Jul 2010 14:47:41 +0000</pubDate>
		<dc:creator>Mark Needham</dc:creator>
				<category><![CDATA[J]]></category>
		<category><![CDATA[j]]></category>

		<guid isPermaLink="false">http://www.markhneedham.com/blog/?p=2683</guid>
		<description><![CDATA[A couple of months ago I wrote about tacit programming with respect to F#, a term which I first came across while reading about the J programming language. There&#8217;s a good introduction to tacit programming on the J website which shows the evolution of a function which originally has several local variables into a state [...]]]></description>
			<content:encoded><![CDATA[<p>A couple of months ago I wrote about <a href="http://www.markhneedham.com/blog/2010/05/10/f-tacit-programming/">tacit programming with respect to F#,</a> a term which I first came across while reading about the <a href="http://en.wikipedia.org/wiki/J_(programming_language)">J programming language</a>.</p>
<p>There&#8217;s a <a href="http://www.jsoftware.com/help/primer/tacit_definition.htm">good introduction to tacit programming on the J website</a> which shows the evolution of a function which originally has several local variables into a state where it has none at all.</p>
<p>I&#8217;ve been having a go at writing <a href="http://osherove.com/tdd-kata-1/">Roy Osherove&#8217;s TDD Kata</a> in J and while I haven&#8217;t got very far yet I saw a good opportunity to move the code I&#8217;ve written so far into a more tacit style.</p>
<p>From my understanding two of the ways that we can drive towards a tacit style are by removing explicitly passed arguments and variables from our code.</p>
<h3>Remove explicitly passed arguments</h3>
<p>The second part of the kata requires us to allow new lines separating numbers as well as commas so with the help of the guys on the J mailing list I wrote a function which converts all new lines characters into commas:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">replaceNewLines =: 3 : 0
	y rplc ('\n';',')
)</pre></div></div>

<p>&#8216;rplc&#8217;  takes an input as its left hand argument and a 2 column boxed matrix as its right hand argument. </p>
<p>In this case the left hand argument is &#8216;y&#8217; which gets passed to &#8216;replaceNewLines&#8217; and the boxed matrix contains &#8216;\n&#8217; and &#8216;,&#8217;. The left item in the matrix gets replace by the right item.</p>
<p>We want to get to the point where we <strong>don&#8217;t have to explicitly pass y</strong> &#8211; it should just be inferred from the function definition.</p>
<p>As is the case in F# it seems like if we want to do this then we need to have the inferred value (i.e. y) passing as the last argument to a function which in this case means that it needs to be passed as the right hand argument.</p>
<p>&#8216;rplc&#8217; is actually the same as another function &#8216;stringreplace&#8217; which takes in the arguments the other way around which is exactly what we need.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">replaceNewLines =: 3 : 0
	('\n';',') stringreplace y
)</pre></div></div>

<p>The next step is to apply the left hand argument of &#8216;stringreplace&#8217; but infer the right hand argument.</p>
<p>We can use the bond conjunction (&#038;) to do this. The bond conjunction creates a new function (or verb in J speak) which has partially applied the left argument to the verb passed as the right hand argument.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">replaceNewLines =:  ('\n' ; ',') &amp; stringreplace</pre></div></div>

<p>&#8216;replaceNewLines&#8217; represents the partial application of &#8216;stringReplace&#8217;. We can now pass a string to &#8216;replaceNewLines&#8217; and it will replace the new lines characters with commas.</p>
<h3>Remove local variables</h3>
<p>My &#8216;add&#8217; function currently looks like this:</p>

<div class="wp_syntax"><table><tr><td class="line_numbers"><pre>1
2
3
4
</pre></td><td class="code"><pre class="text" style="font-family:monospace;">add =: 3 : 0
	newY =. replaceNewLines y
 	+/ &quot;. newY
 )</pre></td></tr></table></div>

<p>We want to try and drive &#8216;add&#8217; to the point where it&#8217;s just <strong>a composition of different functions</strong>.</p>
<p>At the moment on line 3 we have &#8216;+/&#8217; which can be used to get the sum of a list of numbers.</p>
<p>e.g.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">+/ 1 2 3
&gt; 6</pre></div></div>

<p>We also have &#8216;&#8221;.&#8217; which converts a character array into numbers.</p>
<p>e.g.</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">&quot;. '1,2,3'
&gt; 1 2 3</pre></div></div>

<p>In order to compose our 3 functions together without explicitly passing in &#8216;y&#8217; or &#8216;newY&#8217; we need to make use of atop conjunction (@) which &#8220;combines two verbs into a derived verb that applies the right verb to its argument and then applies the left verb to that result.&#8221;</p>
<p>It works in the same way as <a href="http://www.markhneedham.com/blog/2009/12/09/haskell-vs-f-function-composition/">Haskell&#8217;s function composition operator</a> i.e. it applies the functions starting with the one furthest right.</p>
<p>We end up with this:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">add =: +/ @ &quot;. @ replaceNewLines</pre></div></div>

<p>or if we want to inline the whole thing:</p>

<div class="wp_syntax"><div class="code"><pre class="text" style="font-family:monospace;">add =:  +/ @ &quot;. @ ( ('\n' ; ',') &amp; stringreplace )</pre></div></div>

<h3>Overall</h3>
<p>I&#8217;m still getting the hang of this language &#8211; it&#8217;s taking much longer than with any other languages I&#8217;ve played with &#8211; but so far the ideas I&#8217;ve come across are very interesting and it seems like it massively reduces the amount of code required to solve certain types of problems.</p>
]]></content:encoded>
			<wfw:commentRss>http://www.markhneedham.com/blog/2010/07/13/j-tacit-programming/feed/</wfw:commentRss>
		<slash:comments>1</slash:comments>
		</item>
	</channel>
</rss>

