Guest User

Raku custom hash-like associative type implementation using complex type for the keys

a guest
Jan 5th, 2024
325
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Perl 6 0.64 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.     subset K where CustomHash.keyof;
  15.     subset V where CustomHash.of;
  16.     method AT-KEY(K \k --> V) { %!data{k.Str} }
  17.     method EXISTS-KEY(K \k --> Bool:D) { %!data{k.Str}:exists }
  18.     method ASSIGN-KEY(K \k, V \v) { %!data{k.Str} = v }
  19. }
  20.  
  21. my CustomHash:D \a = CustomHash.new;
  22.  
  23. a{$(12,34)} = (foo => 123, bar => 'hi').sort.List;
  24. a{$(12,34)}.raku.say; # $(:bar("hi"), :foo(123))
Advertisement
Add Comment
Please, Sign In to add comment