xx and closures

1 view
Skip to first unread message

Luke Palmer

unread,
Mar 28, 2005, 8:57:01 AM3/28/05
to Language List
We were discussing on #perl6, and thought that the feature:

sub foo () {
say "Blech";
}
{ foo() } xx 5;

Would be useful. Since it is dwimmery, and you wouldn't want the
closure to execute if you didn't know it was there, we decided that it
would probably be best if this were distinct from:

my $closure = { foo() };
$closure xx 5;

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

Luke

Juerd

unread,
Mar 28, 2005, 9:08:25 AM3/28/05
to Luke Palmer, perl6-l...@perl.org
Juerd skribis 2005-03-28 16:05 (+0200):
> 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;

I wonder now if that can just be

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

(No reason to not implement the requested feature, though)


Juerd
--
http://convolution.nl/maak_juerd_blij.html
http://convolution.nl/make_juerd_happy.html
http://convolution.nl/gajigu_juerd_n.html

Juerd

unread,
Mar 28, 2005, 9:05:16 AM3/28/05
to Luke Palmer, perl6-l...@perl.org
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 # \@foo, 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
:).)

Eirik Berg Hanssen

unread,
Mar 28, 2005, 9:01:02 PM3/28/05
to perl6-l...@perl.org
Juerd <ju...@convolution.nl> writes:

> I wonder now if that can just be
>
> my $password = any('a'..'z') x 5;

Wouldn't that generate a junction, and so need a .pick?

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


Or perhaps just leave it a junction, to use as a generator:

my $any_password = any('a'..'z') x 5;
my %password = map { $_ => $any_password.pick } @users;


... or am I missing something?


Eirik
--
Skill without imagination is craftsmanship and gives us many useful objects
such as wickerwork picnic baskets. Imagination without skill gives us modern
art.
-- Tom Stoppard

Reply all
Reply to author
Forward
0 new messages