Re: Cmmposition binop

2005-05-05 Thread Michele Dondi
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


Re: Cmmposition binop

2005-05-04 Thread Stuart Cook
 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


Re: Cmmposition binop

2005-05-04 Thread Rob Kinyon
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;
};
}

On 5/4/05, Michele Dondi [EMAIL PROTECTED] wrote:
 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



Re: Cmmposition binop

2005-05-04 Thread Ingo Blechschmidt
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