fork download
  1. use v6;
  2.  
  3. sub find_first(@list, &matcher --> Int)
  4. {
  5. @fl = @list.grep(&matcher);
  6. if @fl.elems >= 0
  7. {
  8. for @list.kv -> $i, $v
  9. {
  10. return $i if $v eqv @fl[0];
  11. }
  12. }
  13. return Int;
  14. }
  15.  
  16. class Symbol
  17. {
  18. our @!strlist;
  19. has Int $!index
  20.  
  21. submethod BUILD(Str $data)
  22. {
  23. Int $ix = find_first(@!strlist, { $_.k eq $data });
  24. if $ix.defined
  25. {
  26. @!strlist[ix].v++;
  27. $!index = $ix;
  28. }
  29. else
  30. {
  31. $!index = @!strlist.elems;
  32. @!strlist.push($data => 1);
  33. }
  34. }
  35.  
  36. multi method new(Str $data)
  37. {
  38. return self.bless(*, :$data);
  39. }
  40.  
  41. submethod DESTROY()
  42. {
  43. @!strlist[$!index].v--;
  44. if @!strlist[$!index].v == 0
  45. {
  46. @!strlist.splice($!index, 1);
  47. }
  48. }
  49.  
  50. multi method Str() is export
  51. {
  52. @!strlist[$!index].k;
  53. }
  54. }
  55.  
  56. multi sub infix:<cmp>(Symbol $x, Symbol $y) is deep
  57. {
  58. return $x.Str cmp $y.Str;
  59. }
  60.  
  61. multi sub infix:<cmp>(Symbol $x, $y) is deep
  62. {
  63. return $x.Str cmp ~$y;
  64. }
  65.  
  66. multi sub infix:<cmp>($y, Symbol $x) is deep
  67. {
  68. return ~$x. cmp $y.Str;
  69. }
  70.  
  71. my $a = Symbol.new('lol');
  72. my $b = Symbol.new('wut');
  73. "$a {$a.WHERE}".say;
  74. "$b {$b.WHERE}".say;
  75. "----------------".say;
  76. my $x = Symbol.new('lol');
  77. my $y = Symbol.new('wut');
  78. "$x {$x.WHERE}".say;
  79. "$y {$y.WHERE}".say;
Compilation error #stdin compilation error #stdout 0s 0KB
stdin
Standard input is empty
compilation info
===SORRY!===
Unable to parse blockoid, couldn't find final '}' at line 19
stdout
Standard output is empty