Michael Lazzaro <[EMAIL PROTECTED]> writes:

> >> The only encompassing solution would seem to be to find a grammar rule
> >> by which map,grep,etc are unambiguously methods of Array, but can
> >> still be called in a fashion similar to [1].  That would, I suspect,
> >> satisfy everyone.
> 
> On Friday, December 13, 2002, at 03:07  AM, Piers Cawley wrote:
> > What's wrong with:
> >
> >     class Array {
> >         method grep ( &block ) {
> <snip>
> >     }
> >
> >     sub grep (Object $obj, @*ary) { @ary.grep($obj); }
> >
> > AFAICT, (modulo getting the laziness done right, this should allow
> > you to write)
> >
> >     grep { ... } @ary;
> >     grep /.../, @ary;
> 
> The only issue is that it still treats C<grep> as a universal sub -- 
> it's just giving you an identically named C<grep> method of Array,
> too. The ideal solution IMO is one that would remove C<map>, C<grep>,
> etc. _entirely_ from the main/core/Object, and leave it _solely_ as a
> method of Array.  Not because C<grep> is inherently evil, but because
> that would give us a completely generic way to make other "piped"
> grep-style functions, like C<part>, without polluting the core
> namespace.

To what end? Actually, if we have multimethods correctly sorted, one
could envisage a CunningCollection class:

    class CunningCollection {
        method grep ( Object $obj ) { .as_array.grep($obj) }
        sub grep ( Object $obj, CunningCollection $coll ) is exported {
            $coll.grep($obj);
        }
    }

And it's trivial to do the same with any other syntax-like generic
function you care to come up with, no need for an extra syntactic wart.

> 
> A (placeholder for a) R-to-L "dot-like" operator:
> 
>     grep {...} <- @foo;
> 
> would be intended to be the *exact* equiv of
> 
>     @foo.grep {...};
> 
> but with a reversed syntax for the invocant.  So grep-like functions
> could be nested, and new ones could be created by making new methods
> of Array, AND we could get rid of the "specialness" of the Perl5
> map/grep syntax -- the syntax isn't special to a few list-munging
> functions, it can be used for anything.
> 
>     @out = sort <- map {...} <- grep {...} <- @in;
> 
> So it's almost what we had in Perl5, but more regular.
> 
> Of course, maybe '<-' is just spelled post-"given", and we're done:
> 
>     @out = sort
>       given map { ... }
>         given grep { ... }
>           given @in;
> 
> Or would that be @out = .sort given .map ...etc...?

Both of your proposed options are, frankly, vile. The
multimethod/generic function approach has the advantage of putting the
'burden' of writing the generic function on the implementor rather
than on the user. Given that implementation happens once and something
is used countless times, that seems like a decent trade off to me.

Reply via email to