jhuni's Journal http://use.perl.org/~jhuni/journal/ jhuni's use Perl Journal en-us use Perl; is Copyright 1998-2006, Chris Nandor. Stories, comments, journals, and other submissions posted on use Perl; are Copyright their respective owners. 2012-02-09T00:12:56+00:00 pudge pudge@perl.org Technology hourly 1 1970-01-01T00:00+00:00 jhuni's Journal http://use.perl.org/images/topics/useperl.gif http://use.perl.org/~jhuni/journal/ More Rosetta code written in Perl6 http://use.perl.org/~jhuni/journal/40253?from=rss <a href="http://rosettacode.org/wiki/Dot_product#Perl_6">http://rosettacode.org/wiki/Dot_product#Perl_6</a> <br> <br> I added a solution to the dot product, which it turns out was very easy knowing Perl6 meta operators: <br> <br> <code>[+] ( @a &lt;&lt;*&gt;&gt; @b )</code> <br> <br> <a href="http://rosettacode.org/wiki/Find_the_missing_permutation#Perl_6">http://rosettacode.org/wiki/Find_the_missing_permutation#Perl_6</a> <br> <br> Another problem I worked on is this missing permutations problem, which I solved using cross operators, yet it still doesn't feel that elegant to repeat things four times: <br> <br> <code>[X~] @letters = @letters X~ @letters X~ @letters X~ @letters</code> <br> <br> I would've used the <tt>[X~]</tt> operator, however, it does not seem to work in any implementation yet, so instead I wrote things like on the right hand side, which is quite messy. The only other thing which is sort of interesting is the code to check duplicates. I forget about the <tt>uniq</tt> function for a while so I wrote this messy code to check for duplicates: <br> <br> <code> sub lacksDuplicates($str) {<br> <br> &nbsp;&nbsp;&nbsp;&nbsp;my @chars = $str.split('');<br> &nbsp;&nbsp;&nbsp;&nbsp;my @pastValues;<br> <br> &nbsp;&nbsp;&nbsp;&nbsp;for @chars -&gt; $char {<br> <br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;return ?(0) if $char eq any @pastValues;<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;@pastValues.push($char);<br> <br> &nbsp;&nbsp;&nbsp;&nbsp;}<br> <br> &nbsp;&nbsp;&nbsp;&nbsp;return ?(1);<br> <br> } </code> <br> <br> Fortunately we have <tt>uniq</tt> so I wrote something more pleasant instead: <br> <br><nobr> <wbr></nobr><code>.chars ==<nobr> <wbr></nobr>.split('').uniq.elems</code> jhuni 2010-03-19T11:21:11+00:00 journal Perl6 code http://use.perl.org/~jhuni/journal/40252?from=rss I uploaded a repository containing lots of perl6 examples/code that I have written over the years: <br> <br> <a href="http://github.com/jhuni/perl6-general-examples">http://github.com/jhuni/perl6-general-examples</a> <br> <br> Most of that code is stuff that I put on wikis such as perl6.wikia.com. I will probably add more to that repository over the days to come, however, I intend to focus on more practical applications now then just learning and examples. But in the mean time I found this page on Rosetta code and I would like to briefly rant about it: <br> <br> <a href="http://rosettacode.org/wiki/Loops/For#Perl_6">http://rosettacode.org/wiki/Loops/For#Perl_6</a> <br> <br> That page asks you to solve the problem of printing the following triangle of asterisks: <br> <br> <code> *<br> **<br> ***<br> ****<br> *****<br> </code> <br> I just feel that the solutions written in Perl6 on that page are more elegant then those written in any of the other languages. Part of this is the quantity of characters. Perl6 has the least, perhaps due to the great new operators and the fact that you can exclude parenthesis. <br> <br> <code> import sys<br> for i in xrange(5):<br> &nbsp;&nbsp;&nbsp;&nbsp;for j in xrange(i+1):<br> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;sys.stdout.write("*")<br> &nbsp;&nbsp;&nbsp;&nbsp;print<br> </code> <br> <br> This solution from that link makes me wonder... who ever said Python is readable? How many people actually know what <tt>stdout</tt> is, and how many people are going to know what a blank <tt>print</tt> statement does? Admittely, that code is constrained because Rosetta code asked that two nested loops be used, so lets look at a more "Pythonic" way of doing things: <br> <br> <code> for i in range(1,6):<br> &nbsp;&nbsp;&nbsp;&nbsp;print '*' * i<br> </code> <br> <br> I don't think that code is actually that much better because the <tt>*</tt> character is used to replicate strings and it just so happens to be the character which is being replicated! Furthermore, it is confusing that the range is represented as <tt>range(1,6)</tt> when the actual triangle you need to print is <tt>1..5</tt>. <br> <br> <code>say '*' x $_ for 1..5;</code> <br> <br> How about that? It is only 22 characters, as opposed to 96 and 38 for the Python solutions. Even constrained by the condition that you have to use two for loops, the perl6 solution is still elegant because you can leave off parenthesis. <br> <br> <code>([\~] "*" xx 5).join("\n").say;</code> <br> <br> This is another solution using triangular reduction operators, which are a new feature in Perl6. It doesn't seem to be as elegant as the previous example, but I still think it is nice. Related to that, you can calculate the number of asteriks in that triangle using reduction operators: <tt>([+] 1..5 == 15)</tt>. Anyways I think that is enough of a rant for today =/ jhuni 2010-03-18T07:00:17+00:00 journal OpenJSAN http://use.perl.org/~jhuni/journal/39599?from=rss I have been developing on JSAN recently since it was fixed. It is a little lonely though because it is nothing near as popular as CPAN. I noticed though that the JSAN loader is super slow so I have been using "Tasks" a lot, which are basically JavaScript files containing many modules in one, this has saved me from HTTP requests and bandwidth on my website. <br> <br> The sad thing thing is I still use PHP on my website which is disgusting. In my opinion get_file_contents makes a lot more sense then <tt>file_get_contents</tt>, it just doesn't sound right in English to say <tt>file_get_contents</tt> so I tend to get in wrong all the time. I simply fail at coding whenever I try to use PHP, which is okay though because I have been moving everything I can to the client-side with JavaScript. jhuni 2009-09-09T08:59:53+00:00 journal