[Date Prev][Date Next][Thread Prev][Thread Next] [Search] [Date Index] [Thread Index]

Re: [FWP] Something like "grepfirst"



At 9:54 AM -0400 on 7/26/99, John Porter wrote:
>Roland wrote:
>> What is the shortest code for finding the first
>> existing directory in a path of directories?
>
>Functional programming to the rescue:
>
>
>sub first_existing_dir {
>  my $d=shift or die;
>  -d $d ? $d : first_existing_dir(@_)
>}
>
>my $dir = first_existing_dir( 'dir1', 'dir2', $default );

I can trim 3 (real) bytes:

sub first_existing_dir {
  my $d=shift or die;
  -d $d ? $d : &first_existing_dir
}

Of course, it's still a nice recursive function... perfect for that one
time the user gives you 10,000 things to check <g>.

For a 11 less bytes, eleminate the recursion:

sub first_existing_dir {
  d: my $d=shift or die;
  -d $d ? $d : goto d
}

Now down to 73 bytes, including indentation and newlines. I must confess I
never knew you could do that with ?.



==== Want to unsubscribe from Fun With Perl?  Well, if you insist...
==== Send email to <fwp-request@technofile.org> with message _body_
====   unsubscribe