Skip to content

Commit

Permalink
Streamline infix:<eqv>
Browse files Browse the repository at this point in the history
  • Loading branch information
lizmat committed Apr 21, 2016
1 parent fe2be65 commit 463e758
Show file tree
Hide file tree
Showing 13 changed files with 70 additions and 46 deletions.
6 changes: 1 addition & 5 deletions src/core/Buf.pm
Expand Up @@ -760,11 +760,7 @@ multi sub infix:<~^>(Blob:D \a, Blob:D \b) {
}

multi sub infix:<eqv>(Blob:D \a, Blob:D \b) {
a =:= b
?? True
!! a.WHAT === b.WHAT
?? a.SAME(b)
!! False
nqp::p6bool(nqp::eqaddr(a,b) || (nqp::eqaddr(a.WHAT,b.WHAT) && a.SAME(b)))
}

multi sub infix:<cmp>(Blob:D \a, Blob:D \b) { ORDER(a.COMPARE(b)) }
Expand Down
8 changes: 6 additions & 2 deletions src/core/Capture.pm
Expand Up @@ -146,8 +146,12 @@ my class Capture { # declared in BOOTSTRAP
}
}

multi sub infix:<eqv>(Capture $a, Capture $b) {
$a.WHAT === $b.WHAT && $a.list eqv $b.list && $a.hash eqv $b.hash
multi sub infix:<eqv>(Capture \a, Capture \b) {
nqp::p6bool(
nqp::eqaddr(a,b)
|| (nqp::eqaddr(a.WHAT,b.WHAT)
&& a.list eqv b.list && a.hash eqv b.hash)
)
}

# vim: ft=perl6 expandtab sw=4
19 changes: 12 additions & 7 deletions src/core/Map.pm
Expand Up @@ -263,14 +263,19 @@ my class Map does Iterable does Associative { # declared in BOOTSTRAP
}
}

multi sub infix:<eqv>(Map:D $a, Map:D $b) {
if +$a != +$b { return Bool::False }
for $a.kv -> $k, $v {
unless $b.EXISTS-KEY($k) && $b{$k} eqv $v {
return Bool::False;
}
multi sub infix:<eqv>(Map:D \a, Map:D \b) {
if a =:= b {
True
}
elsif a.WHAT =:= b.WHAT && a.elems == b.elems {
return False
unless b.EXISTS-KEY($_) && a.AT-KEY($_) eqv b.AT-KEY($_)
for a.keys;
True
}
else {
False
}
Bool::True;
}

# vim: ft=perl6 expandtab sw=4
17 changes: 9 additions & 8 deletions src/core/Mu.pm
Expand Up @@ -674,16 +674,17 @@ multi sub infix:<eqv>(Any $a, Any $b) {
}

multi sub infix:<eqv>(@a, @b) {
if @a.WHAT === @b.WHAT && (my int $n = @a.elems) == @b.elems {
my int $i;
while $i < $n {
return Bool::False unless @a.AT-POS($i) eqv @b.AT-POS($i);
$i = $i + 1;
}
Bool::True
if @a =:= @b {
True
}
elsif @a.WHAT =:= @b.WHAT && (my int $n = @a.elems) == @b.elems {
my int $i = -1;
return False unless @a.AT-POS($i) eqv @b.AT-POS($i)
while nqp::islt_i($i = nqp::add_i($i,1),$n);
True
}
else {
Bool::False;
False
}
}

Expand Down
4 changes: 2 additions & 2 deletions src/core/Numeric.pm
Expand Up @@ -29,8 +29,8 @@ my role Numeric {
method pred() { self - 1 }
}

multi sub infix:<eqv>(Numeric:D $a, Numeric:D $b) {
$a.WHAT === $b.WHAT && $a == $b
multi sub infix:<eqv>(Numeric:D \a, Numeric:D \b) {
nqp::p6bool(a =:= b || (a.WHAT =:= b.WHAT && a == b)) # RT #127951
}

## arithmetic operators
Expand Down
4 changes: 2 additions & 2 deletions src/core/Pair.pm
Expand Up @@ -77,8 +77,8 @@ my class Pair does Associative {
method FLATTENABLE_HASH() { nqp::hash($!key.Str, $!value) }
}

multi sub infix:<eqv>(Pair:D $a, Pair:D $b) {
$a.WHAT === $b.WHAT && $a.key eqv $b.key && $a.value eqv $b.value
multi sub infix:<eqv>(Pair:D \a, Pair:D \b) {
a =:= b || (a.WHAT =:= b.WHAT && a.key eqv b.key && a.value eqv b.value)
}

multi sub infix:<cmp>(Pair:D \a, Pair:D \b) {
Expand Down
3 changes: 3 additions & 0 deletions src/core/Parameter.pm
Expand Up @@ -388,6 +388,9 @@ multi sub infix:<eqv>(Parameter \a, Parameter \b) {
# we're us
return True if a =:= b;

# different container type
return False unless a.WHAT =:= b.WHAT;

# different nominal or coerce type
return False
unless nqp::iseq_s(
Expand Down
4 changes: 2 additions & 2 deletions src/core/Promise.pm
Expand Up @@ -223,8 +223,8 @@ my class Promise {
method Numeric(Promise:D:) { self.result.Numeric }
}

multi sub infix:<eqv>(Promise:D $a, Promise:D $b) {
infix:<eqv>($a.result, $b.result);
multi sub infix:<eqv>(Promise:D \a, Promise:D \b) {
a =:= b || a.result eqv b.result
}

# vim: ft=perl6 expandtab sw=4
10 changes: 6 additions & 4 deletions src/core/Range.pm
Expand Up @@ -721,10 +721,12 @@ sub prefix:<^>($max) is pure {
}

multi sub infix:<eqv>(Range:D \a, Range:D \b) {
a.min eqv b.min
&& a.max eqv b.max
&& a.excludes-min eqv b.excludes-min
&& a.excludes-max eqv b.excludes-max
a =:= b
|| (a.WHAT =:= b.WHAT
&& a.min eqv b.min
&& a.max eqv b.max
&& a.excludes-min eqv b.excludes-min
&& a.excludes-max eqv b.excludes-max)
}

multi sub infix:<+>(Range:D \a, Real:D \b) { a.clone-with-op(&[+], b) }
Expand Down
30 changes: 20 additions & 10 deletions src/core/Seq.pm
Expand Up @@ -424,16 +424,26 @@ sub GATHER(&block) {
}.new(&block))
}

multi sub infix:<eqv>(Seq:D $a, Seq:D $b) {
return False unless $a.WHAT === $b.WHAT;
my \ia := $a.iterator;
my \ib := $b.iterator;
loop {
my \va := ia.pull-one;
my \vb := ib.pull-one;
return Bool::True if va =:= IterationEnd && vb =:= IterationEnd;
return Bool::False if va =:= IterationEnd or vb =:= IterationEnd or not va eqv vb;
}
multi sub infix:<eqv>(Seq:D \a, Seq:D \b) {

# we're us
return True if a =:= b;

# not same container type
return False unless a.WHAT =:= b.WHAT;

my \ia := a.iterator;
my \ib := b.iterator;
my $va;
my $vb;

# same until a-list exhausted
return False
if ($vb := ib.pull-one) =:= IterationEnd || !($va eqv $vb)
until ($va := ia.pull-one) =:= IterationEnd;

# b-list also exhausted?
ib.pull-one =:= IterationEnd
}

# vim: ft=perl6 expandtab sw=4
3 changes: 3 additions & 0 deletions src/core/Signature.pm
Expand Up @@ -141,6 +141,9 @@ multi sub infix:<eqv>(Signature \a, Signature \b) {
# we're us
return True if a =:= b;

# different container type
return False unless a.WHAT =:= b.WHAT;

# arity or count mismatch
return False if a.arity != b.arity || a.count != b.count;

Expand Down
4 changes: 2 additions & 2 deletions src/core/Stringy.pm
Expand Up @@ -2,8 +2,8 @@ my class X::NYI { ... }

my role Stringy { }

multi sub infix:<eqv>(Stringy:D $a, Stringy:D $b) {
$a.WHAT === $b.WHAT && ($a cmp $b) == 0
multi sub infix:<eqv>(Stringy:D \a, Stringy:D \b) {
a =:= b || (a.WHAT =:= b.WHAT && (a cmp b) == 0)
}

proto sub prefix:<~>($) is pure { * }
Expand Down
4 changes: 2 additions & 2 deletions src/core/Version.pm
Expand Up @@ -106,8 +106,8 @@ class Version {
}


multi sub infix:<eqv>(Version:D $a, Version:D $b) {
$a.WHAT === $b.WHAT && $a.Str eq $b.Str
multi sub infix:<eqv>(Version:D \a, Version:D \b) {
a =:= b || (a.WHAT =:= b.WHAT && a.Str eq b.Str)
}


Expand Down

0 comments on commit 463e758

Please sign in to comment.