Re: .trim and 'gilding the lilly'

2009-01-26 Thread Aristotle Pagaltzis
* Nicholas Clark n...@ccl4.org [2009-01-24 15:00]:
 But personally I feel that the added conceptual complexity of
 having over-ridable regexps, and in particular .ltrim and
 .rtrim methods with over-ridable regexps is not worth it.

Yeah. I have come around to this view as well.

In programming, everything we do is a special case of
something more general – and often we know it too quickly.
—Alan J. Perlis

I think this is a case of us overgeneralising `.trim` when
perfectly appropriate truly general ways of achieving the same
effect already exist – ie. `.subst`. The reason to have `.trim`
at all is that the use of `.subst` is awkward for the very common
case of wanting to trim both ends. But wanting to trim only a
single side is much rarer while simultaneously not being
appreciably awkward to do with `.subst`. (It’s more typing, but
not enough to matter for such a relatively rare thing.) Sticking
to a single common use-case eliminates the need for configuration
API, improving usability as a whole. Keep it simple.

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


Re: .trim and 'gilding the lilly'

2009-01-24 Thread Nicholas Clark
On Sat, Jan 24, 2009 at 04:55:44AM -0800, Ovid wrote:
 In chromatic's latest Perl 6 Design Minutes post 
 (http://use.perl.org/~chromatic/journal/38334), he writes 
 
Missing a discussion on.trim and gilding the lily. 
 
Nicholas: 
   * if I wanted PHP I know where to find it
 
 So there's a lot of context missing there and I'm unsure of what this 
 implies, but if it's deemed that .trim and friends are too much, so be it.  I 
 would submit that .chop is also a great candidate for removal (.chop?  Why is 
 it even in there?)
 
 Is there guidance on where to go from here or what the balance is between 
 convenience functions and the bare minimum?

My personal opinion, which I can remember, was that .trim itself was a useful
addition, as it's a good terse answer to the F.A.Q. How do I strip blank
space from the beginning/end of a string?

In particular, being able to trim both ends of the string at once efficiently
both in code terms and implementation terms solves the conundrum of this part
of the answer in the FAQ:

You can also write that as a single substitution, although it turns
out the combined statement is slower than the separate ones. That
might not matter to you, though.

s/^\s+|\s+$//g;


If there's one answer that is both elegant and fast, there is no longer a
compromise needed, nor do journeymen programmers waste time questing for a
better answer that masters know does not exist.*


But personally I feel that the added conceptual complexity of having
over-ridable regexps, and in particular .ltrim and .rtrim methods with
over-ridable regexps is not worth it. It's probably more typing than directly
doing a substitution on the string (and note, I don't qualify as part of
@Larry because I can't actually remember the Perl 6 syntax for any of this)
and I can't see any way it can be more efficient in implementation**. So I see
it as all downside, and no upside.

Perl 6 is a much bigger language than Perl 5. Often it makes me think of the
difference between C++ and C, including the standard libraries. C is small
enough that mortals stand a chance of remembering it all, and maybe even
mastering it. C++ (particularly with templates and their nuances) has always
felt as if the contest would end with me dying first (and that it was
cheating, because it was undead.)

All these individual operators *are* useful, *in context*. But it then becomes
a matter of remembering all the special cases for the special contexts. And
at that point, maybe it's faster to implement it using the memorable general
tools in your toolbox, rather than the correct special builtins.

It's like Lego, going wrong by adding all these special purpose bricks that do
only one thing. Or the Perl 5 documentation, to which everyone*** wants to add
a paragraph with their own little caveat, which had they known that particular
fact in advance would have saved them an hour or a day. But who reads the fine
manual any more, let alone remembers it?

Perl 6 hasn't reached the level of date_sunrise() or .ninth yet, nor will it
ever. But one of the criticisms of Perl 5, valid in context, is that unlike
some other languages, because there's more than one right way to do it, it's
difficult to bring in inexperienced programmers to look at a codebase, because
different house styles can be so different.

So be wary of adding ever more features to the language, because the trade off
is that everyone has to remember them, the language becomes fractionally
harder to learn, and fractionally harder to understand.


And no I didn't answer the question. I don't think that the discussion on the
call did either. And if left alone I can ramble that much, is anyone surprised
that chromatic can't manage to minute several people discussing it? :-)

Nicholas Clark

*   Maybe journeyman isn't the right term, but I mean the dangerous middle
state in
1: Apprentice - I have a lot to learn
2: Journeyman - I know nearly everything.
3: Master - You can never know everything.
Although the Victorian physicists weren't actually that dangerous.
**  I think it's likely that a both-ends trim might just be. Certainly one that
is implemented directly, rather than a pair of substitutions using the rules
engine. But this is micro-optimising, and likely beaten by a better
algorithm somewhere else.
*** For a small value of everyone. Heck. A big value of everyone would be
worse. We'd be spending all our time apologising about rejecting
enthusiastic contributions.


Re: .trim and 'gilding the lilly'

2009-01-24 Thread Ovid
- Original Message 

 From: Nicholas Clark n...@ccl4.org

 You can also write that as a single substitution, although it turns
 out the combined statement is slower than the separate ones. That
 might not matter to you, though.
 
 s/^\s+|\s+$//g;
 
 
 If there's one answer that is both elegant and fast, there is no longer a
 compromise needed, nor do journeymen programmers waste time questing for a
 better answer that masters know does not exist.*

You know, I rewrote .trim as:

  .sub 'trim' :method :multi(_)
  .local string s
  s = self
  s = 'trim_start'(s)
  s = 'trim_end'(s)
  .return(s)
  .end

I thought about the performance issue but opted for correctness and no 
duplicate code.  I figured it's trivial to speed up later, if need be.

Short of trying to figure out how to survey the CPAN and see what people are 
really doing with strings, I'm unsure of how to solve the useful/baggage 
dichotomy.  Maybe just going through and turning the Perl 5 Cookbook into a 
series of builtins ... :)

(Note that the latter suggestion was a joke.  Mostly)

I'd still opt for removing .chop, though.  I think only once have I ever seen 
it used appropriately.  All other times the user wanted .chomp.
 
Cheers,
Ovid



Re: .trim and 'gilding the lilly'

2009-01-24 Thread Nicholas Clark
On Sat, Jan 24, 2009 at 09:27:04AM -0800, Ovid wrote:

 You know, I rewrote .trim as:
 
   .sub 'trim' :method :multi(_)
   .local string s
   s = self
   s = 'trim_start'(s)
   s = 'trim_end'(s)
   .return(s)
   .end
 
 I thought about the performance issue but opted for correctness and no 
 duplicate code.  I figured it's trivial to speed up later, if need be.

Yes. Definitely correctness comes before optimisation

 I'd still opt for removing .chop, though.  I think only once have I ever seen 
 it used appropriately.  All other times the user wanted .chomp.

I've used it intentionally, but only because it's less typing than
s/.\z//m; (or whatever the canonically correct Perl 5 equivalent is)

The Perl 6 version is 1 character terser?

Nicholas Clark


Re: .trim and 'gilding the lilly'

2009-01-24 Thread chromatic
On Saturday 24 January 2009 05:56:03 Nicholas Clark wrote:

 And if left alone I can ramble that much, is anyone surprised
 that chromatic can't manage to minute several people discussing it?

Amusingly, you were the one who didn't minute it; I wasn't on the call that 
week.

-- c


Re: .trim and 'gilding the lilly'

2009-01-24 Thread Nicholas Clark
On Sat, Jan 24, 2009 at 10:58:51AM -0800, chromatic wrote:
 On Saturday 24 January 2009 05:56:03 Nicholas Clark wrote:
 
  And if left alone I can ramble that much, is anyone surprised
  that chromatic can't manage to minute several people discussing it?
 
 Amusingly, you were the one who didn't minute it; I wasn't on the call that 
 week.

Ah. That explains a lot. :-)

I can't keep up with the typing even with my mouth shut. I definitely can't
think, talk and type at the same time.

Nicholas Clark


Re: .trim and 'gilding the lilly'

2009-01-24 Thread Ovid
- Original Message 

 From: Nicholas Clark n...@ccl4.org

 I can't keep up with the typing even with my mouth shut. I definitely can't
 think, talk and type at the same time.

For what it's worth, I have trouble doing more than *one* of those at once.

In any event, it's nice to get a slightly better idea of what others are 
thinking.

(And a bit thanks to chromatic for regularly posting those updates.  It helps a 
lot)


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