I would like to be able to wtite a function that I can use like this:
my @grouped = split_at {
$a->{time}->strftime("%d-%b-%Y %H") ne
$b->{time}->strftime("%d-%b-%Y %H")
} @files;
where split_at splits an array into an array of arrayrefs based on a function, and looks like this:
sub split_at(&@) {
# split an array into an arrayrefs based on a function
my $cb = shift;
my @input = @_;
my @retval = ( [ ] );
$i = 0;
while ($i <= $#input) {
push @{$retval[$#retval]}, $input[$i];
$i++;
if (($i < $#input) && $cb->($input[$i-1], $input[$i])) { push @retval, [] }
}
pop @retval unless @{$retval[$#retval]};
return @retval;
}
For now I can only call it like this:
my @grouped = split_at {
$_[0]->{time}->strftime("%d-%b-%Y %H") ne
$_[1]->{time}->strftime("%d-%b-%Y %H")
} @files;
where this batches files by the mtime hour using Time::Piece.
I'm trying to find out what's the way to be able to call it as (simplified):
my @foo = split_at { $a <=> $b } @foo;
in a similar way as sort or List::Util::reduce
I have checked the code to reduce in List::Util::PP for reference but I don't understand it enough to port it to my case.