Re: Trimming arrays

2009-01-14 Thread Ovid
- Original Message 

 From: Patrick R. Michaud pmich...@pobox.com
  I would expect this to be roughly equivalent to:
  
  for @array { $_ .= trim; }
  
  For an array of hashes, this would result in each hash element
  of @array being replaced with a reference to an array of the 
  trimmed string representation of the hash.
 
 Oops -- I over-referenced here.  The corrected form:
 
 For C @array».=trim ,  each element of @array would be replaced 
 with its trimmed string representation.  If @array starts out
 as an array of hashes, then @array».=trim would leave @array 
 with the trimmed stringification of each hash element.

I don't get this.  If I can't call @array.trim, I certainly can't call 
%hash.trim.  If you mean stringify the hash and then trim *that*, what does 
that mean?  We get that all the time in Perl 5 and I've written tests to catch 
things like HASH(0x80631d0) showing up in templates.  This is probably some of 
the most useless behavior in Perl 5 and I don't want to see it propagate to 
Perl 6.  Either that should die violently or it should do something useful and 
I'm unsure of what would be useful here.

A hash is basically a set of pairs.  You *can't* trim the keys because they're 
supposed to be unique.  Otherwise, what does it mean to trim { '  foo' = 1, 
'foo  ' = 2 }?  So we can only trim values, but what if the values are pairs?  
Then wouldn't you effectively be apply the hyperop recursively throughout the 
data structure?  That might be expensive and have unwanted side-effects (what 
do you mean my hash had a reference to your ORM data?).

 
Cheers,
Ovid
--
Buy the book - http://www.oreilly.com/catalog/perlhks/
Tech blog- http://use.perl.org/~Ovid/journal/
Twitter  - http://twitter.com/OvidPerl
Official Perl 6 Wiki - http://www.perlfoundation.org/perl6



Re: Trimming arrays

2009-01-14 Thread Patrick R. Michaud
On Wed, Jan 14, 2009 at 03:37:57AM -0800, Ovid wrote:
 - Original Message 
  From: Patrick R. Michaud pmich...@pobox.com
  
  Oops -- I over-referenced here.  The corrected form:
  
  For C @array».=trim ,  each element of @array would be replaced 
  with its trimmed string representation.  If @array starts out
  as an array of hashes, then @array».=trim would leave @array 
  with the trimmed stringification of each hash element.
 
 I don't get this.  If I can't call @array.trim, I certainly can't call 
 %hash.trim.  If you mean stringify the hash and then trim *that*, 
 what does that mean?  

You _can_ call @array.trim -- it returns the trimmed value of the
stringified value of the array.  Arrays stringify to all of their
elements separated by a space (if the element doesn't already
end with some kind of space, although Rakudo doesn't implement
that part yet).

Stringifying a hash in Perl 6 produces a string with keys and values
separated by tab characters, and each pair terminated by a newline.
See Synopsis 2, beginning with In order to interpolate an entire hash...

Pm


Re: Trimming arrays

2009-01-13 Thread Aristotle Pagaltzis
* Ovid publiustemp-perl6langua...@yahoo.com [2009-01-13 00:35]:
* Larry Wall la...@wall.org [2009-01-13 00:25]:
 It should probably say No such method.  We have hyperops now
 to apply scalar operators to composite values explicitly:

 @array».=trim


 Won't that fail with 'No such method' on an array of hashes? Or
 are hyperops applied recursively?

I would *NOT* want a simple `».` to recurse down into a data
structure.

But I wonder if it’s reasonable to expect that hypermethodcalls
will collect their return values in an array. Then trimming the
values of the hashes in an array would be simply

@array».values».=trim;

Imagine writing this in another language.

Regards,
-- 
Aristotle Pagaltzis // http://plasmasturm.org/


Re: Trimming arrays

2009-01-13 Thread Patrick R. Michaud
On Mon, Jan 12, 2009 at 03:30:05PM -0800, Ovid wrote:
  From: Larry Wall la...@wall.org
  :   my @array = '   foo   ', '   bar  ';
  :   @array .= trim;
  :   say @array.perl;
  :  
  : And what if I have an array of hashes of hashes of arrays?
  : 
  : Currently you can call 'trim' on arrays, but it's a no-op.  
  : Similar issues with chomp and friends.
 
  It should probably say No such method.  

S29 doesn't document .trim, but like other similar methods I
would expect it to be defined on CAny.  If so, I would
expect the output of the above to be  C [foo  bar]\n .

  We have hyperops now to apply
  scalar operators to composite values explicitly:
  
  @array».=trim
 
 Won't that fail with 'No such method' on an array of hashes? 
 Or are hyperops applied recursively?

I would expect this to be roughly equivalent to:

for @array { $_ .= trim; }

For an array of hashes, this would result in each hash element
of @array being replaced with a reference to an array of the 
trimmed string representation of the hash.

Pm


Re: Trimming arrays

2009-01-13 Thread Moritz Lenz
Ovid wrote:
 What should this output?
 
   my @array = '   foo   ', '   bar  ';
   @array .= trim;
 
   say @array.perl;
  
 And what if I have an array of hashes of hashes of arrays?
 
 Currently you can call 'trim' on arrays, but it's a no-op.  Similar issues 
 with chomp and friends.

I think calling a Str method on an Array should force the array to
stringify, then call that method.

If you want to call it on each item, you can use @array.map: { .=trim };
 or maybe @array».=trim

Cheers,
Moritz


Re: Trimming arrays

2009-01-13 Thread Patrick R. Michaud
On Tue, Jan 13, 2009 at 08:19:25AM -0600, Patrick R. Michaud wrote:
   @array».=trim
  
  Won't that fail with 'No such method' on an array of hashes? 
  Or are hyperops applied recursively?
 
 I would expect this to be roughly equivalent to:
 
 for @array { $_ .= trim; }
 
 For an array of hashes, this would result in each hash element
 of @array being replaced with a reference to an array of the 
 trimmed string representation of the hash.

Oops -- I over-referenced here.  The corrected form:

For C @array».=trim ,  each element of @array would be replaced 
with its trimmed string representation.  If @array starts out
as an array of hashes, then @array».=trim would leave @array 
with the trimmed stringification of each hash element.

Pm


Re: Trimming arrays

2009-01-12 Thread Larry Wall
On Mon, Jan 12, 2009 at 03:05:21PM -0800, Ovid wrote:
: What should this output?
: 
:   my @array = '   foo   ', '   bar  ';
:   @array .= trim;
: 
:   say @array.perl;
:  
: And what if I have an array of hashes of hashes of arrays?
: 
: Currently you can call 'trim' on arrays, but it's a no-op.  Similar issues 
with chomp and friends.

It should probably say No such method.  We have hyperops now to apply
scalar operators to composite values explicitly:

@array».=trim

Larry


Re: Trimming arrays

2009-01-12 Thread Ovid
- Original Message 

 From: Larry Wall la...@wall.org

 :   my @array = '   foo   ', '   bar  ';
 :   @array .= trim;
 : 
 :   say @array.perl;
 :  
 : And what if I have an array of hashes of hashes of arrays?
 : 
 : Currently you can call 'trim' on arrays, but it's a no-op.  Similar issues 
 with chomp and friends.
 
 It should probably say No such method.  We have hyperops now to apply
 scalar operators to composite values explicitly:
 
 @array».=trim


Won't that fail with 'No such method' on an array of hashes? Or are hyperops 
applied recursively?

Cheers,
Ovid
--
Buy the book - http://www.oreilly.com/catalog/perlhks/
Tech blog- http://use.perl.org/~Ovid/journal/
Twitter  - http://twitter.com/OvidPerl
Official Perl 6 Wiki - http://www.perlfoundation.org/perl6