Re: x, xx, and negative counts

2007-05-29 Thread Andy Armstrong

On 29 May 2007, at 19:21, Mark J. Reed wrote:


My expectation before reading the delta was that negative counts
would do a reversal:

123 x -1 = 321

('a', 'b', 'c') xx -3 = ('c', 'b', 'a', 'c', 'b', 'a', 'c', 'b', 'a');

I don't know why I think that makes sense, but it was honestly my
first thought.  Does it make sense to anyone else?  Is there a
compelling reason I'm missing for having negative values behave as if
they were zero rather than adding some other potentially useful
functionality?


You're going to surprise people who use x to create padding strings  
if a negative count produces a padding string of the same length.


--
Andy Armstrong, hexten.net



Re: x, xx, and negative counts

2007-05-29 Thread Larry Wall
On Tue, May 29, 2007 at 02:21:37PM -0400, Mark J. Reed wrote:
: My expectation before reading the delta was that negative counts
: would do a reversal:
: 
: 123 x -1 = 321
: 
: ('a', 'b', 'c') xx -3 = ('c', 'b', 'a', 'c', 'b', 'a', 'c', 'b', 'a');
: 
: I don't know why I think that makes sense, but it was honestly my
: first thought.  Does it make sense to anyone else?  Is there a
: compelling reason I'm missing for having negative values behave as if
: they were zero rather than adding some other potentially useful
: functionality?

The main rationale for going with null return is that the biggest use of
replication has generally been something like:

say $foo, ' ' x (20 - $foo.width), $bar

and it would be counterproductive to degrade to negative spaces in
such a case.  (This is also the rationale for not returning failure
on negative counts.)

Note, however, that these are just multimethods, so if you defined a
variant that accepted a count of type Int where *..-1, it would come
earlier in the candidate list than the normal count of type Int.
So you could give it different semantics if you like.

Arguably, in Perl 6 people might instead write

say $foo.fmt(%20s), $bar

but just because people can write it that way doesn't mean they will.
(Plus that approach doesn't work if you want to count tabs.)

And generally, I think

@list xx -1

is less readable than

@list.reverse

so I don't feel inclined to include it as another Way To Do It.

Larry


Re: x, xx, and negative counts

2007-05-29 Thread Mark J. Reed

On 5/29/07, Larry Wall [EMAIL PROTECTED] wrote:

The main rationale for going with null return is that the biggest use of
replication has generally been something like:

say $foo, ' ' x (20 - $foo.width), $bar

and it would be counterproductive to degrade to negative spaces in
such a case.  (This is also the rationale for not returning failure
on negative counts.)


OK.  That makes sense.  Thanks.


And generally, I think

@list xx -1

is less readable than

@list.reverse

so I don't feel inclined to include it as another Way To Do It.


I wholeheartedly agree on the readability issue.  Nor was I setting
out to add a new Way To Do anything; it's just that my automatic
expectation for what would happen (which I'm not trying to defend as
particularly logical) was being violated, and P6 hasn't done that too
often.  So I prodded to see what was going on in @larry.brain.  Your
explanation is all I needed to shush the little grumbles in my head.
Thanks again!

--
Mark J. Reed [EMAIL PROTECTED]


Re: x, xx, and negative counts

2007-05-29 Thread Chas Owens

On 5/29/07, Mark J. Reed [EMAIL PROTECTED] wrote:

 My expectation before reading the delta was that negative counts
would do a reversal:

123 x -1 = 321

('a', 'b', 'c') xx -3 = ('c', 'b', 'a', 'c', 'b', 'a', 'c', 'b', 'a');

I don't know why I think that makes sense, but it was honestly my
first thought.  Does it make sense to anyone else?  Is there a
compelling reason I'm missing for having negative values behave as if
they were zero rather than adding some other potentially useful
functionality?

snip

In Perl 5 I commonly say things like

sub zeropad (
   my ($len, $s) = @_;
   my $slen = length $s;
   carp the string $s is longer than the specified length $len if
$slen  $len;
   return = 0 x  ($len - length($str)) . $str;
);

Which is roughly equivalent to doing a sprintf with the format %0${len}d.