SPUG: Grep syntax

Eric Wilhelm scratchcomputing at gmail.com
Fri Jun 15 00:20:41 PDT 2007

# from Bill Campbell
# on Thursday 14 June 2007 11:39 pm:

>grep { $start0=0 unless $_ == 0; } @a;
>if ( ! $start0 ) { ...


Perhaps that means:

  my $start0 = ! grep({$_} @a);
  unless($start0) { ...

But that's still awfully non-un-negatedified.

  if(grep({$_} @a)) { ...

Isn't it?

And wasn't this a discussion about how you can't do stupid stuff in 
python?  There's nothing perlish about void-context greps and backwards 
negated logic.  IME Perl requires you to do silly things less often 
than other languages.  If the language supports it, you can just say 
what you mean.

Reading code out loud (or to yourself) is always a good thing.

The original code reads like "Set variable to true.  Looping through 
list (wtf? grep makes no sense here!), uh, anyway... looping through 
list, make variable false unless the current element is false.  Now if 
the variable is not true ... Uh, if ... uh nothing, no! something in 
the list was true, then it is false, no true, so ... now what? ... oh! 
if something in the list was true, then do this."

Where the idiomatic code reads as: "if something in the list is true, do 

So, "idioms are good, bad code is bad", or something simple like that.

Like a lot of people, I was mathematically abused as a child.
--Paul Graham

More information about the spug-list mailing list