Hi,
(Update it is not an problem with operators in rakudo)
(Update perl5 code fails because $f and $g aren't local in my original code, as almut points out)

I was trying to create a composition operator in perl. I know it is too simple, it only accepts functions, which have 1 argument, but that isn't the problem:

sub infix:<!*> (&f, &g) { return sub ($x) { return f(g($x)); }; }

It works when I make a composition of 2 functions:

sub test ($x) { return $x ~ " test"; } # outputs (&test !* &test) test test say (&test !* &test)('&test !* &test');

When I try to composite 3 functions, it gets a bit wacky:

# Fails with too much recursion say (&test !* (&test !* &test))('&test !* &test !* &test');

When I try to first assign the working case to a variable and then composite it with another function, I get an even stranger result:

my $p = &test !* &test; my $q = $p !* &test; # outputs $p = &test !* &test test say $p('$p = &test !* &test'); #should be $p !* test test test (?), but says $p !* test test say $q('$p !* &test');

I wrote it in a comparable way for javascript to see and perl5, if it was working there. <s>In perl5 it didn't work either</s> and in javascript it did:

(Runned with rhino)
function comp(f, g){ return function(x){ return f(g(x)); } } function test(x){ return x + " test"; } print (comp(test,comp(test,test))("test "));

Failing perl5 code

sub comp { $f = shift; $g = shift; return sub { $x = shift; return $f->($g->($x)); }; } sub test { return (shift) . " test"; } print comp(\&test, comp(\&test,\&test))->("test ");

Correct perl5 code

sub comp { my ($f, $g) = @_; return sub { my $x = shift; return $f->($g->($x)); }; } sub test { return (shift) . " test"; } print comp(\&test, comp(\&test,\&test))->("test ");

The code on rosettacode.org has the same problem. (perl6)

Is this a bug, or is this something I clearly don't understand?


In reply to Rakudo function composition problem by eklerks

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.