in reply to Re^2: RFC: Tool::Box
in thread RFC: Tool::Box

Why do you define the anonymous subroutine every time you call the function? Your anonymous subs aren't closures - they're just anon subs. They don't close over anything, let alone anything in @_. Your functions would be much faster if you define the subrefs ahead of time. Your functions would also be simpler because they just dispatch to the subref or return it, as needed.

Another item - you make the same mistake nearly every unique'ing function makes: what if I want to find unique objects or hashrefs? Much better is @uniq{@_} = @_; values %uniq. That way, you also respect any overloaded stringification I might use. Your uniq_ordered() function doesn't have this mistake.

Being right, does not endow the right to be rude; politeness costs nothing.
Being unknowing, is not the same as being stupid.
Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Replies are listed 'Best First'.
Re^4: RFC: Tool::Box
by Limbic~Region (Chancellor) on Nov 21, 2004 at 14:25 UTC
    dragonchild,
    I think the fact that the code morphed from being nothing but closures to being either/or depending on if called with an argument list made the code confusing. The commentary and the POD did explain this however. You say that none of the anonymous subs close over anything.
    sub ave { my $tot; my $cnt; my $find_ave = sub { $cnt += @_; $tot += $_ for @_; return $cnt ? $tot / $cnt : undef; }; return @_ ? $find_ave->( @_ ) : $find_ave; }
    This is true if ave is called like print ave(5, 10, -1, 7.3); as the anonymous sub is executed and the value 5.325 is returned - that's the way it was designed. If on the other hand, the code is called like my $ave = ave();, there most certainly is a closure returned - it closes over both $cnt and $tot. I am guessing the ternary use of @_ made you think I was trying to close over something in @_? Perhaps it would have been more clear (and better) if I had not modified the original code and re-written it as:
    sub ave { my ($cnt , $tot); if ( @_ ) { $cnt += @_; $tot += $_ for @_; return $cnt ? $tot / $cnt : undef; } else { return sub { $cnt += @_; $tot += $_ for @_; return $cnt ? $tot / $cnt : undef; }; } }

    Another item - you make the same mistake nearly every unique'ing function makes...
    Thanks. While the POD doesn't make any promises like that, I think it would be better off that way.

    Cheers - L~R

    Updated: I was grumpy when I first replied as I had just woken up. Modified tone slightly to be less combative.

      <lightbulb>Now I see what you're doing.</lightbulb>

      That kind of semantics, though ... that's very non-intuitive. Very cool, but still non-intuitive. I didn't see a lot about that in the POD. Imho, that should be a very prominent section.

      Being right, does not endow the right to be rude; politeness costs nothing.
      Being unknowing, is not the same as being stupid.
      Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
      Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

        dragonchild,
        As far as I am concerned, this is still very much in the elementary stages of development so making the code and documentation clearer is a given. As diotalevi pointed out, it may not work that great either. If someone does:
        my @ints = grep { int($_) == $_ } @numbers; my $ave = ave( @ints );
        Expecting to get back the average are going to be suprised if it returns a function (there were no integers in @numbers). Explaining in the POD may not be enough and this is something that requires careful consideration.

        Thanks for the feedback.

        Cheers - L~R