Guest User

Raku custom hash type demo

a guest
Jan 5th, 2024
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 6 0.70 KB | None | 0 0
  1. #! /usr/bin/env raku
  2.  
  3. use v6.d;
  4.  
  5. subset CustomKey of List where .elems == 2 && .all ~~ UInt:D;
  6.  
  7. subset CustomValue of List where * ~~ (
  8.     :foo(UInt:D)
  9.     :bar(Str:D)
  10. ).sort;
  11.  
  12. class CustomHash does Associative[CustomValue:D, CustomKey:D] {
  13.     has CustomValue:D %!data{Str:D};
  14.     method AT-KEY(CustomKey:D \k --> CustomValue) { %!data{k.Str} }
  15.     method EXISTS-KEY(CustomKey:D \k --> Bool:D) { %!data{k.Str}:exists }
  16.     method ASSIGN-KEY(CustomKey:D \k, CustomValue:D \v) { %!data{k.Str} = v }
  17. }
  18.  
  19. my CustomHash:D \a = CustomHash.new;
  20.  
  21. a{$(12,34)} = (foo => 123, bar => 'hi').sort.List;
  22. a{$(12,34)}.raku.say; # $(:bar("hi"), :foo(123))
  23.  
  24. a{$(12,35)}.defined.say; # False
  25. a{$(13,34)}.defined.say; # False
Advertisement
Add Comment
Please, Sign In to add comment