Skip to content

Instantly share code, notes, and snippets.

@ab5tract
Last active August 29, 2015 14:08
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 ab5tract/7b8d2a82a2674f4d41be to your computer and use it in GitHub Desktop.
Save ab5tract/7b8d2a82a2674f4d41be to your computer and use it in GitHub Desktop.
Two possible approaches to coercion in set comparisons
multi sub infix:<<(<)>>(Bag $a, Bag $b --> Bool) {
[&&] $a.keys.map({ $a{$_} < $b{$_} });
}
multi sub infix:<<(<)>>(Bag $a, Any $b --> Bool) {
$b = $b.bag(:view);
[&&] $a.keys.map({ $a{$_} < $b{$_} });
}
multi sub infix:<<(<)>>(Any $a, Bag $b --> Bool) {
$a = $a.bag(:view);
[&&] $a.keys.map({ $a{$_} < $b{$_} });
}
only sub infix:<<(<=)>>(Any $a, Any $b --> Bool) {
my ($c,$d);
given $a|$b {
when Mix { $c = $a.Mix(:view); $d = $b.Mix(:view); }
when Bag { $c = $a.Bag(:view); $d = $b.Bag(:view); }
default { $c = $a.Set(:view); $d = $b.Set(:view); }
}
[&&] $c.keys.map({ $c{$_} <= $d{$_} });
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment