class CaseInsensitiveHash does Associative {
has %!values handles <gist Str raku perl pairs list keys values>;
has %!keys;
method STORE(Hash() $values, |) {
%!values = $values;
for %!values.keys {
%!keys{.lc} = .Str
}
}
method AT-KEY($key) is rw {
Proxy.new:
FETCH => {
%!values{%!keys{$key.lc}}
},
STORE => -> $, \val {
%!values{%!keys{$key.lc}}:delete
if %!keys{$key.lc}:exists;
%!keys{$key.lc} = $key.Str;
%!values{%!keys{$key.lc}} = val
}
;
}
method EXISTS-KEY($key) {
%!keys{$key.lc}:exists
}
}
my %received = bLa => 1, BLE => 2, bli => 3;
my %ci-hash is CaseInsensitiveHash = %received;
say %ci-hash<bla>;
say %ci-hash<ble>;
say %ci-hash<bla ble bli>;
say %ci-hash<BlA bLe BLI>;
dd %ci-hash;
%ci-hash<AAA> = 42;
%ci-hash<bBb> = 13;
%ci-hash<BBB> = 3.14; # overrides bBb
dd %ci-hash;
say %ci-hash.keys;
say %ci-hash.values;
say %ci-hash.pairs;