Skip to content

Instantly share code, notes, and snippets.

@MikeInnes
Last active January 4, 2016 11:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save MikeInnes/8618047 to your computer and use it in GitHub Desktop.
Save MikeInnes/8618047 to your computer and use it in GitHub Desktop.
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;
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]
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