Message Boards Message Boards

1
|
4232 Views
|
4 Replies
|
8 Total Likes
View groups...
Share
Share this post:

Unexpected error related to Unprotecting StringPartition

Posted 5 years ago

Unprotect@StringPartition

StringPartition[n_Integer, d_Integer] := Function[str, StringPartition[str, n, d]]

results in the following error on my machine:

SetDelayed::write: Tag StringPartition in StringPartition[n_Integer,d_Integer] is Protected.

I have added operator definitions to many other built-in functions using this methodology and StringPartition is the first one that gives me this error.

Please let me know if I'm missing something here or if this error message is a bug?

Thanks, Rian

POSTED BY: Rian Shams
4 Replies
Posted 5 years ago

Hi Shenghui,

Thank you for your response.

I am familiar with Curry, however I would like to avoid defining a new symbol (op in your example) and instead would like to add an operator definition to the existing, built-in symbol StringPartition.

Yes you are right, the unexpected behavior I was referring to in my question is exactly the fact that StringPartition for some reason automatically re-protects itself.

Do you know why this is or how to go about fixing it?

Thanks, Rian

POSTED BY: Rian Shams
Posted 5 years ago

If you look at the definition of StringPartition, you'll see that it is just a stub loading a package, not a real function (in a fresh kernel, of course):

ClearAttributes[StringPartition, {Protected, ReadProtected}]
Definition@StringPartition
StringPartition := System`Dump`AutoLoad[Hold[StringPartition], 
                                 Hold[StringPart, StringPartition], "StringUtilities`"] /; System`Dump`TestLoad

The package redefines StringPartition and protects it again:

StringPartition;
ClearAttributes[StringPartition, {Protected, ReadProtected}]
Definition@StringPartition    
StringPartition[StringUtilitiesDump`args___]:=With[{StringUtilitiesDump`res=StringUtilitiesDump`oStringPartition[StringUtilitiesDump`args]},StringUtilitiesDump`res/;StringUtilitiesDump`res=!=$Failed]

This explains the behavior you observe. The workaround is to preload the package before making any new definitions as I made above. But remember that modifying built-in functions is dangerous and isn't recommened until you know exactly what you do...

POSTED BY: Alexey Popkov

From V11.3, I feel comfortable to deal with this type of operator definition with Curry

In[3]:= op=Curry[StringPartition,{3,1,2}][1][2]
Out[3]= Curry[StringPartition,{3,1,2}][1,2]
In[4]:= op["abc"]
Out[4]= {a,c}
POSTED BY: Shenghui Yang

I have a quite interesting observation of this function as well:

In[1]:= Attributes[StringPartition]
Out[1]= {Protected, ReadProtected}

In[2]:= Unprotect@StringPartition
Out[2]= {StringPartition}

In[3]:= Attributes[StringPartition]
Out[3]= {ReadProtected}

In[4]:= StringPartition[n_Integer,d_Integer]:=Function[str,StringPartition[str,n,d]]
During evaluation of In[4]:= SetDelayed::write: Tag StringPartition in StringPartition[n_Integer,d_Integer] is Protected.
Out[4]= $Failed

In[5]:= Attributes[StringPartition]
Out[5]= {Protected,ReadProtected}

Somehow the function is automatically protected again. It does not happen for Log in this online doc

POSTED BY: Shenghui Yang
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