Re: xx and re-running

2004-07-26 Thread Matthew Walton
Larry Wall wrote:
The rand function may be a bad example, since it's by nature a
generator, and you should maybe have to work harder to get a single
value out of it.  We haven't really said what $fh xx 100 should do,
for instance.  I guess the real question is whether xx supplies a
list context to its left argument (and whether rand pays attention
to list context).  These might produce very different results:
@foo = (rand);
@bar = (+rand);
On the other hand, history has rand producing a scalar, so arguably we
should stick with that.
One supposes that it's not entirely unreasonable to have rand in list 
context produce an infinite (lazy) list of random numbers, successively 
drawn from the generator.

The results of
@foo = *(rand);
(if the syntax is right) in that case would of course be in the 'enough 
rope to hang yourself' category. But it might be handy to be able to say

my ($rand1, $rand2, $rand3) = (rand)[4..6];
although I'm sure some would argue that this is needlessly obfuscated, 
and that

my ($rand1, $rand2, $rand3) = (rand, rand, rand);
would be better. Maybe it would.
This random thought was brought to you by the demons of lunchtime idleness.



Re: xx and re-running

2004-07-24 Thread Larry Wall
On Thu, Jul 22, 2004 at 02:48:59PM -0600, Luke Palmer wrote:
: JOSEPH RYAN writes:
:  When I think about your description of xxx, I 
:  summarized it in my head as Call a coderef a certain
:  number of times, and then collect the results.  
:  That's pretty much what map is, except that xxx is 
:  infix and map is prefix.
:  
:  
:  @results = { ... } xxx 100;
:  @results = map { ... } 1.. 100;
:  
:  Doesn't seem that special to me.
: 
: And adding to that the definition of a unary hyper operator:
: 
: [EMAIL PROTECTED] == map { §$_ } @list
: 
: It seems that the rand problem could be solved this way:
: 
: my @nums = rand« (100 xx 100);

The rand function may be a bad example, since it's by nature a
generator, and you should maybe have to work harder to get a single
value out of it.  We haven't really said what $fh xx 100 should do,
for instance.  I guess the real question is whether xx supplies a
list context to its left argument (and whether rand pays attention
to list context).  These might produce very different results:

@foo = (rand);
@bar = (+rand);

On the other hand, history has rand producing a scalar, so arguably we
should stick with that.

Larry


Re: xx and re-running

2004-07-23 Thread Michele Dondi
On Thu, 22 Jul 2004, JOSEPH RYAN wrote:

 When I think about your description of xxx, I 
 summarized it in my head as Call a coderef a certain
 number of times, and then collect the results.  
 That's pretty much what map is, except that xxx is 
 infix and map is prefix.
 
 @results = { ... } xxx 100;
 @results = map { ... } 1.. 100;
 
 Doesn't seem that special to me.

It isn't: the difference is not practical in nature but 
aesthetical/phylosophical. Even if behind the scenes it may not be really 
so, the latter conveys the idea of creating a list to actually ignore its 
elements. So that while map() would be most always what one really wants, 
there may be fewer cases in which an operator like the one suggested by 
the OP would be the Right Tool(TM)...

Well, after all one thing that I've definitely understood about Perl6 is
that it will make easy to construct such custom operators on a per-user 
basis. But IMHO at least some of them would fit in well as predefined 
functions/operator or companions in crime of map() and grep() ;-)

Quite similarly, for example, I'd like to have a fold() function like the 
one that is available in many functional programming languages, a la:

  my $tot = fold 0, { + }, 1..10; # 55
  my $fact = fold 1, { * }, 2..5; # 120

(i.e. please DO NOT point out that there other WTDI: it's obvious that 
there are...)


Michele
-- 
SILVIO CLEPTOMANE
- Scritta su un muro, 
  Via F. Sforza, Milano


Re: xx and re-running

2004-07-23 Thread Brent 'Dax' Royal-Gordon
Michele Dondi wrote:
Quite similarly, for example, I'd like to have a fold() function like the 
one that is available in many functional programming languages, a la:

  my $tot = fold 0, { + }, 1..10; # 55
  my $fact = fold 1, { * }, 2..5; # 120
Those blocks would be a syntax error; the appropriate way to do that 
would be to refer to the operator by its proper name:

 my $tot = fold 0, infix:+, 1..10;
OTOH, Perl 5 already has List::Util::reduce:
use List::Util;
my $tot=reduce { $a + $b } 1..10;
my $fact=reduce { $a * $b } 2..5;
Your fold() can be trivially expressed with reduce(), IIUC.  I would 
hope that Perl 6 will have reduce() as well--perhaps even in a form that 
doesn't require using List::Util explicitly.

--
Brent Dax Royal-Gordon [EMAIL PROTECTED]
Perl and Parrot hacker
Oceania has always been at war with Eastasia.


Re: xx and re-running

2004-07-23 Thread Luke Palmer
Michele Dondi writes:
 Quite similarly, for example, I'd like to have a fold() function like the 
 one that is available in many functional programming languages, a la:
 
   my $tot = fold 0, { + }, 1..10; # 55
   my $fact = fold 1, { * }, 2..5; # 120
 
 (i.e. please DO NOT point out that there other WTDI: it's obvious that 
 there are...)

Well, Perl 6 is coming with one of those as a builtin, called Creduce
(see List::Util).  But you can't quite use a shorthand syntax like
yours.  You have to say either:

my $tot  = reduce { $^a + $^b } 1..10;  # 55

Or:

my $tot  = reduce operator:+ == 1..10; # 55

(The == was added as documentation; it could have been a comma as
well).

Luke


Re: xx and re-running

2004-07-23 Thread Michele Dondi
On Thu, 22 Jul 2004, Luke Palmer wrote:

 And adding to that the definition of a unary hyper operator:
 
 [EMAIL PROTECTED] == map { $_ } @list
 
 It seems that the rand problem could be solved this way:
 
 my @nums = rand (100 xx 100);

Huh?!? While not so bad (apart the unicode operator I still can't help 
feeling uneasy with), do you really think it would be intuitive? For one 
thing, the symmetry of the xx operator doesn't make it immediate to tell 
which of its arguments is the one that will be feed to rand() and which is 
the iterator...


Michele
-- 
you need a brain hack. or a brain of any sort. try a nematode first. the
small incremental improvement won't be such a shock. then you can
graduate to segmented worm brains.
- Uri Guttman on CPLM, Array Sort Using Regex Matching Fails


Re: xx and re-running

2004-07-23 Thread Michele Dondi
On Fri, 23 Jul 2004, Brent 'Dax' Royal-Gordon wrote:

 Those blocks would be a syntax error; the appropriate way to do that 
 would be to refer to the operator by its proper name:
 
   my $tot = fold 0, infix:+, 1..10;

Well, I suspected that. The matter is I still know too few concretely 
about Perl6... [ashamed - slowly learning though!]
 
 OTOH, Perl 5 already has List::Util::reduce:

This has already been pointed out by another poster. As I said though, I'd 
like to have such a beast in the core, for some reasonable meaning of 
core (taking into account recent discussion about the matter!) In 
particular I'd rather prefer not to have to Cuse yet another module for 
such a useful beast!

 Your fold() can be trivially expressed with reduce(), IIUC.  I would 

You UC! ;-)

 hope that Perl 6 will have reduce() as well--perhaps even in a form that 
 doesn't require using List::Util explicitly.

That's exactly what I mean, and I *fully* agree!!


Michele
-- 
[about penis-enlargement spam]
Suppose you (applies to male readers only!) had taken advantage
of every offer of the above type received, and all had worked as claimed.
Estimate how long it would be.
- Derek Holt on sci.math (slightly edited)


Re: xx and re-running

2004-07-23 Thread Michele Dondi
On Fri, 23 Jul 2004, Luke Palmer wrote:

 Well, Perl 6 is coming with one of those as a builtin, called Creduce
 (see List::Util).  But you can't quite use a shorthand syntax like
 yours.  You have to say either:

Cool, that's what I wanted to know. Taking into account both this 
circumstance and the other replies, it seems I wasn't the only one to 
consider this useful enough to deserve being a builtin...


Michele
-- 
 A mathematically-inclined glove-fetishist [...]
What a wonderful introduction to a puzzle!
- Richard Heathfield in sci.math, Re: Gloves in the dark


xx and re-running

2004-07-22 Thread James Mastros
Recently on perlmonks, at http://perlmonks.org/index.pl?node_id=375255, 
someone (DWS, actually) brought up the common error of expecting x (in 
particular, listy x, which is xx in perl6) to not create aliases.  What 
he was doing in particular, I don't have any expectation of making it 
work, but what about the also-common problem of C @randoms = (int rand 
100) xx 100 ?  In perl5, this picks one random integer between 0 and 
99, and copies it 100 times -- not what was intended.  The best way to 
do this is C my @randoms = map {int rand 100} 0..100; , which is 
rather yucky -- conceptually, you aren't trying to transform one list 
into another.  OTOH, C my @randoms; push @randoms, int rand 100 for 
0..100  is even yuckier.

Perhaps if the LHS of a xx operator is a closure, it should run the 
closure each time around... or perhaps there should be an xxx operator. 
 (Both suggestions from BrowserUk's reply, 
http://perlmonks.org/index.pl?node_id=375344).  The former raises the 
question of what you do if you really want to repeat a coderef, and the 
later raises the possibly of being blocked (really), and starts to 
become confusing -- the difference between x and xx is sensical -- the 
former repeats one thing, the later many... but what's the reasoning for 
xxx, other then that it's like xx?  How will users be able to remember 
which is which?

-=- James Mastros,
theorbtwo


Re: xx and re-running

2004-07-22 Thread JOSEPH RYAN
- Original Message -
From: James Mastros [EMAIL PROTECTED]
Date: Sunday, July 18, 2004 5:03 am
Subject: xx and re-running

 Recently on perlmonks, at 
 http://perlmonks.org/index.pl?node_id=375255, 
 someone (DWS, actually) brought up the common error of expecting x 
 (in 
 particular, listy x, which is xx in perl6) to not create aliases.  
 What 
 he was doing in particular, I don't have any expectation of making 
 it 
 work, but what about the also-common problem of C @randoms = (int 
 rand 
 100) xx 100 ?  In perl5, this picks one random integer between 0 
 and 
 99, and copies it 100 times -- not what was intended.  The best 
 way to 
 do this is C my @randoms = map {int rand 100} 0..100; , which is 
 rather yucky -- conceptually, you aren't trying to transform one 
 list 
 into another.  OTOH, C my @randoms; push @randoms, int rand 100 
 for 
 0..100  is even yuckier.
 
 Perhaps if the LHS of a xx operator is a closure, it should run 
 the 
 closure each time around... or perhaps there should be an xxx 
 operator. 
  (Both suggestions from BrowserUk's reply, 
 http://perlmonks.org/index.pl?node_id=375344).  The former raises 
 the 
 question of what you do if you really want to repeat a coderef, 
 and the 
 later raises the possibly of being blocked (really), and starts to 
 become confusing -- the difference between x and xx is sensical -- 
 the 
 former repeats one thing, the later many... but what's the 
 reasoning for 
 xxx, other then that it's like xx?  How will users be able to 
 remember 
 which is which?=

When I think about your description of xxx, I 
summarized it in my head as Call a coderef a certain
number of times, and then collect the results.  
That's pretty much what map is, except that xxx is 
infix and map is prefix.


@results = { ... } xxx 100;
@results = map { ... } 1.. 100;

Doesn't seem that special to me.

- Joe



Re: xx and re-running

2004-07-22 Thread Luke Palmer
JOSEPH RYAN writes:
 When I think about your description of xxx, I 
 summarized it in my head as Call a coderef a certain
 number of times, and then collect the results.  
 That's pretty much what map is, except that xxx is 
 infix and map is prefix.
 
 
 @results = { ... } xxx 100;
 @results = map { ... } 1.. 100;
 
 Doesn't seem that special to me.

And adding to that the definition of a unary hyper operator:

[EMAIL PROTECTED] == map { $_ } @list

It seems that the rand problem could be solved this way:

my @nums = rand (100 xx 100);

Luke