Skip to content

Instantly share code, notes, and snippets.

@ggl
Last active December 28, 2015 18:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ggl/7545861 to your computer and use it in GitHub Desktop.
Save ggl/7545861 to your computer and use it in GitHub Desktop.
Four ways to match two arays
#!/usr/bin/env perl
# four ways to match two arays
use strict;
use warnings;
use match::smart qw(match);
use Test::Deep;
use Benchmark qw(cmpthese);
my @a = qw(a b c d e);
my @b = qw(a b c d e);
my $array_comp = sub {
my ($xr, $yr) = @_;
return unless @$xr == @$yr;
my $i;
for my $e (@$xr) {
return unless $e eq $yr->[$i++];
}
return 1;
};
cmpthese -2, {
smartmatch => sub { @a ~~ @b },
match_smart => sub { match(\@a, \@b) },
eq_deeply => sub { eq_deeply(\@b, \@a) },
array_comp => sub { $array_comp->(\@a, \@b) }
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment