Cmmposition binop

4 views
Skip to first unread message

Michele Dondi

unread,
May 4, 2005, 9:43:43 AM5/4/05
to perl6-l...@perl.org
I had implicitly touched on this in the past, but speaking of binops - and
of functional features in Perl6, is there any provision of a (list
associative?) composition binop?

I had naively thought about ==> and/or <==, but that's somewhat on a
different level.

What I refer to now is something that takes two {coderefs,anonymous
subs,closures} and returns (an object that behaves like) another anonymous
sub, precisely the one that acts like the former followed by the latter
(or vice versa!).

Optionally, (ab)using C<< ==> >> for it, if we have

my $composed = sub_1 ==> sub_2;

and sub_1 has a signature, and sub_2 has a returns trait, $composed should
have the same signature as sub_1 and the same returns trait as sub_2. Also
it should complain if sub_2 has a signature and sub_1 a returns trait and
these do not match.

How 'bout this idea?


Michele
--
L'amava come si ama qualcosa che e' necessario alla vita.
La odiava come si odia chi tradisce.
- Laura Mancinelli, "Gli occhi dell'imperatore"

Rob Kinyon

unread,
May 4, 2005, 10:27:53 AM5/4/05
to perl6-l...@perl.org
What about the function compose() that would live in the module
"keyword", imported by the incantation "use keyword qw( compose );"?

(NB: My P6-fu sucks right now)

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

Ingo Blechschmidt

unread,
May 4, 2005, 10:44:02 AM5/4/05
to perl6-l...@perl.org
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 *@fs) {
# Note: We're losing compile-time type checking here.
return -> *@args is copy {
@args = $_(*@args) 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

Stuart Cook

unread,
May 4, 2005, 10:24:47 AM5/4/05
to Michele Dondi, perl6-l...@perl.org
> What I refer to now is something that takes two {coderefs,anonymous
> subs,closures} and returns (an object that behaves like) another anonymous
> sub, precisely the one that acts like the former followed by the latter
> (or vice versa!).

Do you mean like the mathematical 'f o g'?

i.e. (f o g)($x) == f(g($x))

Maybe we could just use 'o'.

(Too bad we can't use Haskell's 'f . g'...)


Stuart

Michele Dondi

unread,
May 5, 2005, 3:55:01 AM5/5/05
to Stuart Cook, perl6-l...@perl.org
On Thu, 5 May 2005, Stuart Cook wrote:

>> What I refer to now is something that takes two {coderefs,anonymous
>> subs,closures} and returns (an object that behaves like) another anonymous
>> sub, precisely the one that acts like the former followed by the latter
>> (or vice versa!).
>
> Do you mean like the mathematical 'f o g'?

Indeed.


Michele
--
Have you noticed that people whose parents did not have children, also
tend not to have children.
- Robert J. Kolker in sci.math, "Re: Genetics and Math-Ability"

Reply all
Reply to author
Forward
0 new messages