{"id":1669,"date":"2015-01-16T09:12:52","date_gmt":"2015-01-16T09:12:52","guid":{"rendered":"http:\/\/www.dyalog.com\/blog\/?p=1669"},"modified":"2015-07-08T07:46:08","modified_gmt":"2015-07-08T07:46:08","slug":"the-diamond-kata","status":"publish","type":"post","link":"https:\/\/www.dyalog.com\/blog\/2015\/01\/the-diamond-kata\/","title":{"rendered":"The Diamond Kata"},"content":{"rendered":"<h2>Acknowledgments<\/h2>\n<p>Morten Kromberg is the other co-author of this text but the blogging software prevents his being listed as such.<\/p>\n<p>We are indebted to Jay Foad, Nick Nikolov, John Scholes, and Fiona Smith for comments on successive drafts of the MS.<\/p>\n<h2>The Problem<\/h2>\n<p>The diamond kata is a programming exercise used in the agile development, XP and TDD communities. [<acronym title=\"Rose, Seb, Recycling Tests in TDD, Claysnow Blog, 2014-11-23.\">0<\/acronym>, <acronym title=\"Cockburn, Alistair, Thinking Before Programming, Alistair Cockburn Blog, 2014-11-29.\">1<\/acronym>, <acronym title=\"Jeffries, Ron, TDD on the Diamond Problem, RonJeffries.com Blog, 2014-11-29.\">2<\/acronym>, <acronym title=\"Rusk, John, An Experiment in Think-First Development, AgileWiki Blog, 2014-12-01.\">3<\/acronym>]. It first came to our attention in 2012 in discussions with <a href=\"http:\/\/www.meetup.com\/Stockholm-Software-Craftsmanship\/members\/42780122\/\">Gianfranco Alongi<\/a> of Ericcson AB, Sweden, and again more recently in <acronym title=\"Rusk, John, An Experiment in Think-First Development, AgileWiki Blog, 2014-12-01.\">[3]<\/acronym>. The following problem description is from <acronym title=\"Jeffries, Ron, TDD on the Diamond Problem, RonJeffries.com Blog, 2014-11-29.\">[2]<\/acronym> (the description varies slightly in each of the above references).<\/p>\n<p>We&#8217;re going to write a program that builds a diamond picture like this, for some set of letters from A to whatever:<\/p>\n<pre><code class=\"language-apl\">--A--\r\n-B-B-\r\nC---C\r\n-B-B-\r\n--A--\r\n<\/code><\/pre>\n<h2>Solutions in Dyalog APL<\/h2>\n<p>We are actually going to solve a slightly different problem: the function will have two arguments. The left argument is a scalar of the background element; the right argument is a vector of the &#8220;letters&#8221; to be used. The result described in the opening section can be produced as:<\/p>\n<pre><code class=\"language-apl\">   '-' dia 'ABCD'\r\n---A---\r\n--B-B--\r\n-C---C-\r\nD-----D\r\n-C---C-\r\n--B-B--\r\n---A---\r\n<\/code><\/pre>\n<p>(In an APL session, what you enter is indented and the resultant output is aligned at the left margin. As well, in this text, we sometimes present the indented-input\/output pairs across the page.)<\/p>\n<p>Making the argument the actual letters rather than the number of letters or the last letter, facilitates working with &#8220;alphabets&#8221; other than A-Z. For example:<\/p>\n<pre><code class=\"language-apl\">   0 dia 3 1 4 2\r\n0 0 0 3 0 0 0\r\n0 0 1 0 1 0 0\r\n0 4 0 0 0 4 0\r\n2 0 0 0 0 0 2\r\n0 4 0 0 0 4 0\r\n0 0 1 0 1 0 0\r\n0 0 0 3 0 0 0\r\n<\/code><\/pre>\n<p>The diamond result is readily seen to be composed of reflections of one of the quadrants. For example, <code class=\"language-apl\">'-' dia 'ABCD'<\/code> (shown above) is composed of reflections of<\/p>\n<pre><code class=\"language-apl\">A---\r\n-B--\r\n--C-\r\n---D\r\n<\/code><\/pre>\n<p>The functions <code class=\"language-apl\">\u233d<\/code> (reverse last axis) and <code class=\"language-apl\">\u2296<\/code> (reverse first axis) provide the requisite reflections. If <code class=\"language-apl\">q<\/code> is the quadrant shown above, then:<\/p>\n<pre><code class=\"language-apl\">   \u233dq       q\r\n---A     A---\r\n--B-     -B--\r\n-C--     --C-\r\nD---     ---D\r\n\r\n   \u233d\u2296q      \u2296q\r\nD---     ---D\r\n-C--     --C-\r\n--B-     -B--\r\n---A     A---\r\n<\/code><\/pre>\n<p>There are various ways to stitch the quadrants together (and to drop the common middle row or column) to get the required result. The following is a terse way:<\/p>\n<pre><code class=\"language-apl\">   f\u2190{\u2349\u2375\u236a1\u2193\u2296\u2375}\r\n\r\n   f \u233dq          f f \u233dq        f\u23632 \u233dq\r\n---D---       ---A---       ---A---\r\n--C-C--       --B-B--       --B-B--\r\n-B---B-       -C---C-       -C---C-\r\nA-----A       D-----D       D-----D\r\n              -C---C-       -C---C-\r\n              --B-B--       --B-B--\r\n              ---A---       ---A---\r\n<\/code><\/pre>\n<p>The same pattern (<em>metakata<\/em>?) of <code class=\"language-apl\">f\u23632<\/code> involving <code class=\"language-apl\">\u2349<\/code> can be used to compute the minors of a matrix <acronym title=\"Hui, Roger, and Kenneth E. Iverson, J Introduction and Dictionary,1990-2014; \\. entry\">[4]<\/acronym>.<\/p>\n<p>It remains to produce the quadrant <code class=\"language-apl\">q<\/code>. We note that it is a diagonal matrix and resembles the identity matrix for which creation many techniques are known. We use two of the 34 different methods in <acronym title=\"Hui, Roger, Identity Matrix, Jwiki Essay, 2005-11-18.\">[5]<\/acronym>.<\/p>\n<p>The first method: For a vector with <code class=\"language-apl\">n<\/code> letters, if each vector is followed by <code class=\"language-apl\">n<\/code> copies of the background element, then the <code class=\"language-apl\">2\u2374n<\/code> reshape of <em>that<\/em> rotates each row an additional step to the right, and the result is the required diagonal matrix.<\/p>\n<pre><code class=\"language-apl\">   n\u2190\u2262v\u2190'ABCD'\r\n\r\n   v,'-'\u2374\u2368s\u21902\u2374n           s \u2374 v,'-'\u2374\u2368s\u21902\u2374n\r\nA----                  A---\r\nB----                  -B--\r\nC----                  --C-\r\nD----                  ---D\r\n<\/code><\/pre>\n<p>The second method: for the proposed <em>merge<\/em> operator <acronym title=\"Scholes, John, Merge, Dfns, 2012-03-30.\">[6]<\/acronym> <code class=\"language-apl\">\u237a f merge \u2375<\/code> is an array like <code class=\"language-apl\">\u2375<\/code> with <code class=\"language-apl\">\u237a<\/code> at positions selected by <code class=\"language-apl\">f<\/code>. As such, it is a functional version of selective assignment. Merge with <code class=\"language-apl\">1\u00a01\u2218\u2349<\/code> as the operand does the trick.<\/p>\n<pre><code class=\"language-apl\">   4 4\u2374\u237316                1 1\u2218\u2349 4 4\u2374\u237316\r\n 1  2  3  4            1 6 11 16\r\n 5  6  7  8\r\n 9 10 11 12\r\n13 14 15 16\r\n\r\n   '-'\u2374\u23682\u2374n              'ABCD' (1 1\u2218\u2349merge) '-'\u2374\u23682\u2374n\r\n----                   A---\r\n----                   -B--\r\n----                   --C-\r\n----                   ---D\r\n<\/code><\/pre>\n<p>Putting it together:<\/p>\n<pre><code class=\"language-apl\">dia  \u2190 {{\u2349\u2375\u236a1\u2193\u2296\u2375}\u23632\u233ds\u2374\u2375,\u237a\u2374\u2368s\u21902\u2374\u2262\u2375}\r\ndiam \u2190 {{\u2349\u2375\u236a1\u2193\u2296\u2375}\u23632\u233d\u2375(1 1\u2218\u2349merge)\u237a\u2374\u23682\u2374\u2262\u2375}\r\n\r\n\r\n   '-' dia 'ABCD'         0 dia 3 1 4 2\r\n---A---                0 0 0 3 0 0 0\r\n--B-B--                0 0 1 0 1 0 0\r\n-C---C-                0 4 0 0 0 4 0\r\nD-----D                2 0 0 0 0 0 2\r\n-C---C-                0 4 0 0 0 4 0\r\n--B-B--                0 0 1 0 1 0 0\r\n---A---                0 0 0 3 0 0 0\r\n\r\n   '-' diam 'ABCD'        0 diam 3 1 4 2\r\n---A---                0 0 0 3 0 0 0\r\n--B-B--                0 0 1 0 1 0 0\r\n-C---C-                0 4 0 0 0 4 0\r\nD-----D                2 0 0 0 0 0 2\r\n-C---C-                0 4 0 0 0 4 0\r\n--B-B--                0 0 1 0 1 0 0\r\n---A---                0 0 0 3 0 0 0\r\n<\/code><\/pre>\n<h2>Further Examples<\/h2>\n<pre><code class=\"language-apl\">   \u2191\u2218\u2395a\u00a8 0 1 5 3           \u235d four prefixes of the alphabet\r\n\u250c\u252c\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2510\r\n\u2502\u2502A\u2502ABCDE\u2502ABC\u2502\r\n\u2514\u2534\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2518\r\n   '.' dia\u00a8 \u2191\u2218\u2395a\u00a8 0 1 5 3  \u235d apply dia to each prefix\r\n\u250c\u252c\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2510\r\n\u2502\u2502A\u2502....A....\u2502..A..\u2502\r\n\u2502\u2502 \u2502...B.B...\u2502.B.B.\u2502\r\n\u2502\u2502 \u2502..C...C..\u2502C...C\u2502\r\n\u2502\u2502 \u2502.D.....D.\u2502.B.B.\u2502\r\n\u2502\u2502 \u2502E.......E\u2502..A..\u2502\r\n\u2502\u2502 \u2502.D.....D.\u2502     \u2502\r\n\u2502\u2502 \u2502..C...C..\u2502     \u2502\r\n\u2502\u2502 \u2502...B.B...\u2502     \u2502\r\n\u2502\u2502 \u2502....A....\u2502     \u2502\r\n\u2514\u2534\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2518\r\n   '.' {{\u2349\u2375\u236a1\u2193\u2296\u2375}\u23632\u233ds\u2374\u2375,\u237a\u2374\u2368s\u21902\u2374\u2262\u2375}\u00a8 \u2191\u2218\u2395a\u00a8 0 1 5 3\r\n\u250c\u252c\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u252c\u2500\u2500\u2500\u2500\u2500\u2510\r\n\u2502\u2502A\u2502....A....\u2502..A..\u2502\r\n\u2502\u2502 \u2502...B.B...\u2502.B.B.\u2502\r\n\u2502\u2502 \u2502..C...C..\u2502C...C\u2502\r\n\u2502\u2502 \u2502.D.....D.\u2502.B.B.\u2502\r\n\u2502\u2502 \u2502E.......E\u2502..A..\u2502\r\n\u2502\u2502 \u2502.D.....D.\u2502     \u2502\r\n\u2502\u2502 \u2502..C...C..\u2502     \u2502\r\n\u2502\u2502 \u2502...B.B...\u2502     \u2502\r\n\u2502\u2502 \u2502....A....\u2502     \u2502\r\n\u2514\u2534\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2500\u2534\u2500\u2500\u2500\u2500\u2500\u2518\r\n<\/code><\/pre>\n<p>The last input expression is standalone (replacing <code class=\"language-apl\">dia<\/code> in the penultimate input expression with its definition) and you can <a href=\"http:\/\/tryapl.org\/?a=%27.%27%20%7B%7B%u2349%u2375%u236A1%u2193%u2296%u2375%7D%u23632%u233Ds%u2374%u2375%2C%u237A%u2374%u2368s%u21902%u2374%u2262%u2375%7D%A8%20%u2191%u2218%u2395a%A8%200%201%205%203&amp;run\" target=\"_blank\">try it in a http:\/\/tryapl.org session<\/a>.<\/p>\n<h2>Testing<\/h2>\n<p>Tests are often descriptive rather than prescriptive, asserting what a result should be rather than how to compute it. For that reason they tend to be more robust and easier to develop than the actual function. For example, it is much easier to test that <code class=\"language-apl\">r<\/code> is a root of a polynomial than to compute it, or to check that <code class=\"language-apl\">S<\/code> is a solution to an NP-complete problem than to derive it.<\/p>\n<p>In this instance, we did not write the tests before writing the diamond functions, but we easily could have. The same expressive power in APL that enabled terse solutions of the diamond kata also enabled their concise and comprehensive testing.<\/p>\n<p><code class=\"language-apl\">testd d<\/code> checks that <code class=\"language-apl\">d<\/code> is a correct result of a diamond function, and returns a 1 if it is. Like the diamond functions, <code class=\"language-apl\">testd<\/code> treats <code class=\"language-apl\">d<\/code> in its totality, as an object (matrix) rather than as individual lines. This treatment allows straightforward statement of properties that would be more laborious otherwise. (For example, <code class=\"language-apl\">d\u2261\u2296d<\/code> and <code class=\"language-apl\">d\u2261\u233dd<\/code> assert that <code class=\"language-apl\">d<\/code> is symmetric in the horizontal and vertical axes.)<\/p>\n<pre><code class=\"language-apl\">assert\u2190{\u237a\u2190'assertion failure' \u22c4 0\u220a\u2375:\u237a \u2395signal 8 \u22c4 shy\u21900}\r\n\r\ntestd\u2190{                   \u235d test the result of a diamond function\r\n assert (2=\u2374\u2374\u2375)\u2227=\/\u2374\u2375:     \u235d square matrix\r\n assert (0=\u2262\u2375)\u22281=2|\u2262\u2375:    \u235d dimensions are 0 or odd\r\n assert \u2375\u2261\u233d\u2375:             \u235d symmetric in vertical   axis\r\n assert \u2375\u2261\u2296\u2375:             \u235d symmetric in horizontal axis\r\n n\u2190\u23082\u00f7\u2368\u2262\u2375                 \u235d number of input \"letters\"\r\n q\u2190(n,-n)\u2191\u2375               \u235d upper right quadrant\r\n assert (q=\u236c\u2374\u2375)\u2228\u2218.=\u2368\u2373n:   \u235d background or diagonal\r\n 1                        \u235d 1 iff everything OK\r\n}\r\n\r\n   testd '-' dia 'ABCD'\r\n1\r\n\r\n   testd 0 dia 3 1 4 2\r\n1<\/code><\/pre>\n<p>If a check fails (or if there is an outright programming error), execution suspends at the first line with the error. While suspended an interactive debugging environment is provided for investigating the problem.<\/p>\n<pre><code class=\"language-apl\">   testd 'ABCD'\r\nassertion failure\r\ntestd[1] assert(2=\u2374\u2374\u2375)\u2227=\/\u2374\u2375: \u235d square matrix\r\n     \u2227\r\n   \u2375\r\nABCD\r\n   \u2374\u2375\r\n4\r\n   \u2374\u2374\u2375\r\n1<\/code><\/pre>\n<p><code class=\"language-apl\">testd<\/code> is not a complete test, because it does not &#8220;know&#8221; what the specified background element or what the &#8220;letters&#8221; were. The operator <code class=\"language-apl\">T<\/code> provides a complete test:<\/p>\n<pre>T\u2190{                       \u235d test a diamond function \u237a\u237a\r\n d\u2190\u237a \u237a\u237a \u2375                 \u235d result of diamond function\r\n assert testd d:          \u235d checks not knowing arguments\r\n n\u2190\u2262\u2375                     \u235d number of input \"letters\"\r\n assert (\u2374d)\u22610 0\u2308\u00af1+2\u00d7n:  \u235d dimensions are 0 or \u00af1+2\u00d7n\r\n assert (1\u2265n)\u2228\u237a=\u236c\u2374d:      \u235d background element\r\n assert \u2375\u22611 1\u2349(n,-n)\u2191d:   \u235d \"letters\" in diagonal of quadrant\r\n 1                        \u235d 1 iff everything OK\r\n}<\/pre>\n<p><code class=\"language-apl\">\u237a dia T \u2375<\/code> checks that <code class=\"language-apl\">\u237a dia \u2375<\/code> produces a correct result, and returns a 1 if it does (and suspends at the first line of error if it does not).<\/p>\n<pre><code class=\"language-apl\">   '-' dia T 'ABCD'         '-' diam T 'ABCD'\r\n1                        1\r\n\r\n   0 dia T 3 1 4 2          0 diam T 3 1 4 1 5 9\r\n1                        1<\/code><\/pre>\n<p>The following expression tests <code class=\"language-apl\">'-' dia x<\/code> where <code class=\"language-apl\">x<\/code> is a prefix of the alphabet <code class=\"language-apl\">\u2395a<\/code>, from 0 to 26 letters:<\/p>\n<pre><code class=\"language-apl\">   '-' (dia T)\u2218(\u2374\u2218\u2395a)\u00a8 \u00af1+3 9\u2374\u237327\r\n1 1 1 1 1 1 1 1 1\r\n1 1 1 1 1 1 1 1 1\r\n1 1 1 1 1 1 1 1 1<\/code><\/pre>\n<p>The following expression tests <code class=\"language-apl\">0 dia n?n<\/code> for <code class=\"language-apl\">n<\/code> from 0 to 99. The &#8220;letters&#8221; are <code class=\"language-apl\">n?n<\/code>, a random permutation of order <code class=\"language-apl\">n<\/code>:<\/p>\n<pre><code class=\"language-apl\">   0 (dia T)\u2218(?\u2368)\u00a8 10 10\u2374\u00af1+\u2373100\r\n1 1 1 1 1 1 1 1 1 1\r\n1 1 1 1 1 1 1 1 1 1\r\n1 1 1 1 1 1 1 1 1 1\r\n1 1 1 1 1 1 1 1 1 1\r\n1 1 1 1 1 1 1 1 1 1\r\n1 1 1 1 1 1 1 1 1 1\r\n1 1 1 1 1 1 1 1 1 1\r\n1 1 1 1 1 1 1 1 1 1\r\n1 1 1 1 1 1 1 1 1 1\r\n1 1 1 1 1 1 1 1 1 1<\/code><\/pre>\n<h2>References<\/h2>\n<ol class=\"reference\">\n<li>Rose, Seb, <a href=\"http:\/\/claysnow.co.uk\/recycling-tests-in-tdd\/\" target=\"_blank\"><em>Recycling Tests in TDD<\/em><\/a>, Claysnow Blog, 2014-11-23.<\/li>\n<li>Cockburn, Alistair, <a href=\"http:\/\/alistair.cockburn.us\/Thinking+before+programming\" target=\"_blank\"><em>Thinking Before Programming<\/em><\/a>, Alistair Cockburn Blog, 2014-11-29.<\/li>\n<li>Jeffries, Ron, <a href=\"http:\/\/ronjeffries.com\/articles\/tdd-diamond\/\" target=\"_blank\"><em>TDD on the Diamond Problem<\/em><\/a>, RonJeffries.com Blog, 2014-11-29.<\/li>\n<li>Rusk, John, <a href=\"http:\/\/www.agilekiwi.com\/other\/agile\/think-first-development\" target=\"_blank\"><em>An Experiment in Think-First Development<\/em><\/a>, AgileWiki Blog, 2014-12-01.<\/li>\n<li>Hui, Roger, and Kenneth E. Iverson, <a href=\"http:\/\/www.jsoftware.com\/help\/dictionary\/d431.htm\" target=\"_blank\"><i>J Introduction and Dictionary<\/i><\/a>,1990-2014; \\. entry<\/li>\n<li>Hui, Roger, <a href=\"http:\/\/www.jsoftware.com\/jwiki\/Essays\/Identity%20Matrix\" target=\"_blank\"><em>Identity Matrix<\/em><\/a>, Jwiki Essay, 2005-11-18.<\/li>\n<li>Scholes, John, <a href=\"http:\/\/dfns.dyalog.com\/n_merge.htm\" target=\"_blank\"><em>Merge<\/em><\/a>, Dfns, 2012-03-30.<\/li>\n<li>Legrand, Bernard, <a href=\"http:\/\/www.dyalog.com\/uploads\/documents\/MasteringDyalogAPL.pdf\" target=\"_blank\"><i>Mastering Dyalog APL<\/i><\/a>, 2009-11.<\/li>\n<\/ol>\n<p><strong>NOTE:<\/strong> A full description and tutorial of the language can be found in <acronym title=\"Legrand, Bernard, Mastering Dyalog APL, 2009-11.\">[7]<\/acronym>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Acknowledgments Morten Kromberg is the other co-author of this text but the blogging software prevents his being listed as such. We are indebted to Jay Foad, Nick Nikolov, John Scholes, and Fiona Smith for comments on successive drafts of the &hellip; <a href=\"https:\/\/www.dyalog.com\/blog\/2015\/01\/the-diamond-kata\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":13,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[13,8],"tags":[],"class_list":["post-1669","post","type-post","status-publish","format-standard","hentry","category-algorithms","category-apl-language"],"_links":{"self":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/1669","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/users\/13"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/comments?post=1669"}],"version-history":[{"count":16,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/1669\/revisions"}],"predecessor-version":[{"id":3091,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/1669\/revisions\/3091"}],"wp:attachment":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/media?parent=1669"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/categories?post=1669"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/tags?post=1669"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}