Skip to content

Instantly share code, notes, and snippets.

@Xliff
Last active March 31, 2016 21:03
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 Xliff/2907106c84e9eb14d4c0 to your computer and use it in GitHub Desktop.
Save Xliff/2907106c84e9eb14d4c0 to your computer and use it in GitHub Desktop.
Match infix:<eqv> -- Draft!
multi sub infix:<eqv>(Match:D $a, Match:D $b) {
   $a.to   eqv $b.to   &&
   $a.from eqv $b.from &&
   $a.orig eqv $b.orig &&
   $a.made eqv $b.made &&
   $a.list eqv $b.list &&
   $a.hash eqv $b.hash;
}

A better implementation has been suggested:

multi sub infix:<eqv>(Match:D $a, Match:D $b) {
    [&&] (
        $a.to   eqv $b.to,
        $a.from eqv $b.from,
        $a.orig eqv $b.orig,
        $a.made eqv $b.made,
        $a.list eqv $b.list,
        $a.hash eqv $b.hash
    );
}

And that's where things currently stand.

@Xliff
Copy link
Author

Xliff commented Mar 24, 2016

Test cases:

my $string = 'aaaaaaaa' ;
my regex xxx  { $<t1> = aa  $<t2> = a  a } ;
my regex yyy { ($<t1> = [aa] ) ($<t2> = a) a } ;
my regex zzz { ($<t1> = [aa] ) ($<t2> = a) a } ;

my $m1 = $string ~~ m:g/<xxx>/;
my $m2 = $string ~~ m:g/<yyy>/;
my $m3 = $string ~~ m:g/<yyy>/;
my $m4 = $string ~~ m:g/<zzz>/;

my $ml1 = $string ~~ my regex y1 { ($<t1> = [aa] ) ($<t2> = a) a };
my $ml2 = $string ~~ my regex y2 { ($<t1> = [aa] ) ($<t2> = a) a };
my $ml3 = $string ~~ &yyy;
my $ml4 = $string ~~ &zzz;

my $t1 = $m1 eqv $m1;
my $t2 = $m1 eqv $m2;
my $t3 = $m2 eqv $m1;
my $t4 = $m2 eqv $m2;
my $t5 = $m3 eqv $m2;
my $t6 = $m4 eqv $m2;

my $t_lex_1 = $ml1 eqv $ml2;
my $t_lex_2 = $ml3 eqv $ml4;

my $t_im1 = ('aaa' ~~ /a(..)/) eqv ('aaa' ~~ /a../);
my $t_im2 = ('a' ~~ /(.)/) eqv ('ab' ~~ /(.)/);

say "$t1 / $t2 / $t3 / $t4 / $t5 / $t6";     # True / False / False / True / True / False
say "$t_lex_1 / $t_lex_2";                   # True / True
say "$t_im1 / $t_im2";                       # False / False

@Xliff
Copy link
Author

Xliff commented Mar 30, 2016

Someone has suggested:

?all(
    $a.to   eqv $b.to,
    $a.from eqv $b.from,
    $a.orig eqv $b.orig,
    $a.made eqv $b.made,
    $a.list eqv $b.list,
    $a.hash eqv $b.hash
)

...for readability.

I'd change it in a heartbeat if there was a performance improvement, but I figure just about anyone can read the logic as it currently stands. The ?all() syntax might still be confusing for new programmers in the short-term.

@Xliff
Copy link
Author

Xliff commented Mar 30, 2016

multi sub infix:<eqv>(Match:D $a, Match:D $b) {
    [&&] (
        $a.to   eqv $b.to,
        $a.from eqv $b.from,
        $a.orig eqv $b.orig,
        $a.made eqv $b.made,
        $a.list eqv $b.list,
        $a.hash eqv $b.hash
    );
}

... now this I like!

@Xliff
Copy link
Author

Xliff commented Mar 31, 2016

Since most objects implement some operators as methods, how about this one for the Match class:

method eqv(Match:D: $b) {
        self === $b || [&&] (
                $!to      eqv $b.to,
                $!from    eqv $b.from,
                $!orig    eqv $b.orig,
                $!made    eqv $b.made,
                self.list eqv $b.list,
                self.hash eqv $b.hash
        );
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment