Last active
January 4, 2016 11:49
-
-
Save MikeInnes/8618047 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SetAttributes[Dictionary, Orderless]; | |
Dictionary[key_ -> val_, ___][key_] := val; | |
Dictionary[___][key_] := key; | |
d_Dictionary.key_ ^:= d[key]; | |
x_ /. Dictionary[rs___] ^:= x /. {rs}; | |
Dictionary[{rs___}] := Dictionary[rs]; | |
Assoc[Dictionary[key_ -> _, rs___], key_ -> val_] := | |
Dictionary[key -> val, rs]; | |
Assoc[Dictionary[rs___], key_ -> val_] := Dictionary[key -> val, rs]; | |
Assoc[map_Dictionary, rs : (_ -> _) ..] := Fold[Assoc, map, {rs}]; | |
map_Dictionary /. ((Dictionary | List)[rs___] | r_) ^:= | |
Assoc[map, r, rs]; | |
Dissoc[Dictionary[key_ -> _, rs___], key_] := Dictionary[rs]; | |
Dissoc[map_Dictionary, keys__] := Fold[Dissoc, map, {keys}]; | |
Dissoc[map_Dictionary, key_] := map; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
m = Dictionary[a->1, b->2] | |
m.a + m.b == m[a] + m[b] == (a + b) /. m == 3 | |
m.c == m[c] == c /. m == c | |
Assoc[m, b -> 4, c -> 5] == m /. {b -> 4, d -> 5} == Dictionary[a -> 1, b -> 4, c -> 5] | |
Dissoc[m, a] == Dictionary[b->2] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
SetAttributes[Dictionary, Orderless]; | |
Dictionary[key_ -> val_, ___][key_] := val; | |
Dictionary[___][key_] := key; | |
Assoc[Dictionary[key_ -> _, rs___], key_ -> val_] := Dictionary[key -> val, rs]; | |
Assoc[Dictionary[rs___], key_ -> val_] := Dictionary[key -> val, rs]; | |
Dissoc[Dictionary[key_ -> _, rs___], key_] := Dictionary[rs]; | |
Dissoc[map_Dictionary, key_] := map; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment