in reply to Re: Pipe dream
in thread Pipe dream

If you're going to write a grep iterator, have the return value be an empty list on element failure. This allows failures to be removed from the list instead of just being substituted with undefined values. In fact, grep is just a special case of map.

grep /^42/ map { /^42/ ? $_ : () } sub { /^42/ ? $_ : () }

Replies are listed 'Best First'.
Re^3: Pipe dream
by Roy Johnson (Monsignor) on Sep 09, 2005 at 18:09 UTC
    undef is a special value in the scheme I've proposed. All results are taken in scalar context (assigned to $_), since this is a line-piping scheme. If the grep step returns undef, the iterator goes back to the first sub to try a new line. undefs do not show up in the output stream.

    You could use an empty list as your no-line-returned indicator, but it would require you evaluating the subs in list context and then turning the returned value into a scalar afterward. That would be problematic for some common pipe actions, like reading one line at a time from a file (in list context, the whole file would be read).


    Caution: Contents may have been coded under pressure.
      Oh. I don't normally privilege undef that way. Its another value, just like other values.