Message Boards Message Boards

0
|
4092 Views
|
7 Replies
|
3 Total Likes
View groups...
Share
Share this post:

Delete DownValues of the form fun[_Integer] with Clear, Unset StringPattern

Posted 6 years ago

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

POSTED BY: Daniel Huber
7 Replies

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

POSTED BY: Neil Singer
Posted 6 years ago

I think I found a generic solution:

DownValues[f]= Select[DownValues[f], FreeQ[#, HoldPattern[fun[_Integer]]] &]

Thanks a lot for all answers, Daniel

POSTED BY: Daniel Huber

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

POSTED BY: Neil Singer

Hi Daniel,

Clear[fun]

or

ClearAll[fun]

should do the trick. But then there is no longer any fun ...

Regards -- Henrik

POSTED BY: Henrik Schachner
Posted 6 years ago

Hi Henrik, I do not want to delete the whole "fun". Only selectively "fun[_Integer]" regards, Daniel

POSTED BY: Daniel Huber

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

POSTED BY: Henrik Schachner
Posted 6 years ago

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

POSTED BY: Daniel Huber
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract