[{"data":1,"prerenderedAt":115},["ShallowReactive",2],{"\u002Ftlt-2011.04.07-funky-function-filters-in-perl6":3},{"id":4,"title":5,"body":6,"createdAt":102,"description":12,"draft":103,"extension":104,"image":105,"mastodonThread":105,"meta":106,"navigation":107,"path":108,"seo":109,"stem":110,"tags":111,"updatedAt":113,"__hash__":114},"content\u002FTLT - 2011.04.07 - Funky Function Filters in Perl6.md","TLT_-_2011.04.07_-_Funky_Function_Filters_in_Perl6",{"type":7,"value":8,"toc":99},"minimark",[9,13,22,25,36,39,45,48,54,57,60,66,69,75,78,84,87,93,96],[10,11,12],"p",{},"Tags: Perl6, Rakudo, Programming",[10,14,15,16,21],{},"A while back I wrote about ",[17,18,20],"a",{"href":19},"\u002Ftlt-2006.03.03-funky-function-filters","TLT - 2006.03.03 - Funky Function Filters",". Let me refresh your memory - we have a Python and a Ruby snippet and we translate it into Perl5. The code is a toy to show some fancy shmancy lambda (unnamed functions) usage. The idea is to take a list of functions, filter them, and then with the remaining ones show what happens with parameters from -10 to 10.",[10,23,24],{},"Python example:",[26,27,32],"pre",{"className":28,"code":30,"language":31},[29],"language-text","for f in filter(lambda f: f(-1)>=f(1),\n                [lambda x:x, lambda x:x**2, lambda x:x**3]):\n    for x in range(-10, 11):\n        print x, f(x)\n","text",[33,34,30],"code",{"__ignoreMap":35},"",[10,37,38],{},"Ruby translation:",[26,40,43],{"className":41,"code":42,"language":31},[29],"for f in [lambda { |x| x }, lambda { |x| x**2 }, lambda { |x| x**3 }\n         ].select { |f| f.call(-1) >= f.call(1) }\n    for x in -10..10\n        print x, f.call(x)\n    end\nend\n",[33,44,42],{"__ignoreMap":35},[10,46,47],{},"My perl5 translation:",[26,49,52],{"className":50,"code":51,"language":31},[29],"for my $f (grep { $_->(-1) >= $_->(1) }\n        ((sub{shift}, sub{(shift)**2}, sub{(shift)**3})) {\n  for my $x (-10..10) {\n    print \"$x: \",$f->($x),\"\\n\";\n  }\n}\n",[33,53,51],{"__ignoreMap":35},[10,55,56],{},"Today I thought I'd give a stab at a perl6 translation (using the 2011.01 Rakudo Star release). I started with a straightforward translation from the perl5, then switched to perl6 idioms.",[10,58,59],{},"Perl6 translation:",[26,61,64],{"className":62,"code":63,"language":31},[29],"for (* ** 1, * ** 2, * ** 3).grep({ $_.(-1) >= $_.(1) }) -> $f {\n  for -10 .. 10 -> $x {\n    say \"$x: { $f.($x) }\"\n  }\n}\n",[33,65,63],{"__ignoreMap":35},[10,67,68],{},"The first line is a doozy! This is similar in some ways to the perl5 translation, though the grep now shows up on the righthand-side of the list like in Ruby. The biggest difference to me is in the list of lambdas:",[26,70,73],{"className":71,"code":72,"language":31},[29],"(* ** 1, * ** 2, * ** 3)\n",[33,74,72],{"__ignoreMap":35},[10,76,77],{},"I mean... what does THAT mean?! Let's take the last one, \"* ** 3\". Translated into English:",[26,79,82],{"className":80,"code":81,"language":31},[29],"Whatever to-the-power-of 3\n",[33,83,81],{"__ignoreMap":35},[10,85,86],{},"Wait wait... \"whatever\"? The Whatever-Star is a fancy thing from perl6 that took me a while to work out, and there may still be some nuances that I'm missing. In this context it magically makes a closure... er... lambda, that takes one parameter which it puts where you see the '*'.",[26,88,91],{"className":89,"code":90,"language":31},[29],"# Whatever-Star notation\n* ** 3\n\n# Pointy-block notation\n-> $x { $x ** 3 }\n\n# Subref notation\nsub ($x) { $x ** 3 }\n",[33,92,90],{"__ignoreMap":35},[10,94,95],{},"(Note: that last one should be SLURPY! But I don't know how to do that apparently)",[10,97,98],{},"Enjoy... Whatever!",{"title":35,"searchDepth":100,"depth":100,"links":101},2,[],"2011-04-07T17:51-04:00",false,"md",null,{},true,"\u002Ftlt-2011.04.07-funky-function-filters-in-perl6",{"title":5,"description":12},"TLT - 2011.04.07 - Funky Function Filters in Perl6",[112],"blog","2011-10-16T09:08-04:00","fzAeoLy0nvQgJVc63u-iu0oAuO86sesdXfYeIsoNCsY",1778988314399]