I try to selectively delete DownValues of the form;
fun[_Integer]
According to the help, Clear should work with StringPatterns. Can somebody explain why the following does not work:
Clear["fun[" ~~ DigitCharacter .. ~~ "]"]
Unset does also not work. Maybe somebody has an explanation why not even the less specific pattern does not work:
HoldPattern[fun[_]] =.
Thanks for any help, Daniel
Hi Daniel,
Clear[fun]
or
ClearAll[fun]
should do the trick. But then there is no longer any fun …
Regards – Henrik
Hi Henrik,
I do not want to delete the whole “fun”. Only selectively “fun[_Integer]”
regards, Daniel
Ok, I see …
Say we have the following definition:
f[n_Integer] := Fibonacci[n]
f[x_Real] := Sin[x]
f[s_String] := PalindromeQ[s]
then we get e.g.:
Thread[f[{4, 4., "4"}]]
(* Out: {3,-0.7568024953079282`,True} *)
Those definitions are a matter of DownValues:
DownValues[f]
(* Out: {HoldPattern[f[x_Real]]\[RuleDelayed]Sin[x],HoldPattern[f[s_String]]\[RuleDelayed]PalindromeQ[s],HoldPattern[f[n_Integer]]\[RuleDelayed]\Fibonacci[n]} *)
Now if the definition regarding the integer argument should be deleted, one can do this by explicitly setting these DownValues:
DownValues[f] = {HoldPattern[f[x_Real]] :> Sin[x], HoldPattern[f[s_String]] :> PalindromeQ[s]};
or like
DownValues[f] = Most@DownValues[f];
Regards – Henrik
Hi Henrik,
if it only were one item, I would have deleted it by hand. But I have a lot of items to delete and it has to be done automatically.
regards, Daniel
neils
(Neil Singer)
March 3, 2018, 12:29pm
6
Hi Henrik,
if it only were one item, I would have deleted it by hand. But I have a lot of items to delete and it has to be done automatically.
regards, Daniel
neils
(Neil Singer)
March 3, 2018, 12:29pm
7
NOTE: my name was attached to Daniel’s post and mine was deleted:
Daniel,
Use Unset:
f[n_] := f[n] = n
f[1]
f[2]
Creates f[1]=1 and f[2]=2
Use
Unset[f[1]]
Explicitly or Programmatically use
b=1;
Unset[f[Evaluate[b]]]
Regards
I think I found a generic solution:
DownValues[f]= Select[DownValues[f], FreeQ[#, HoldPattern[fun[_Integer]]] &]
Thanks a lot for all answers, Daniel