1. my class Mix does Mixy {
  2. has $!WHICH;
  3. has Real $!total;
  4. #--- interface methods
  5. multi method DELETE-KEY(Mix:D: \k) {
  6. X::Immutable.new(method => 'DELETE-KEY', typename => self.^name).throw;
  7. }
  8. #--- introspection methods
  9. multi method WHICH(Mix:D:) {
  10. nqp::if(
  11. nqp::attrinited(self,Mix,'$!WHICH'),
  12. $!WHICH,
  13. $!WHICH := self!WHICH
  14. )
  15. }
  16. method total(Mix:D: --> Real:D) {
  17. nqp::if(
  18. nqp::attrinited(self,Mix,'$!total'),
  19. $!total,
  20. $!total := self!TOTAL
  21. )
  22. }
  23. #--- selection methods
  24. multi method grab($count? --> Real:D) {
  25. X::Immutable.new( method => 'grab', typename => self.^name ).throw;
  26. }
  27. multi method grabpairs($count? --> Real:D) {
  28. X::Immutable.new( method => 'grabpairs', typename => self.^name ).throw;
  29. }
  30. #--- coercion methods
  31. method Mix { self }
  32. method MixHash { MixHash.new-from-pairs(%!elems.values) }
  33. method Bag { Bag.new-from-pairs(%!elems.values.grep(*.value > 0).map({.key => .value.Int})) }
  34. method BagHash { BagHash.new-from-pairs(%!elems.values.grep(*.value > 0).map({.key => .value.Int})) }
  35. proto method classify-list(|) {
  36. X::Immutable.new(:method<classify-list>, :typename(self.^name)).throw;
  37. }
  38. proto method categorize-list(|) {
  39. X::Immutable.new(:method<categorize-list>, :typename(self.^name)).throw;
  40. }
  41. }