1. my role Mixy does Baggy {
  2. method SANITY(\elems --> Nil) {
  3. nqp::stmts(
  4. (my $iter := nqp::iterator(elems)),
  5. nqp::while(
  6. $iter,
  7. nqp::if(
  8. nqp::getattr(
  9. nqp::iterval(nqp::shift($iter)),
  10. Pair,
  11. '$!value'
  12. ) == 0,
  13. nqp::deletekey(elems,nqp::iterkey_s($iter))
  14. )
  15. )
  16. )
  17. }
  18. multi method hash(Mixy:D: --> Hash:D) { self.HASHIFY(Any) }
  19. multi method Hash(Mixy:D: --> Hash:D) { self.HASHIFY(Real) }
  20. multi method kxxv(Mixy:D:) {
  21. Failure.new(".kxxv is not supported on a {self.^name}")
  22. }
  23. multi method grab(Mixy:D: $count?) {
  24. Failure.new(".grab is not supported on a {self.^name}")
  25. }
  26. multi method pick(Mixy:D: $count?) {
  27. Failure.new(".pick is not supported on a {self.^name}")
  28. }
  29. multi method roll(Mixy:D:) {
  30. nqp::if(
  31. (my $total :=
  32. Rakudo::QuantHash.MIX-TOTAL-POSITIVE(my $raw := self.raw_hash)),
  33. nqp::getattr(
  34. nqp::iterval(Rakudo::QuantHash.MIX-ROLL($raw,$total)),
  35. Pair,
  36. '$!key'
  37. ),
  38. Nil
  39. )
  40. }
  41. multi method roll(Mixy:D: Whatever) { self.roll(Inf) }
  42. multi method roll(Mixy:D: Callable:D $calculate) {
  43. nqp::if(
  44. (my $total := Rakudo::QuantHash.MIX-TOTAL-POSITIVE(self.raw_hash)),
  45. self.roll($calculate($total)),
  46. Seq.new(Rakudo::Iterator.Empty)
  47. )
  48. }
  49. multi method roll(Mixy:D: $count) {
  50. Seq.new(nqp::if(
  51. (my $total :=
  52. Rakudo::QuantHash.MIX-TOTAL-POSITIVE(my $raw := self.raw_hash)),
  53. nqp::stmts(
  54. (my $done = 0),
  55. Rakudo::Iterator.Callable( {
  56. nqp::if(
  57. $done++ < $count,
  58. nqp::getattr(
  59. nqp::iterval(Rakudo::QuantHash.MIX-ROLL($raw,$total)),
  60. Pair,
  61. '$!key'
  62. ),
  63. IterationEnd
  64. )
  65. })
  66. ),
  67. Rakudo::Iterator.Empty
  68. ))
  69. }
  70. }