Hi,

Rob Kinyon wrote:
> What about the function compose() that would live in the module
> "keyword", imported by the incantation "use keyword qw( compose );"?

FWIW, I like "o" better -- function composing is very often used in FP,
and should therefore have a short name.

Luckily, it's very easy to define that:
  sub *infix:<o>(Code $a, Code $b) {...}

> multimethod compose (@*List) {
>     return {
>         $_() for @List;
>     };
> }

I don't that will work for functions that take arguments.

My take at it:
  sub compose (Code [EMAIL PROTECTED]) {
    # Note: We're losing compile-time type checking here.
    return -> [EMAIL PROTECTED] is copy {
      @args = $_([EMAIL PROTECTED]) for reverse @fs;
      return @args;
    };
  }

  sub f(Int $x) { 100 + $x }
  sub g(Int $x) {   2 * $x }
  my $f_o_g = compose &f, &g;
  say $f_o_g(42); # 184


Is there a way to not lose compile-time type checking, so that the
following will barf at compile time?
  sub f(Int $x)         returns Int { 100 + $x }
  sub g(Int $a, Int $b) returns Int {...}
  my $f_o_g = compose &f, &g; # should die


--Ingo

-- 
Linux, the choice of a GNU | Life would be so much easier if we could
generation on a dual AMD   | just look at the source code.
Athlon!                    | -- Dave Olson

Reply via email to