Skip to content

Instantly share code, notes, and snippets.

Created July 10, 2015 11:56
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 anonymous/571b2a3c97a6176a6ecf to your computer and use it in GitHub Desktop.
Save anonymous/571b2a3c97a6176a6ecf to your computer and use it in GitHub Desktop.
weird behavior with eqv
use v6;
class Node does Positional
{
has ( $.Str, @.list );
submethod BUILD( :$node ) { @!list = ( $!Str = $node ).comb: / \D+ | \d+ / }
method AT-POS( *@i ) { @!list[@i] }
method cmp( Node $n )
{
my $i = self.ind( $n );
$i.defined ?? @!list[$i] cmp $n[$i] !! $!Str leg $n;
}
method ind( Node $n )
{
my @i = $n.keys.grep: { @!list[$_] ne $n[$_] } if @!list == +$n;
## why does the line below not work with !eqv ?
## my @i = $n.keys.grep: { @!list[$_] !eqv $n[$_] } if @!list == +$n;
## say @i; ## with input below this should be 1, not 0, 1
return @i == 1 ?? @i[0] !! Nil;
}
}
multi prefix:<+>( Node $n ) { $n.elems }
multi infix:<~>( Node $l, Node $r ) { Node.new: :node{ $l ~ $r } }
multi infix:<*>( Node $l, Node $r ) { $l.ind: $r }
multi infix:<cmp>( Node $l, Node $r ) { $l.cmp: $r }
my $a = Node.new( :node<abc000> );
my $b = Node.new( :node<abc001> );
say $a * $b; ## expecting 1, which is the index $a and $b have in common
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment