{"id":491,"date":"2010-05-31T15:50:42","date_gmt":"2010-05-31T14:50:42","guid":{"rendered":"http:\/\/www.streppone.it\/cosimo\/blog\/2010\/05\/a-working-bogosort-in-perl6\/"},"modified":"2010-05-31T15:50:42","modified_gmt":"2010-05-31T14:50:42","slug":"a-working-bogosort-in-perl6","status":"publish","type":"post","link":"https:\/\/www.streppone.it\/cosimo\/blog\/2010\/05\/a-working-bogosort-in-perl6\/","title":{"rendered":"A working bogosort in Perl6"},"content":{"rendered":"<p>It&#39;s not even mine actually. @moritz helped me with that. I couldn&#39;t remember the syntax for the reduce operator (and it&#39;s <code>[&lt;op&gt;]<\/code>), so here it is, in its <a href=\"http:\/\/en.wikipedia.org\/wiki\/Bogosort#Perl_6\" rel=\"nofollow\">full wikipedia glory<\/a>.<\/p>\n<pre>\r\n@list .= pick(*) until [&lt;=] @list\r\n<\/pre>\n<p>Here&#39;s a somewhat more complete example, that you can actually run:<\/p>\n<pre>\r\nmy @list = 1, 2, 7, 4, 3;\r\n@list .= pick(*) until [&lt;=] @list;\r\nsay @list.perl;\r\n<\/pre>\n<p>This will <a href=\"http:\/\/wikipedia.org\/wiki\/Bogosort\" rel=\"nofollow\">bogosort the array (wikipedia)<\/a>, that is, shuffle it until it&#39;s ordered. Look at the Java version for a nice comparison.<\/p>\n<p>Here&#39;s the explanation:<\/p>\n<pre>\r\nmy @list = 1, 2, 7, 4, 3;\r\n<\/pre>\n<p>Declares the list. You don&#39;t need parens around the elements. If that was a deck of cards, you could write it as:<\/p>\n<pre>\r\nmy @list = &lt;A 2 3 4 5 6 7 8 9 J Q K&gt;\r\n<\/pre>\n<p>The angular brackets <code>&lt;&gt;<\/code> are the equivalent of <code>qw()<\/code> in Perl 5. Following line is quite dense:<\/p>\n<pre>\r\n@list .= pick(*) until [&lt;=] @list;\r\n<\/pre>\n<p>This can be written also as:<\/p>\n<pre>\r\nwhile not [&lt;=] @list {\r\n  @list .= pick(*)\r\n}\r\n<\/pre>\n<p>As you can see, you don&#39;t need parens around the <code>while<\/code> condition. The <code>[&lt;=] @list<\/code> bit is IINM a reduce operator for a list. For example, to sum all elements of a list, you&#39;d use:<\/p>\n<pre>\r\nmy @num = 1, 2, 3, 4;\r\nsay [+] @num;\r\n<\/pre>\n<p>So the <code>[&lt;=]<\/code> operator applied to a list compares each element with the next one, then the result with the next one. Assuming <code>@list = 2, 4, 8, 5<\/code>, that is how I think it runs:<\/p>\n<pre>\r\n2 &lt;= 4 (true, go on, last evaluated value is 4)\r\n4 &lt;= 8 (true, go on, last value is 8)\r\n8 &lt;= 5 (false, stop here, return value is false)\r\n<\/pre>\n<p>So, if the array is ordered, the while\/until block is skipped. If the array is not ordered, execute the following:<\/p>\n<pre>\r\n@list .= pick(*)\r\n<\/pre>\n<p><code>.=<\/code> is an operator for <b>in-place method call<\/b>. Writing that is equivalent to:<\/p>\n<pre>\r\n@list = @list.pick(*)\r\n<\/pre>\n<p>That in Perl 5 would be written as:<\/p>\n<pre>\r\n$list = SomeListObject-&gt;new(1, 2, 3, 4, 5);\r\n$list-&gt;pick(???);\r\n<\/pre>\n<p>Actually, there wouldn&#39;t be any Perl 5 direct equivalent code, since <b>the magic bit here (the *, aka &quot;whatever&quot;)<\/b> it&#39;s not even there. &quot;whatever&quot; is magic for <i>&quot;anything that&#39;s applicable here, until there&#39;s an end to it&quot;<\/i>, or at least this is my ignorant interpretation of it&#8230; :)<\/p>\n<p>So in this context, <code>pick()<\/code> is a method that returns a random element from a list. Together with <code>*<\/code>, it means pick a random element from any of the original <code>@list<\/code>. Mmmh. But then it should be equivalent to:<\/p>\n<pre>\r\n@list .= pick(@list)\r\n<\/pre>\n<p>But it&#39;s not, so <code>pick(*)<\/code> <b>knows<\/b> the right thing to do&#8230;<br \/>\nAnd it&#39;s clear that I don&#39;t know how pick() works :)<br \/>Anyway&#8230;<\/p>\n<p>BTW, this is <b>runnable code<\/b>. If you want to try it, <a href=\"http:\/\/github.com\/rakudo\/rakudo\" rel=\"nofollow\">clone the rakudo<\/a> repository, and run <code>perl Configure.pl --gen-parrot<\/code> to build it. Have fun!<\/p><\/p>\n","protected":false},"excerpt":{"rendered":"<p>It&#39;s not even mine actually. @moritz helped me with that. I couldn&#39;t remember the syntax for the reduce operator (and it&#39;s [&lt;op&gt;]), so here it is, in its full wikipedia glory. @list .= pick(*) until [&lt;=] @list Here&#39;s a somewhat more complete example, that you can actually run: my @list = 1, 2, 7, 4, [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[431,430,50,290,286,429,428],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v22.9 - https:\/\/yoast.com\/wordpress\/plugins\/seo\/ -->\n<title>A working bogosort in Perl6 - Random hacking<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.streppone.it\/cosimo\/blog\/2010\/05\/a-working-bogosort-in-perl6\/\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"A working bogosort in Perl6 - Random hacking\" \/>\n<meta property=\"og:description\" content=\"It&#039;s not even mine actually. @moritz helped me with that. I couldn&#039;t remember the syntax for the reduce operator (and it&#039;s [&lt;op&gt;]), so here it is, in its full wikipedia glory. @list .= pick(*) until [&lt;=] @list Here&#039;s a somewhat more complete example, that you can actually run: my @list = 1, 2, 7, 4, [&hellip;]\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.streppone.it\/cosimo\/blog\/2010\/05\/a-working-bogosort-in-perl6\/\" \/>\n<meta property=\"og:site_name\" content=\"Random hacking\" \/>\n<meta property=\"article:published_time\" content=\"2010-05-31T14:50:42+00:00\" \/>\n<meta name=\"author\" content=\"cosimo\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"cosimo\" \/>\n\t<meta name=\"twitter:label2\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\/\/www.streppone.it\/cosimo\/blog\/2010\/05\/a-working-bogosort-in-perl6\/#article\",\"isPartOf\":{\"@id\":\"https:\/\/www.streppone.it\/cosimo\/blog\/2010\/05\/a-working-bogosort-in-perl6\/\"},\"author\":{\"name\":\"cosimo\",\"@id\":\"https:\/\/www.streppone.it\/cosimo\/blog\/#\/schema\/person\/c443bedbf6ecf99550d6395620801df1\"},\"headline\":\"A working bogosort in Perl6\",\"datePublished\":\"2010-05-31T14:50:42+00:00\",\"dateModified\":\"2010-05-31T14:50:42+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\/\/www.streppone.it\/cosimo\/blog\/2010\/05\/a-working-bogosort-in-perl6\/\"},\"wordCount\":360,\"commentCount\":0,\"publisher\":{\"@id\":\"https:\/\/www.streppone.it\/cosimo\/blog\/#\/schema\/person\/c443bedbf6ecf99550d6395620801df1\"},\"keywords\":[\"array\",\"bogosort\",\"perl\",\"perl6\",\"rakudo\",\"sort\",\"wikipedia\"],\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"CommentAction\",\"name\":\"Comment\",\"target\":[\"https:\/\/www.streppone.it\/cosimo\/blog\/2010\/05\/a-working-bogosort-in-perl6\/#respond\"]}]},{\"@type\":\"WebPage\",\"@id\":\"https:\/\/www.streppone.it\/cosimo\/blog\/2010\/05\/a-working-bogosort-in-perl6\/\",\"url\":\"https:\/\/www.streppone.it\/cosimo\/blog\/2010\/05\/a-working-bogosort-in-perl6\/\",\"name\":\"A working bogosort in Perl6 - Random hacking\",\"isPartOf\":{\"@id\":\"https:\/\/www.streppone.it\/cosimo\/blog\/#website\"},\"datePublished\":\"2010-05-31T14:50:42+00:00\",\"dateModified\":\"2010-05-31T14:50:42+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/www.streppone.it\/cosimo\/blog\/2010\/05\/a-working-bogosort-in-perl6\/#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/www.streppone.it\/cosimo\/blog\/2010\/05\/a-working-bogosort-in-perl6\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/www.streppone.it\/cosimo\/blog\/2010\/05\/a-working-bogosort-in-perl6\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/www.streppone.it\/cosimo\/blog\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"A working bogosort in Perl6\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/www.streppone.it\/cosimo\/blog\/#website\",\"url\":\"https:\/\/www.streppone.it\/cosimo\/blog\/\",\"name\":\"Random hacking\",\"description\":\"Assume nothing. Code defensively. Keep it simple, stupid!\",\"publisher\":{\"@id\":\"https:\/\/www.streppone.it\/cosimo\/blog\/#\/schema\/person\/c443bedbf6ecf99550d6395620801df1\"},\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\/\/www.streppone.it\/cosimo\/blog\/?s={search_term_string}\"},\"query-input\":\"required name=search_term_string\"}],\"inLanguage\":\"en-US\"},{\"@type\":[\"Person\",\"Organization\"],\"@id\":\"https:\/\/www.streppone.it\/cosimo\/blog\/#\/schema\/person\/c443bedbf6ecf99550d6395620801df1\",\"name\":\"cosimo\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/www.streppone.it\/cosimo\/blog\/#\/schema\/person\/image\/\",\"url\":\"https:\/\/secure.gravatar.com\/avatar\/cb1d938720df45a2720724aae99e3bfc?s=96&r=g\",\"contentUrl\":\"https:\/\/secure.gravatar.com\/avatar\/cb1d938720df45a2720724aae99e3bfc?s=96&r=g\",\"caption\":\"cosimo\"},\"logo\":{\"@id\":\"https:\/\/www.streppone.it\/cosimo\/blog\/#\/schema\/person\/image\/\"},\"url\":\"https:\/\/www.streppone.it\/cosimo\/blog\/author\/cosimo\/\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"A working bogosort in Perl6 - Random hacking","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.streppone.it\/cosimo\/blog\/2010\/05\/a-working-bogosort-in-perl6\/","og_locale":"en_US","og_type":"article","og_title":"A working bogosort in Perl6 - Random hacking","og_description":"It&#39;s not even mine actually. @moritz helped me with that. I couldn&#39;t remember the syntax for the reduce operator (and it&#39;s [&lt;op&gt;]), so here it is, in its full wikipedia glory. @list .= pick(*) until [&lt;=] @list Here&#39;s a somewhat more complete example, that you can actually run: my @list = 1, 2, 7, 4, [&hellip;]","og_url":"https:\/\/www.streppone.it\/cosimo\/blog\/2010\/05\/a-working-bogosort-in-perl6\/","og_site_name":"Random hacking","article_published_time":"2010-05-31T14:50:42+00:00","author":"cosimo","twitter_card":"summary_large_image","twitter_misc":{"Written by":"cosimo","Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.streppone.it\/cosimo\/blog\/2010\/05\/a-working-bogosort-in-perl6\/#article","isPartOf":{"@id":"https:\/\/www.streppone.it\/cosimo\/blog\/2010\/05\/a-working-bogosort-in-perl6\/"},"author":{"name":"cosimo","@id":"https:\/\/www.streppone.it\/cosimo\/blog\/#\/schema\/person\/c443bedbf6ecf99550d6395620801df1"},"headline":"A working bogosort in Perl6","datePublished":"2010-05-31T14:50:42+00:00","dateModified":"2010-05-31T14:50:42+00:00","mainEntityOfPage":{"@id":"https:\/\/www.streppone.it\/cosimo\/blog\/2010\/05\/a-working-bogosort-in-perl6\/"},"wordCount":360,"commentCount":0,"publisher":{"@id":"https:\/\/www.streppone.it\/cosimo\/blog\/#\/schema\/person\/c443bedbf6ecf99550d6395620801df1"},"keywords":["array","bogosort","perl","perl6","rakudo","sort","wikipedia"],"inLanguage":"en-US","potentialAction":[{"@type":"CommentAction","name":"Comment","target":["https:\/\/www.streppone.it\/cosimo\/blog\/2010\/05\/a-working-bogosort-in-perl6\/#respond"]}]},{"@type":"WebPage","@id":"https:\/\/www.streppone.it\/cosimo\/blog\/2010\/05\/a-working-bogosort-in-perl6\/","url":"https:\/\/www.streppone.it\/cosimo\/blog\/2010\/05\/a-working-bogosort-in-perl6\/","name":"A working bogosort in Perl6 - Random hacking","isPartOf":{"@id":"https:\/\/www.streppone.it\/cosimo\/blog\/#website"},"datePublished":"2010-05-31T14:50:42+00:00","dateModified":"2010-05-31T14:50:42+00:00","breadcrumb":{"@id":"https:\/\/www.streppone.it\/cosimo\/blog\/2010\/05\/a-working-bogosort-in-perl6\/#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.streppone.it\/cosimo\/blog\/2010\/05\/a-working-bogosort-in-perl6\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.streppone.it\/cosimo\/blog\/2010\/05\/a-working-bogosort-in-perl6\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.streppone.it\/cosimo\/blog\/"},{"@type":"ListItem","position":2,"name":"A working bogosort in Perl6"}]},{"@type":"WebSite","@id":"https:\/\/www.streppone.it\/cosimo\/blog\/#website","url":"https:\/\/www.streppone.it\/cosimo\/blog\/","name":"Random hacking","description":"Assume nothing. Code defensively. Keep it simple, stupid!","publisher":{"@id":"https:\/\/www.streppone.it\/cosimo\/blog\/#\/schema\/person\/c443bedbf6ecf99550d6395620801df1"},"potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.streppone.it\/cosimo\/blog\/?s={search_term_string}"},"query-input":"required name=search_term_string"}],"inLanguage":"en-US"},{"@type":["Person","Organization"],"@id":"https:\/\/www.streppone.it\/cosimo\/blog\/#\/schema\/person\/c443bedbf6ecf99550d6395620801df1","name":"cosimo","image":{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/www.streppone.it\/cosimo\/blog\/#\/schema\/person\/image\/","url":"https:\/\/secure.gravatar.com\/avatar\/cb1d938720df45a2720724aae99e3bfc?s=96&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/cb1d938720df45a2720724aae99e3bfc?s=96&r=g","caption":"cosimo"},"logo":{"@id":"https:\/\/www.streppone.it\/cosimo\/blog\/#\/schema\/person\/image\/"},"url":"https:\/\/www.streppone.it\/cosimo\/blog\/author\/cosimo\/"}]}},"_links":{"self":[{"href":"https:\/\/www.streppone.it\/cosimo\/blog\/wp-json\/wp\/v2\/posts\/491"}],"collection":[{"href":"https:\/\/www.streppone.it\/cosimo\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.streppone.it\/cosimo\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.streppone.it\/cosimo\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.streppone.it\/cosimo\/blog\/wp-json\/wp\/v2\/comments?post=491"}],"version-history":[{"count":0,"href":"https:\/\/www.streppone.it\/cosimo\/blog\/wp-json\/wp\/v2\/posts\/491\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.streppone.it\/cosimo\/blog\/wp-json\/wp\/v2\/media?parent=491"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.streppone.it\/cosimo\/blog\/wp-json\/wp\/v2\/categories?post=491"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.streppone.it\/cosimo\/blog\/wp-json\/wp\/v2\/tags?post=491"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}