Skip to content

Commit

Permalink
Give QuantHashes a proper .Map method
Browse files Browse the repository at this point in the history
They already had a .Hash method, the .Map method *will* stringify
the objects, though, as Maps cannot contain objects at the moment.
  • Loading branch information
lizmat committed Feb 2, 2021
1 parent 297cba1 commit 110343d
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 0 deletions.
20 changes: 20 additions & 0 deletions src/core.c/Baggy.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -668,6 +668,26 @@ my role Baggy does QuantHash {
multi method Mix(Baggy:D:) { MIXIFY($!elems, Mix) }
multi method MixHash(Baggy:D:) { MIXIFY($!elems, MixHash) }

method Map {
nqp::if(
$!elems && nqp::elems($!elems),
nqp::stmts(
(my \storage := nqp::hash),
(my \iter := nqp::iterator($!elems)),
nqp::while(
iter,
nqp::bindkey(
storage,
nqp::getattr(nqp::iterval(nqp::shift(iter)),Pair,'$!key').Str,
nqp::getattr(nqp::iterval(iter),Pair,'$!value')
)
),
nqp::p6bindattrinvres(nqp::create(Map),Map,'$!storage',storage)
),
nqp::create(Map)
)
}

method RAW-HASH() is raw is implementation-detail { $!elems }
}

Expand Down
1 change: 1 addition & 0 deletions src/core.c/QuantHash.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ my role QuantHash does Associative {

method hash() { ... }
method Hash() { ... }
method Map() { ... }
}

# vim: expandtab shiftwidth=4
20 changes: 20 additions & 0 deletions src/core.c/Setty.pm6
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,26 @@ my role Setty does QuantHash {
multi method hash(Setty:D: --> Hash:D) { self!HASHIFY(Bool) }
multi method Hash(Setty:D: --> Hash:D) { self!HASHIFY(Any) }

method Map {
nqp::if(
$!elems && nqp::elems($!elems),
nqp::stmts(
(my \storage := nqp::hash),
(my \iter := nqp::iterator($!elems)),
nqp::while(
iter,
nqp::bindkey(
storage,
nqp::iterval(nqp::shift(iter)).Str,
True
)
),
nqp::p6bindattrinvres(nqp::create(Map),Map,'$!storage',storage)
),
nqp::create(Map)
)
}

multi method ACCEPTS(Setty:U: \other --> Bool:D) {
other.^does(self)
}
Expand Down

0 comments on commit 110343d

Please sign in to comment.