Luke Palmer skribis 2005-03-28  6:57 (-0700):
> We were discussing on #perl6, and thought that the feature:
>     sub foo () {
>         say "Blech";
>     }
>     { foo() } xx 5;

In the context of x, it makes even more sense. Especially if you
consider, for example, creating a random password:

    my $password = { any('a'..'z').pick } x 5;

This can be done with join+map, of course, but map really isn't the
right idiom visually, because you just want the closure to be called
repeatedly, not transform a list.

For reference, this is one way to do it with join and map:

    my $password = map { any('a'..'z').pick }, 1..5;

The thing that I find wrong with that is the range - 2..6 would have
accomplished the same thing. Only the 5 is important, the 1.. is not.

> Does the convenience (and obviousness when reading) enough to warrant
> adding a special case in this situation?

I certainly think so.

If a special case is not warranted, perhaps we can introduce another set
of x-ish operators, that do something special with the LHS, while x and
xx leave the LHS intact. They could be X and XX or *x and *xx, or
whatever. I'll use X and XX to demonstrate:

    @foo xx 5     # [EMAIL PROTECTED], 5 times
    { foo } xx 5  # 5 times the same closure

    @foo XX 5     # @foo's elements, repeated 5 times
    { foo } XX 5  # execute foo 5 times and collect its elements

IMHO, the special case is more useful, and using a temporary variable is
a good enough way to disambiguate in the rare case that you actually
want to repeat a closure. (Which with string x is even less of a
problem, because you don't often want five equal stringified closures


Reply via email to