Skip to main content
added 74 characters in body
Source Link
Edmund
  • 43.2k
  • 3
  • 53
  • 148

It seems that you are complicating things by attempting to over specify the behavior. Unless I have misunderstood your intent I believe the following will do.

ClearAll[patternReplaceRule]
patternReplaceRule[p : Verbatim[Pattern][s_, _]] := p :> <|1 -> s|>

Creating the rules

r1 = patternReplaceRule[x_]
x_ :> Association[1 -> x]
r2 = patternReplaceRule[y_]
y_ :> Association[1 -> y]

Applying the rules produce the expected result.

x /. # & /@ {r1, r2}
{<|1 -> x|>, <|1 -> x|>}
y /. # & /@ {r1, r2}
{<|1 -> y|>, <|1 -> y|>}

Also

r3 = patternReplaceRule[x_];
r1 == r3
True

Hope this helps.

It seems that you are complicating things by attempting to over specify the behavior. Unless I have misunderstood your intent I believe the following will do.

ClearAll[patternReplaceRule]
patternReplaceRule[p : Verbatim[Pattern][s_, _]] := p :> <|1 -> s|>

Creating the rules

r1 = patternReplaceRule[x_]
x_ :> Association[1 -> x]
r2 = patternReplaceRule[y_]
y_ :> Association[1 -> y]

Applying the rules produce the expected result.

x /. # & /@ {r1, r2}
{<|1 -> x|>, <|1 -> x|>}
y /. # & /@ {r1, r2}
{<|1 -> y|>, <|1 -> y|>}

Hope this helps.

It seems that you are complicating things by attempting to over specify the behavior. Unless I have misunderstood your intent I believe the following will do.

ClearAll[patternReplaceRule]
patternReplaceRule[p : Verbatim[Pattern][s_, _]] := p :> <|1 -> s|>

Creating the rules

r1 = patternReplaceRule[x_]
x_ :> Association[1 -> x]
r2 = patternReplaceRule[y_]
y_ :> Association[1 -> y]

Applying the rules produce the expected result.

x /. # & /@ {r1, r2}
{<|1 -> x|>, <|1 -> x|>}
y /. # & /@ {r1, r2}
{<|1 -> y|>, <|1 -> y|>}

Also

r3 = patternReplaceRule[x_];
r1 == r3
True

Hope this helps.

Source Link
Edmund
  • 43.2k
  • 3
  • 53
  • 148

It seems that you are complicating things by attempting to over specify the behavior. Unless I have misunderstood your intent I believe the following will do.

ClearAll[patternReplaceRule]
patternReplaceRule[p : Verbatim[Pattern][s_, _]] := p :> <|1 -> s|>

Creating the rules

r1 = patternReplaceRule[x_]
x_ :> Association[1 -> x]
r2 = patternReplaceRule[y_]
y_ :> Association[1 -> y]

Applying the rules produce the expected result.

x /. # & /@ {r1, r2}
{<|1 -> x|>, <|1 -> x|>}
y /. # & /@ {r1, r2}
{<|1 -> y|>, <|1 -> y|>}

Hope this helps.