On Tue, Sep 14, 2004 at 12:45:17PM +0200, Michele Dondi wrote:
: To my knowledge and great
: pleasure Perl6 will support currying and a builtin reduce/fold operator.
:
: So what I would like to do is (i) C<map> the list of templates to a list
: of curried closures in which the first parameter is fixed to each given
: template, (ii) C<reduce> this list by means of right pipe binop
: ( C<< ==> >> ) with a "starting value" (leftmost) of, say, @input.
It probably depends on just how we implement infix:==>. Looking for
a righthand argument that is curried to have all of its arguments
except for a list is one way to look at it. But all our currying so
far is explicit, and ==> in some sense wants to auto-curry its right
side to return some function with a missing list, even if it isn't
written that way, which means it's perhaps acting more like a macro
than a first-class function. But just because the actual binary ==>
operator does some extra syntactic magic doesn't necessarily mean
that infix:==> won't do the right thing to its arguments. That is,
syntactic ==> has to be smart enough to realize that it shouldn't
always call infix:==>. For instance:
@foo ==> bletch ==> @bar
has to be smart enough to notice &bletch is a list operator, and that
@bar isn't, and produce code like
@bar = bletch(:list(@foo));
But that doesn't mean that infix:==> has to get confused by a mere
function pointer if you call it for real and all. I don't see offhand
why there can't be an infix:==>. I suppose by the same argument
there could be an infix:<==, even though the compiler would never use
it when it could just pass a :list(...) instead. (Assuming for the
sake of argument that that splat list is always called "list", which
it isn't. But the compiler doesn't actually have to care about that.)
: What I'm asking is (i) wether this would actually be possible, and if so,
: then (ii) what it would probably look like in actual Perl6 code.
I don't know if it's possible, offhand. Well, sure, it's possible, but
I don't know if that's how things will come out.
: BTW: I guess something like
:
: my $result = @input ==>
: reduce &operator:==>, map {...}, @templates;
:
: where {...} would represent the curried closures, if only I knew how they
: are supposed to look like!
Well, you've got the precedence of = and ==> wrong, so you'd need some
parens around the pipelines, but that's a nit. The real question is
whether reduce is powerful enough to transitively make all the righthand
arguments of each ==> autocurry their own righthand arguments. I suspect
there's a way to write it, since we defined the splat list as just a
funky named parameter. Probably something like
my $result =
(@input ==>
reduce &operator:==>,
map { &*pack.assuming(:template($_)) }, @templates
);
Mind you, I wouldn't write such code anywhere I expected anyone with
less than a PhD to stumble across, even if I ain't got one myself.
Larry