News for hackers. Stuff that matters.
Re: How I learnt to love Perl by newt0311 on Sunday December 07, 2008 @10:01PM (#389421)
On my computer:

   $  python -m timeit -s 'import re' '"".join(chr(i) for i in range(256) if re.match(r"\w", chr(i)))'
   1000 loops, best of 3: 841 usec per loop
   $ python -m timeit -s 'import re' "''.join(sorted(c for c in map(chr, range(256)) if re.match(r'\w', c)))"
   1000 loops, best of 3: 792 usec per loop
   $ python -m timeit -s 'import re' "''.join(sorted(c for i in range(256) for c in (chr(i),) if re.match(r'\w', c)))"
   1000 loops, best of 3: 921 usec per loop
Your DRY variants (against which my complaint was) are not faster.

As to the principle of DRY: Which is easier to debug? my code or your code? mine is certainly easier to read. Principles are the average of large amounts of experience. Its never a good idea to ignore them completely but just blindly following them without keeping in mind the context they were formulated in is just as stupid.

PS: Your version with the re.findall is faster but requires the user to bring up a definition of string.maketrans and re.findall, both things that an experienced python coder would know but its another little thing that they now have to track. OTOH, the generator expressions (especially mine) are straightforward and blindingly obvious as to their purpose.

Re: How I learnt to love Perl by draegtun on Monday December 08, 2008 @05:39AM (#389717)
I find the Perl and especially the Ruby versions are a bit easier to read than any of those Python examples.

If u like left to right chains then Perl can also be written like so...

  use autobox::Core;

  say [ 0..255 ]->map( sub { chr } )->grep( sub { m/\w/ } )->sort->join('');

/I3az/