1. my role QuantHash does Associative {
  2. method SET-SELF(QuantHash:D: \elems) {
  3. nqp::stmts(
  4. nqp::if(
  5. nqp::elems(elems),
  6. nqp::bindattr(self,::?CLASS,'$!elems',elems)
  7. ),
  8. self
  9. )
  10. }
  11. method Int ( --> Int:D) { self.total.Int }
  12. method Num ( --> Num:D) { self.total.Num }
  13. method Numeric ( --> Numeric:D) { self.total.Numeric }
  14. method Real ( --> Real:D) { self.total.Real }
  15. method Capture() { self.Hash.Capture }
  16. multi method list(QuantHash:U:) { self.Any::list }
  17. multi method list(QuantHash:D:) { self.pairs.cache }
  18. method fmt(QuantHash: Cool $format = "%s\t\%s", $sep = "\n") {
  19. nqp::iseq_i(nqp::sprintfdirectives( nqp::unbox_s($format.Stringy)),1)
  20. ?? self.keys.fmt($format, $sep)
  21. !! self.pairs.fmt($format, $sep)
  22. }
  23. multi method AT-KEY(QuantHash:U \SELF: $key) is raw {
  24. nqp::istype(self, Set) || nqp::istype(self, Bag) || nqp::istype(self, Mix)
  25. ?? die "Cannot auto-vivify an immutable {SELF.^name}"
  26. !! (SELF = self.new).AT-KEY($key)
  27. }
  28. multi method pairs(QuantHash:D:) { Seq.new(self.iterator) }
  29. }