Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Some hyper metaop and hash updates.
  • Loading branch information
pmichaud committed Aug 8, 2011
1 parent 99f5ce9 commit 429a183
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 23 deletions.
6 changes: 3 additions & 3 deletions src/core/Any.pm
Expand Up @@ -139,10 +139,10 @@ my class Any {
multi method postcircumfix:<{ }>($key) is rw {
self.at_key($key)
}
multi method postcircumfix:<{ }>(Positional $key) {
$key.map({ self{$_ } }).eager.Parcel
multi method postcircumfix:<{ }>(Positional $key) is rw {
$key.map({ self{$_} }).eager.Parcel
}
multi method postcircumfix:<{ }>(Whatever) {
multi method postcircumfix:<{ }>(Whatever) is rw {
self{self.keys}
}

Expand Down
2 changes: 2 additions & 0 deletions src/core/Hash.pm
@@ -1,5 +1,7 @@
my class Hash {
# Has attributes and parent EnumMap declared in BOOTSTRAP

method new(*@args) { @args.hash }

method at_key($key is copy) is rw {
$key = $key.Str;
Expand Down
47 changes: 27 additions & 20 deletions src/core/metaops.pm
Expand Up @@ -116,18 +116,22 @@ sub METAOP_HYPER_PREFIX(\$op, \$obj) { hyper($op, $obj) }

proto sub hyper(|$) { * }
multi sub hyper(\$op, \$a, \$b, :$dwim-left, :$dwim-right) {
my @alist := $a.elems < $b.elems && $dwim-left
?? ($a xx *).munch($b.elems)
!! $a.flat;
my @blist := $b.elems < $a.elems && $dwim-right
?? ($b xx *).munch($a.elems)
!! $b.flat;
die "Sorry, lists on both sides of non-dwimmy hyperop are not of same length:\n"
~ " left: @alist.elems() elements, right: @blist.elems() elements\n"
if @alist != @blist;
my @alist := $a.flat;
my @blist := $b.flat;
my $elems;
if $dwim-left && $dwim-right { $elems = max(@alist.elems, @blist.elems) }
elsif $dwim-left { $elems = @blist.elems }
elsif $dwim-right { $elems = @alist.elems }
else {
die "Sorry, lists on both sides of non-dwimmy hyperop are not of same length:\n"
~ " left: @alist.elems() elements, right: @blist.elems() elements\n"
if @alist.elems != @blist.elems
}
@alist := (@alist xx *).munch($elems) if @alist.elems < $elems;
@blist := (@blist xx *).munch($elems) if @blist.elems < $elems;

(@alist Z @blist).map(
-> $x, $y {
-> \$x, \$y {
Iterable.ACCEPTS($x)
?? $x.new(hyper($op, $x, $y, :$dwim-left, :$dwim-right)).item
!! (Iterable.ACCEPTS($y)
Expand All @@ -143,23 +147,26 @@ multi sub hyper(\$op, \$a) {
!! $op($_) } ).eager
}

multi sub hyper(\$op, %h) {
hash %h.keys Z hyper($op, %h.values)
multi sub hyper(\$op, Associative \$h) {
my @keys = $h.keys;
hash @keys Z hyper($op, $h{@keys})
}

multi sub hyper(\$op, %a, %b, :$dwim-left, :$dwim-right) {
multi sub hyper(\$op, Associative \$a, Associative \$b, :$dwim-left, :$dwim-right) {
my %k;
for %a.keys { %k{$_} = 1 if !$dwim-left || %b.exists($_) }
for %b.keys { %k{$_} = 1 if !$dwim-right }
for $a.keys { %k{$_} = 1 if !$dwim-left || $b.exists($_) }
for $b.keys { %k{$_} = 1 if !$dwim-right }
my @keys = %k.keys;
hash @keys Z hyper($op, %a{@keys}, %b{@keys}, :$dwim-left, :$dwim-right)
hash @keys Z hyper($op, $a{@keys}, $b{@keys}, :$dwim-left, :$dwim-right)
}

multi sub hyper(\$op, %a, \$b, :$dwim-left, :$dwim-right) {
hash %a.keys Z hyper($op, %a.values, $b, :$dwim-left, :$dwim-right);
multi sub hyper(\$op, Associative \$a, \$b, :$dwim-left, :$dwim-right) {
my @keys = $a.keys;
hash @keys Z hyper($op, $a{@keys}, $b, :$dwim-left, :$dwim-right);
}

multi sub hyper(\$op, \$a, %b, :$dwim-left, :$dwim-right) {
hash %b.keys Z hyper($op, $a, %b.values, :$dwim-left, :$dwim-right);
multi sub hyper(\$op, \$a, Associative \$b, :$dwim-left, :$dwim-right) {
my @keys = $b.keys;
hash @keys Z hyper($op, $a, $b{@keys}, :$dwim-left, :$dwim-right);
}

0 comments on commit 429a183

Please sign in to comment.