{"id":1423,"date":"2014-10-13T11:55:53","date_gmt":"2014-10-13T11:55:53","guid":{"rendered":"http:\/\/www.dyalog.com\/blog\/?p=1423"},"modified":"2014-10-13T13:30:47","modified_gmt":"2014-10-13T13:30:47","slug":"data-driven-conditionals-2","status":"publish","type":"post","link":"https:\/\/www.dyalog.com\/blog\/2014\/10\/data-driven-conditionals-2\/","title":{"rendered":"Data-driven Conditionals"},"content":{"rendered":"<p><em><strong>ACKNOWLEDGEMENT<\/strong>: My thanks to Andy Shiers for simplifying the examples<\/em><\/p>\n<p>Visitors to APL from other languages are sometimes a bit sniffy about our use of numbers (1 0) for Boolean values True and False. They&#8217;re further bemused by our fondness for moving conditional processing out of code and into data. For example, instead of:<\/p>\n<pre><code class=\"language-apl\">   0=2|\u2375: \u2375\u00f72 \u22c4 \u2375       \u235d even number: half<\/code><\/pre>\n<p>we might say:<\/p>\n<pre><code class=\"language-apl\">   \u2375 \u00f7 1+0=2|\u2375          \u235d half if even<\/code><\/pre>\n<p>At first blush, this seems a bit loony; such transformations appear to produce code which is both slower and more obscure! Here&#8217;s another example:<\/p>\n<pre><code class=\"language-apl\">   2|\u2375: 1+3\u00d7\u2375 \u22c4 \u2375       \u235d odd: one more than its triple<\/code><\/pre>\n<p>vs:<\/p>\n<pre><code class=\"language-apl\">   p \u2190 2|\u2375              \u235d parity\r\n   p + \u2375 \u00d7 1+2\u00d7p        \u235d one more than triple, if odd<\/code><\/pre>\n<p>But think arrays! Data-driven processing of conditionals is inherently parallel and so performs significantly better for large arrays.<\/p>\n<p>The <a href=\"http:\/\/en.wikipedia.org\/wiki\/Collatz_conjecture\">Collatz conjecture<\/a> asserts that the sequence &#8220;half if even, else one plus triple if odd&#8221; always converges to 1. For example, for the number 7 this sequence is:<\/p>\n<pre><code class=\"language-apl\">   7 22 11 34 17 52 26 13 40 20 10 5 16 8 4 2 1<\/code><\/pre>\n<p>To test this up to a million we can borrow <a href=\"http:\/\/dfns.dyalog.com\/n_time.htm\" target=\"_blank\">time<\/a>, a coarse-grained timing operator, from <a href=\"http:\/\/dfns.dyalog.com\/n_contents.htm\" target=\"_blank\">dfns.dws<\/a>:<\/p>\n<pre><code class=\"language-apl\">      scalar\u2190{                                  \r\n          {                 \u235d scalar function\r\n              2|\u2375:1+3\u00d7\u2375     \u235d odd              \r\n              \u2375\u00f72           \u235d even             \r\n          }\u2363{\u237a=1}\u00a8\u2375         \u235d applied under each\r\n      }\r\n\r\n\r\n      vector\u2190{         \r\n          {                 \u235d vector function                                              \r\n              p\u21902|\u2375         \u235d parity                   \r\n              u\u2190\u2375\u00f72-p       \u235d half of evens            \r\n              v\u2190p+u\u00d71+2\u00d7p   \u235d 1+3\u00d7 odds                \r\n              \u222av~1          \u235d without 1s and duplicates\r\n          }\u2363{\u237a\u2261\u236c}\u2375          \u235d applied to whole vector                           \r\n      }  \r\n\r\n\r\n      scalar time \u23731e6      \u235d scalar processing 8\u00be minutes\r\n08:46.03\r\n \r\n      vector time \u23731e6      \u235d array processing 8\u00be seconds\r\n08.74<\/code><\/pre>\n<p>In the above, <code class=\"language-apl\">time<\/code> and <code class=\"language-apl\">\u2363<\/code> are high order &#8220;operators&#8221;.<\/p>\n<p>Defined operator <a href=\"http:\/\/dfns.dyalog.com\/n_time.htm\" target=\"_blank\">time<\/a> takes a function left operand, which it applies to its argument, returning the execution time in minutes and seconds.<\/p>\n<p>Primitive operator <code class=\"language-apl\">\u2363<\/code> (power or fixpoint) applies its left operand function repeatedly until its right operand function, which is supplied with the result (<code>\u237a<\/code>) and argument of each application, returns True (1).<\/p>\n<p>For further discussion of the Collatz function, see <a href=\"http:\/\/dfns.dyalog.com\/n_osc.htm\">http:\/\/dfns.dyalog.com\/n_osc.htm<\/a>.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>ACKNOWLEDGEMENT: My thanks to Andy Shiers for simplifying the examples Visitors to APL from other languages are sometimes a bit sniffy about our use of numbers (1 0) for Boolean values True and False. They&#8217;re further bemused by our fondness &hellip; <a href=\"https:\/\/www.dyalog.com\/blog\/2014\/10\/data-driven-conditionals-2\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":12,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[8],"tags":[],"class_list":["post-1423","post","type-post","status-publish","format-standard","hentry","category-apl-language"],"_links":{"self":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/1423","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\/12"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/comments?post=1423"}],"version-history":[{"count":5,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/1423\/revisions"}],"predecessor-version":[{"id":3993,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/1423\/revisions\/3993"}],"wp:attachment":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/media?parent=1423"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/categories?post=1423"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/tags?post=1423"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}