I am trying to name what I think is a new idea for a higher-order function. To the important part, here is the code in Python and Haskell to demonstrate the concept, which will
Nice idiom! I just needed to use this in Perl to determine the time between sequential events. Here's what I ended up with.
sub pinch(&@) {
my ( $f, @list ) = @_;
no strict "refs";
use vars qw( $a $b );
my $caller = caller;
local( *{$caller . "::a"} ) = \my $a;
local( *{$caller . "::b"} ) = \my $b;
my @res;
for ( my $i = 0; $i < @list - 1; ++$i ) {
$a = $list[$i];
$b = $list[$i + 1];
push( @res, $f->() );
}
wantarray ? @res : \@res;
}
print join( ",", pinch { $b - $a } qw( 1 2 3 4 5 6 7 ) ), $/;
# ==> 1,1,1,1,1,1
The implementation could probably be prettier if I'd made it dependent on List::Util, but... meh!