Last active
December 28, 2015 18:49
-
-
Save ggl/7545861 to your computer and use it in GitHub Desktop.
Four ways to match two arays
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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