Skip to content

Instantly share code, notes, and snippets.

@tene
Created September 17, 2010 06:55
Show Gist options
  • Select an option

  • Save tene/583838 to your computer and use it in GitHub Desktop.

Select an option

Save tene/583838 to your computer and use it in GitHub Desktop.
use 5.010;
use strict;
use warnings;
use List::MoreUtils qw/natatime zip/;
my $rlp = [ 1, 2, [3, 4, [5, 6]]];
my $rlp1 = [ 1, 2, [3, 4, [5, 6]]];
my $rlp2 = [ 1, 2, [3, 4, [5, 9]]];
sub rcmp {
my ($l, $r, $test) = @_;
if (!(ref $l || ref $r)) {
return $test->($l, $r);
}
elsif ((ref $l && ref $r) && (ref $l eq ref $r)) {
return 0 if @$l != @$r;
my $it = natatime 2, zip(@$l, @$r);
while ( my($x, $y) = $it->() ) {
my $rv = rcmp($x, $y, $test);
return 0 unless $rv;
}
return 1;
}
else {
return 0;
}
}
say rcmp($rlp, $rlp1, sub { $_[0] == $_[1] });
say rcmp($rlp, $rlp2, sub { $_[0] == $_[1] });
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment