package List::Merger; @ISA = qw(Exporter); @EXPORT = qw(); @EXPORT_OK = qw(gen_merger); use strict; use warnings; use List::Util 'first'; our $VERSION = '0.01'; sub gen_merger { my ($list, $fetch, $compare, $finish) = @_; my @item = map $fetch->($_), @$list; my $done; return sub { return $finish if $done; my $idx = first {$item[$_] ne $finish} 0 .. $#item; my $next = $item[$idx]; for ($idx + 1 .. $#item) { next if $item[$_] eq $finish; my $result = $compare->($next, $item[$_]); $next = $item[$_] if $result == 1; } $item[$idx] = $fetch->($list->[$idx]); $done = 1 if ! first {$item[$_] ne $finish} $idx .. $#item; return $next; }; }