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

Following your clarification this seems to be OK, though I would agree that a cleaner solution would be nice:

Options[f] = {foo -> bar, ImageSize -> 333}; f[args__, opts : OptionsPattern[{f, Button, Tooltip}]] := Append[ FilterRules[{opts, Options @ f}, Options @ #] & /@ {Button, Tooltip}, OptionValue[foo] ] // Column 

Test:

f[1, 2, Background -> Blue, AutoAction -> False, TooltipDelay -> 1] 
{Background -> RGBColor[0, 0, 1], AutoAction -> False, ImageSize -> 333} {Background -> RGBColor[0, 0, 1], TooltipDelay -> 1} bar 

Following your updated requirements the only streamlining I can think to recommend is to combine the functionality of your mergeRules with that of FilterRules. This is a trivial refactoring but again I hope you find some value in the idea.

getRules[base_Symbol, op___][target_Symbol] := First /@ GatherBy[{op, Options @ base} ~FilterRules~ Options[target], First] Options[f] = {foo -> bar, ImageSize -> 333}; f[args__, opts : OptionsPattern[{f, Button, Tooltip}]] := Append[getRules[f, opts] /@ {Button, Tooltip}, OptionValue[foo]] // Column 

test:

f[1, 2, Background -> Blue, AutoAction -> False, TooltipDelay -> 1, ImageSize -> 99] 
{Background -> RGBColor[0, 0, 1], AutoAction -> False, ImageSize -> 99} {Background -> RGBColor[0, 0, 1], TooltipDelay -> 1} bar 

getRules could also be written with KeyTake

getRules[base_Symbol, op___][target_Symbol] := {op, Options @ base} // Flatten // KeyTake[Keys @ Options @ target] // Normal 

Following your clarification this seems to be OK, though I would agree that a cleaner solution would be nice:

Options[f] = {foo -> bar, ImageSize -> 333}; f[args__, opts : OptionsPattern[{f, Button, Tooltip}]] := Append[ FilterRules[{opts, Options @ f}, Options @ #] & /@ {Button, Tooltip}, OptionValue[foo] ] // Column 

Test:

f[1, 2, Background -> Blue, AutoAction -> False, TooltipDelay -> 1] 
{Background -> RGBColor[0, 0, 1], AutoAction -> False, ImageSize -> 333} {Background -> RGBColor[0, 0, 1], TooltipDelay -> 1} bar 

Following your updated requirements the only streamlining I can think to recommend is to combine the functionality of your mergeRules with that of FilterRules. This is a trivial refactoring but again I hope you find some value in the idea.

getRules[base_Symbol, op___][target_Symbol] := First /@ GatherBy[{op, Options @ base} ~FilterRules~ Options[target], First] Options[f] = {foo -> bar, ImageSize -> 333}; f[args__, opts : OptionsPattern[{f, Button, Tooltip}]] := Append[getRules[f, opts] /@ {Button, Tooltip}, OptionValue[foo]] // Column 

test:

f[1, 2, Background -> Blue, AutoAction -> False, TooltipDelay -> 1, ImageSize -> 99] 
{Background -> RGBColor[0, 0, 1], AutoAction -> False, ImageSize -> 99} {Background -> RGBColor[0, 0, 1], TooltipDelay -> 1} bar 

Following your clarification this seems to be OK, though I would agree that a cleaner solution would be nice:

Options[f] = {foo -> bar, ImageSize -> 333}; f[args__, opts : OptionsPattern[{f, Button, Tooltip}]] := Append[ FilterRules[{opts, Options @ f}, Options @ #] & /@ {Button, Tooltip}, OptionValue[foo] ] // Column 

Test:

f[1, 2, Background -> Blue, AutoAction -> False, TooltipDelay -> 1] 
{Background -> RGBColor[0, 0, 1], AutoAction -> False, ImageSize -> 333} {Background -> RGBColor[0, 0, 1], TooltipDelay -> 1} bar 

Following your updated requirements the only streamlining I can think to recommend is to combine the functionality of your mergeRules with that of FilterRules. This is a trivial refactoring but again I hope you find some value in the idea.

getRules[base_Symbol, op___][target_Symbol] := First /@ GatherBy[{op, Options @ base} ~FilterRules~ Options[target], First] Options[f] = {foo -> bar, ImageSize -> 333}; f[args__, opts : OptionsPattern[{f, Button, Tooltip}]] := Append[getRules[f, opts] /@ {Button, Tooltip}, OptionValue[foo]] // Column 

test:

f[1, 2, Background -> Blue, AutoAction -> False, TooltipDelay -> 1, ImageSize -> 99] 
{Background -> RGBColor[0, 0, 1], AutoAction -> False, ImageSize -> 99} {Background -> RGBColor[0, 0, 1], TooltipDelay -> 1} bar 

getRules could also be written with KeyTake

getRules[base_Symbol, op___][target_Symbol] := {op, Options @ base} // Flatten // KeyTake[Keys @ Options @ target] // Normal 
added 860 characters in body
Source Link
Mr.Wizard
  • 275.2k
  • 34
  • 606
  • 1.5k

Following your clarification this seems to be OK, though I would agree that a cleaner solution would be nice:

Options[f] = {foo -> bar, ImageSize -> 333}; f[args__, opts : OptionsPattern[{f, Button, Tooltip}]] := Append[ FilterRules[{opts, Options @ f}, Options @ #] & /@ {Button, Tooltip}, OptionValue[foo] ] // Column 

Test:

f[1, 2, Background -> Blue, AutoAction -> False, TooltipDelay -> 1] 
{Background -> RGBColor[0, 0, 1], AutoAction -> False, ImageSize -> 333} {Background -> RGBColor[0, 0, 1], TooltipDelay -> 1} bar 

Following your updated requirements the only streamlining I can think to recommend is to combine the functionality of your mergeRules with that of FilterRules. This is a trivial refactoring but again I hope you find some value in the idea.

getRules[base_Symbol, op___][target_Symbol] := First /@ GatherBy[{op, Options @ base} ~FilterRules~ Options[target], First] Options[f] = {foo -> bar, ImageSize -> 333}; f[args__, opts : OptionsPattern[{f, Button, Tooltip}]] := Append[getRules[f, opts] /@ {Button, Tooltip}, OptionValue[foo]] // Column 

test:

f[1, 2, Background -> Blue, AutoAction -> False, TooltipDelay -> 1, ImageSize -> 99] 
{Background -> RGBColor[0, 0, 1], AutoAction -> False, ImageSize -> 99} {Background -> RGBColor[0, 0, 1], TooltipDelay -> 1} bar 

Following your clarification this seems to be OK, though I would agree that a cleaner solution would be nice:

Options[f] = {foo -> bar, ImageSize -> 333}; f[args__, opts : OptionsPattern[{f, Button, Tooltip}]] := Append[ FilterRules[{opts, Options @ f}, Options @ #] & /@ {Button, Tooltip}, OptionValue[foo] ] // Column 

Test:

f[1, 2, Background -> Blue, AutoAction -> False, TooltipDelay -> 1] 
{Background -> RGBColor[0, 0, 1], AutoAction -> False, ImageSize -> 333} {Background -> RGBColor[0, 0, 1], TooltipDelay -> 1} bar 

Following your clarification this seems to be OK, though I would agree that a cleaner solution would be nice:

Options[f] = {foo -> bar, ImageSize -> 333}; f[args__, opts : OptionsPattern[{f, Button, Tooltip}]] := Append[ FilterRules[{opts, Options @ f}, Options @ #] & /@ {Button, Tooltip}, OptionValue[foo] ] // Column 

Test:

f[1, 2, Background -> Blue, AutoAction -> False, TooltipDelay -> 1] 
{Background -> RGBColor[0, 0, 1], AutoAction -> False, ImageSize -> 333} {Background -> RGBColor[0, 0, 1], TooltipDelay -> 1} bar 

Following your updated requirements the only streamlining I can think to recommend is to combine the functionality of your mergeRules with that of FilterRules. This is a trivial refactoring but again I hope you find some value in the idea.

getRules[base_Symbol, op___][target_Symbol] := First /@ GatherBy[{op, Options @ base} ~FilterRules~ Options[target], First] Options[f] = {foo -> bar, ImageSize -> 333}; f[args__, opts : OptionsPattern[{f, Button, Tooltip}]] := Append[getRules[f, opts] /@ {Button, Tooltip}, OptionValue[foo]] // Column 

test:

f[1, 2, Background -> Blue, AutoAction -> False, TooltipDelay -> 1, ImageSize -> 99] 
{Background -> RGBColor[0, 0, 1], AutoAction -> False, ImageSize -> 99} {Background -> RGBColor[0, 0, 1], TooltipDelay -> 1} bar 
added 52 characters in body
Source Link
Mr.Wizard
  • 275.2k
  • 34
  • 606
  • 1.5k

ThisFollowing your clarification this seems pretty straightforward to me but perhapsbe OK, though I miss the point of the questionwould agree that a cleaner solution would be nice:

Options[f] = {foo -> bar, ImageSize -> 333}; f[args__, opts : OptionsPattern[{f, Button, Tooltip}]] := Column[{Append[ FilterRules[{opts}, Options @ Button]f},   Options @ FilterRules[{opts},#] Options& /@ Tooltip]{Button, Tooltip}, OptionValue[foo] }] // Column 

Test:

f[1, 2, Background -> Blue, AutoAction -> False, TooltipDelay -> 1] 
{Background -> RGBColor[0, 0, 1], AutoAction  -> False, ImageSize -> 333} {Background -> RGBColor[0, 0, 1], TooltipDelay -> 1} bar 

This seems pretty straightforward to me but perhaps I miss the point of the question:

Options[f] = {foo -> bar}; f[args__, opts : OptionsPattern[{f, Button, Tooltip}]] := Column[{ FilterRules[{opts}, Options @ Button],   FilterRules[{opts}, Options @ Tooltip], OptionValue[foo] }] 

Test:

f[1, 2, Background -> Blue, AutoAction -> False, TooltipDelay -> 1] 
{Background -> RGBColor[0, 0, 1], AutoAction -> False} {Background -> RGBColor[0, 0, 1], TooltipDelay-> 1} bar 

Following your clarification this seems to be OK, though I would agree that a cleaner solution would be nice:

Options[f] = {foo -> bar, ImageSize -> 333}; f[args__, opts : OptionsPattern[{f, Button, Tooltip}]] := Append[ FilterRules[{opts, Options @ f}, Options @ #] & /@ {Button, Tooltip}, OptionValue[foo] ] // Column 

Test:

f[1, 2, Background -> Blue, AutoAction -> False, TooltipDelay -> 1] 
{Background -> RGBColor[0, 0, 1], AutoAction  -> False, ImageSize -> 333} {Background -> RGBColor[0, 0, 1], TooltipDelay -> 1} bar 
Source Link
Mr.Wizard
  • 275.2k
  • 34
  • 606
  • 1.5k
Loading