Michael Lazzaro <[EMAIL PROTECTED]> writes:

> On Wednesday, December 11, 2002, at 06:56  PM, Simon Cozens wrote:
> > [EMAIL PROTECTED] (Michael Lazzaro) writes:
> >> Welllll... yes and no.  You can make the same argument for operators
> >> upon scalars, for example, since 'scalar' is arguably no more
> >> universal than 'array'.  And we could easily use that argument to
> >> remove *all* builtins, period:
> >
> > Now you're getting the idea.
> 
> Yes.  But Java sucks.  Me no like make Perl like Java.
> 
> I would still like to be able to do things in Perl6 like:
> 
>      @out = sort map {...} grep { ... } @in;    # [1]
> 
> Even though that technically means having sort, map, grep as builtins.
> 
> We can make that
> 
>      @out = @in.grep({...}).map({...}).sort;    # [2]
> 
> if we want to grind our OO axe, but I find that syntax disappointing.
> I like that the idea is important enough in Perl to have it's own
> grammar, but I realize the problem of namespace pollution involved in
> having a bunch of builtins called grep, map, whatever.
> 
> 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.

What's wrong with:


    class Array {
        method grep ( &block ) {
            foreach .values {
                yield $_ if &block($_);
            }
        }

        method grep ( Rule $rule ) {
            foreach .values {
                yield $_ if /$rule/;
            }
        }

        ...
    }

    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;

Writing the 

    grep foo($_), @ary 

form in Perl 6 is left as an exercise to the interested reader, I get
the feeling it's going to have to wait until you can write 'special
form' subs.

    sub grep ( rx/<expr>/ $expr, @*ary ) { 
        grep $expr.as_block, @ary 
    }

Might be one approach, but I'm waiting for Larry.  

Reply via email to