Template with optional argument in replacement rule

I am trying to define a replacement rule with an optional argument color_RGBColor, which should be replaced with Sequence[]when it is not in the original expression:

style[line_Line, ___, 
  color_RGBColor: Unevaluated@Sequence[], ___] :> {color, line}

When RGBColorpresent in the original expression, the rule works:

style[Line[], RGBColor[{}]] /. 
 style[line_Line, ___, 
   color_RGBColor: Unevaluated@Sequence[], ___] :> {color, line}

=> {RGBColor[{}], Line[]}

But when he is absent, he does not:

style[Line[], Thickness[0.01]] /. 
 Style[line_Line, ___, 
   color_RGBColor: Unevaluated@Sequence[], ___] :> {color, line}

=> style[Line[], Thickness[0.01]]

My questions:

1) Why does this not work?

2) Is it possible to build one template that will work as desired?

+5
source share
2 answers

- , (), , RGBColor. , , Unevaluated[Sequence[]], , _RGBColor.

. - :

In[10]:= style[Line[],Thickness[0.01]]/.
style[line_Line,___,color_: Unevaluated@Sequence[],___]:>{color,line}

Out[10]= {Thickness[0.01],Line[]}

, - . :

In[14]:= style[Line[], RGBColor[{}]] /. 
 style[line_Line, ___, color : (_RGBColor | _Unevaluated) : 
    Unevaluated@Sequence[], ___] :> {Evaluate@color, line}


Out[14]= {RGBColor[{}], Line[]}

In[15]:= style[Line[], Thickness[0.01]] /. 
  style[line_Line, ___, color : (_RGBColor | _Unevaluated) : 
     Unevaluated@Sequence[], ___] :> {Evaluate@color, line}

Out[15]= {Line[]}

:

In[18]:= style[Line[], Thickness[0.01]] /. 
style[line_Line, ___, color : (_RGBColor | Automatic) : Automatic, ___] :> 
  If[color === Automatic, {line}, {color, line}]


Out[18]= {Line[]}

In[17]:= style[Line[], RGBColor[{}]] /. 
 style[line_Line, ___, color : (_RGBColor | Automatic) : Automatic, ___] :> 
   If[color === Automatic, {line}, {color, line}]


Out[17]= {RGBColor[{}], Line[]}

- , : () x:ptrn:default ptrn. . this > Mathgroup.

+7

, :

style[Line[a], RGBColor[{}]] /. 
 style[line_Line, ___, Longest[color___RGBColor], ___] :> {color,line}
(*
{RGBColor[{}], Line[a]}
*)

style[Line[]] /. 
 style[line_Line, ___, Longest[color___RGBColor], ___] :> {color, line}
(*
{Line[]}
*)

, , Head RGBColor, .

+2

All Articles