How can I clear a subset of a symbol's DownValues
?
For example, suppose I have created some DownValues for $f$ like this:
(f[Sequence @@ #] = Plus @@ #) & /@ Subsets[{1, 2, 3}];
f[x_, y_] := x y
DownValues[f]
(* {HoldPattern[f[]] :> 0, HoldPattern[f[1]] :> 1,
HoldPattern[f[2]] :> 2, HoldPattern[f[3]] :> 3,
HoldPattern[f[1, 2]] :> 3, HoldPattern[f[1, 3]] :> 4,
HoldPattern[f[2, 3]] :> 5, HoldPattern[f[1, 2, 3]] :> 6,
HoldPattern[f[x_, y_]] :> x y} *)
I now wish to clear all the downvalues for which $f$ has exactly $n$ numerical arguments. In other words I would like to have a function selectiveClear[f,n]
such that this would happen:
selectiveClear[f,2]
DownValues[f]
(* {HoldPattern[f[]] :> 0, HoldPattern[f[1]] :> 1,
HoldPattern[f[2]] :> 2, HoldPattern[f[3]] :> 3,
HoldPattern[f[1, 2, 3]] :> 6, HoldPattern[f[x_, y_]] :> x y} *)
I have tried using Cases
to pick a subset of the DownValues
, but I can't seem to get the pattern correct.