Skip to main content
added 179 characters in body
Source Link
Mr.Wizard
  • 275.2k
  • 34
  • 606
  • 1.5k

Another thought-provoking question Jacob, and sadly one I missed until today. After only brief consideration I think perhaps what you want can be done more simply but I look forward to your critique of my proposal.

Assumptions:

  • Exact preservation of the structure of option lists is not required
  • The specific order of options is not significant
  • Option rules may be safely evaluated

Basic Proposal

SetAttributes[setOpts, HoldFirst] setOpts[head_[args___, opts : OptionsPattern[]], new__] := First /@ GatherBy[Flatten@{new, opts}, First] /. {op___} :> head[args, op] 

Test:

setOpts[f[1], q -> r] 
f[1, q -> r] 
setOpts[ f[2, q -> b, z -> x, {aa -> bb -> cc, foo :> bar}], z -> dog, a :> cat ] 
f[2, z -> dog, a :> cat, q -> b, aa -> bb -> cc, foo :> bar] 
setOpts[Plot[Sinc@x, {x, 0, 10}], PlotStyle -> Red] 

enter image description here

Extended definition

To make the following case work an additional definition is required:

delay := Plot[Sinc@x, {x, 0, 10}] setOpts[delay, PlotStyle -> Red] 
setOpts[delay, PlotStyle -> RGBColor[1, 0, 0]] (* failure *) 

I shall use the same method I did for:

whichThis will require my step function from: How do I evaluate only one step of an expression?

SetAttributes[step, HoldAll] step[expr_] := Module[{P}, P = (P = Return[#, TraceScan] &) &; TraceScan[P, expr, TraceDepth -> 1] ] setOpts[other_, new__] := step[other] /. _[x_] :> setOpts[x, new] 

Now:

setOpts[delay, PlotStyle -> Red] 

enter image description here


Related:

Another thought-provoking question Jacob, and sadly one I missed until today. After only brief consideration I think perhaps what you want can be done more simply but I look forward to your critique of my proposal.

Assumptions:

  • Exact preservation of the structure of option lists is not required
  • The specific order of options is not significant
  • Option rules may be safely evaluated

Basic Proposal

SetAttributes[setOpts, HoldFirst] setOpts[head_[args___, opts : OptionsPattern[]], new__] := First /@ GatherBy[Flatten@{new, opts}, First] /. {op___} :> head[args, op] 

Test:

setOpts[f[1], q -> r] 
f[1, q -> r] 
setOpts[ f[2, q -> b, z -> x, {aa -> bb -> cc, foo :> bar}], z -> dog, a :> cat ] 
f[2, z -> dog, a :> cat, q -> b, aa -> bb -> cc, foo :> bar] 
setOpts[Plot[Sinc@x, {x, 0, 10}], PlotStyle -> Red] 

enter image description here

Extended definition

To make the following case work an additional definition is required:

delay := Plot[Sinc@x, {x, 0, 10}] setOpts[delay, PlotStyle -> Red] 
setOpts[delay, PlotStyle -> RGBColor[1, 0, 0]] (* failure *) 

I shall use the same method I did for:

which will require my step function from: How do I evaluate only one step of an expression?

setOpts[other_, new__] := step[other] /. _[x_] :> setOpts[x, new] 

Now:

setOpts[delay, PlotStyle -> Red] 

enter image description here


Related:

Another thought-provoking question Jacob, and sadly one I missed until today. After only brief consideration I think perhaps what you want can be done more simply but I look forward to your critique of my proposal.

Assumptions:

  • Exact preservation of the structure of option lists is not required
  • The specific order of options is not significant
  • Option rules may be safely evaluated

Basic Proposal

SetAttributes[setOpts, HoldFirst] setOpts[head_[args___, opts : OptionsPattern[]], new__] := First /@ GatherBy[Flatten@{new, opts}, First] /. {op___} :> head[args, op] 

Test:

setOpts[f[1], q -> r] 
f[1, q -> r] 
setOpts[ f[2, q -> b, z -> x, {aa -> bb -> cc, foo :> bar}], z -> dog, a :> cat ] 
f[2, z -> dog, a :> cat, q -> b, aa -> bb -> cc, foo :> bar] 
setOpts[Plot[Sinc@x, {x, 0, 10}], PlotStyle -> Red] 

enter image description here

Extended definition

To make the following case work an additional definition is required:

delay := Plot[Sinc@x, {x, 0, 10}] setOpts[delay, PlotStyle -> Red] 
setOpts[delay, PlotStyle -> RGBColor[1, 0, 0]] (* failure *) 

I shall use the same method I did for:

This will require my step function from: How do I evaluate only one step of an expression?

SetAttributes[step, HoldAll] step[expr_] := Module[{P}, P = (P = Return[#, TraceScan] &) &; TraceScan[P, expr, TraceDepth -> 1] ] setOpts[other_, new__] := step[other] /. _[x_] :> setOpts[x, new] 

Now:

setOpts[delay, PlotStyle -> Red] 

enter image description here


Related:

added 135 characters in body
Source Link
Mr.Wizard
  • 275.2k
  • 34
  • 606
  • 1.5k

Another thought-provoking question Jacob, and sadly one I missed until today. After only brief consideration I think perhaps what you want can be done more simply but I look forward to your critique of my proposal.

Assumptions:

  • Exact preservation of the structure of option lists is not required
  • The specific order of options is not significant
  • Option rules may be safely evaluated

Basic Proposal

SetAttributes[setOpts, HoldFirst] setOpts[head_[args___, opts : OptionsPattern[]], new__] := First /@ GatherBy[Flatten@{new, opts}, First] /. {op___} :> head[args, op] 

Test:

setOpts[f[1], q -> r] 
f[1, q -> r] 
setOpts[ f[2, q -> b, z -> x, {aa -> bb -> cc, foo :> bar}], z -> dog, a :> cat ] 
f[2, z -> dog, a :> cat, q -> b, aa -> bb -> cc, foo :> bar] 
setOpts[Plot[Sinc@x, {x, 0, 10}], PlotStyle -> Red] 

enter image description here

Extended definition

To make the following case work an additional definition is required:

delay := Plot[Sinc@x, {x, 0, 10}] setOpts[delay, PlotStyle -> Red] 
setOpts[delay, PlotStyle -> RGBColor[1, 0, 0]] (* failure *) 

I shall use the same method I did for:

which will require my step function from: How do I evaluate only one step of an expression?

setOpts[other_, new__] := step[other] /. _[x_] :> setOpts[x, new] 

Now:

setOpts[delay, PlotStyle -> Red] 

enter image description here


Related:

Another thought-provoking question Jacob, and sadly one I missed until today. After only brief consideration I think perhaps what you want can be done more simply but I look forward to your critique of my proposal.

Assumptions:

  • Exact preservation of the structure of option lists is not required
  • The specific order of options is not significant
  • Option rules may be safely evaluated

Basic Proposal

SetAttributes[setOpts, HoldFirst] setOpts[head_[args___, opts : OptionsPattern[]], new__] := First /@ GatherBy[Flatten@{new, opts}, First] /. {op___} :> head[args, op] 

Test:

setOpts[f[1], q -> r] 
f[1, q -> r] 
setOpts[ f[2, q -> b, z -> x, {aa -> bb -> cc, foo :> bar}], z -> dog, a :> cat ] 
f[2, z -> dog, a :> cat, q -> b, aa -> bb -> cc, foo :> bar] 
setOpts[Plot[Sinc@x, {x, 0, 10}], PlotStyle -> Red] 

enter image description here

Extended definition

To make the following case work an additional definition is required:

delay := Plot[Sinc@x, {x, 0, 10}] setOpts[delay, PlotStyle -> Red] 
setOpts[delay, PlotStyle -> RGBColor[1, 0, 0]] (* failure *) 

I shall use the same method I did for:

which will require my step function from: How do I evaluate only one step of an expression?

setOpts[other_, new__] := step[other] /. _[x_] :> setOpts[x, new] 

Now:

setOpts[delay, PlotStyle -> Red] 

enter image description here

Another thought-provoking question Jacob, and sadly one I missed until today. After only brief consideration I think perhaps what you want can be done more simply but I look forward to your critique of my proposal.

Assumptions:

  • Exact preservation of the structure of option lists is not required
  • The specific order of options is not significant
  • Option rules may be safely evaluated

Basic Proposal

SetAttributes[setOpts, HoldFirst] setOpts[head_[args___, opts : OptionsPattern[]], new__] := First /@ GatherBy[Flatten@{new, opts}, First] /. {op___} :> head[args, op] 

Test:

setOpts[f[1], q -> r] 
f[1, q -> r] 
setOpts[ f[2, q -> b, z -> x, {aa -> bb -> cc, foo :> bar}], z -> dog, a :> cat ] 
f[2, z -> dog, a :> cat, q -> b, aa -> bb -> cc, foo :> bar] 
setOpts[Plot[Sinc@x, {x, 0, 10}], PlotStyle -> Red] 

enter image description here

Extended definition

To make the following case work an additional definition is required:

delay := Plot[Sinc@x, {x, 0, 10}] setOpts[delay, PlotStyle -> Red] 
setOpts[delay, PlotStyle -> RGBColor[1, 0, 0]] (* failure *) 

I shall use the same method I did for:

which will require my step function from: How do I evaluate only one step of an expression?

setOpts[other_, new__] := step[other] /. _[x_] :> setOpts[x, new] 

Now:

setOpts[delay, PlotStyle -> Red] 

enter image description here


Related:

replaced http://mathematica.stackexchange.com/ with https://mathematica.stackexchange.com/
Source Link

Another thought-provoking question Jacob, and sadly one I missed until today. After only brief consideration I think perhaps what you want can be done more simply but I look forward to your critique of my proposal.

Assumptions:

  • Exact preservation of the structure of option lists is not required
  • The specific order of options is not significant
  • Option rules may be safely evaluated

Basic Proposal

SetAttributes[setOpts, HoldFirst] setOpts[head_[args___, opts : OptionsPattern[]], new__] := First /@ GatherBy[Flatten@{new, opts}, First] /. {op___} :> head[args, op] 

Test:

setOpts[f[1], q -> r] 
f[1, q -> r] 
setOpts[ f[2, q -> b, z -> x, {aa -> bb -> cc, foo :> bar}], z -> dog, a :> cat ] 
f[2, z -> dog, a :> cat, q -> b, aa -> bb -> cc, foo :> bar] 
setOpts[Plot[Sinc@x, {x, 0, 10}], PlotStyle -> Red] 

enter image description here

Extended definition

To make the following case work an additional definition is required:

delay := Plot[Sinc@x, {x, 0, 10}] setOpts[delay, PlotStyle -> Red] 
setOpts[delay, PlotStyle -> RGBColor[1, 0, 0]] (* failure *) 

I shall use the same method I did for:

which will require my step function from: How do I evaluate only one step of an expression?How do I evaluate only one step of an expression?

setOpts[other_, new__] := step[other] /. _[x_] :> setOpts[x, new] 

Now:

setOpts[delay, PlotStyle -> Red] 

enter image description here

Another thought-provoking question Jacob, and sadly one I missed until today. After only brief consideration I think perhaps what you want can be done more simply but I look forward to your critique of my proposal.

Assumptions:

  • Exact preservation of the structure of option lists is not required
  • The specific order of options is not significant
  • Option rules may be safely evaluated

Basic Proposal

SetAttributes[setOpts, HoldFirst] setOpts[head_[args___, opts : OptionsPattern[]], new__] := First /@ GatherBy[Flatten@{new, opts}, First] /. {op___} :> head[args, op] 

Test:

setOpts[f[1], q -> r] 
f[1, q -> r] 
setOpts[ f[2, q -> b, z -> x, {aa -> bb -> cc, foo :> bar}], z -> dog, a :> cat ] 
f[2, z -> dog, a :> cat, q -> b, aa -> bb -> cc, foo :> bar] 
setOpts[Plot[Sinc@x, {x, 0, 10}], PlotStyle -> Red] 

enter image description here

Extended definition

To make the following case work an additional definition is required:

delay := Plot[Sinc@x, {x, 0, 10}] setOpts[delay, PlotStyle -> Red] 
setOpts[delay, PlotStyle -> RGBColor[1, 0, 0]] (* failure *) 

I shall use the same method I did for:

which will require my step function from: How do I evaluate only one step of an expression?

setOpts[other_, new__] := step[other] /. _[x_] :> setOpts[x, new] 

Now:

setOpts[delay, PlotStyle -> Red] 

enter image description here

Another thought-provoking question Jacob, and sadly one I missed until today. After only brief consideration I think perhaps what you want can be done more simply but I look forward to your critique of my proposal.

Assumptions:

  • Exact preservation of the structure of option lists is not required
  • The specific order of options is not significant
  • Option rules may be safely evaluated

Basic Proposal

SetAttributes[setOpts, HoldFirst] setOpts[head_[args___, opts : OptionsPattern[]], new__] := First /@ GatherBy[Flatten@{new, opts}, First] /. {op___} :> head[args, op] 

Test:

setOpts[f[1], q -> r] 
f[1, q -> r] 
setOpts[ f[2, q -> b, z -> x, {aa -> bb -> cc, foo :> bar}], z -> dog, a :> cat ] 
f[2, z -> dog, a :> cat, q -> b, aa -> bb -> cc, foo :> bar] 
setOpts[Plot[Sinc@x, {x, 0, 10}], PlotStyle -> Red] 

enter image description here

Extended definition

To make the following case work an additional definition is required:

delay := Plot[Sinc@x, {x, 0, 10}] setOpts[delay, PlotStyle -> Red] 
setOpts[delay, PlotStyle -> RGBColor[1, 0, 0]] (* failure *) 

I shall use the same method I did for:

which will require my step function from: How do I evaluate only one step of an expression?

setOpts[other_, new__] := step[other] /. _[x_] :> setOpts[x, new] 

Now:

setOpts[delay, PlotStyle -> Red] 

enter image description here

added 2 characters in body
Source Link
Mr.Wizard
  • 275.2k
  • 34
  • 606
  • 1.5k
Loading
add links, for my own personal organization
Source Link
Mr.Wizard
  • 275.2k
  • 34
  • 606
  • 1.5k
Loading
Source Link
Mr.Wizard
  • 275.2k
  • 34
  • 606
  • 1.5k
Loading