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

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.

Replies are listed 'Best First'.
Re^5: RFC: Tool::Box
by dragonchild (Archbishop) on Nov 22, 2004 at 13:29 UTC
    <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

        I would be thrilled if you would rename your average function from ave() to avg(). I keep reading 'avenue' when you write 'ave' and I note that the common abbreviation seems to be avg().
        I was thinking about this. If I wanted to maintain a running average or running list of unique items (ordered or not) ... I'm probably designing my program around it. Therefore, setting some flag is probably ok with me. What would be wrong with:
        use Tool::Box qw( :running_totals )

        That was, you provide normal functions as the default (with $ave returning undef or 0 in your example above). But, if someone wants the cool running-totals feature, they can turn it on.

        Another possibility would be to provide Tool::Box and Tool::Box::Running_Total. *shrug* I'm just brainstorming here ...

        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.