Skip to main content
replaced http://mathematica.stackexchange.com/ with https://mathematica.stackexchange.com/
Source Link

If you only want the default values for the leading parameters (or some other order you choose), and not an arbitrary parameter at the time of the function call, you can prioritize the patterns as described heredescribed here.

If your function does have Hold attributes you will need a slightly more complicated replacement. I will make use of RuleCondition(1)(1):

Michael E2Michael E2 showed what is perhaps the most declarative method. I don't think by itself it is as easily extensible, but then again there must be a limit to how many arguments a user is going to count anyway. Nevertheless one could automate that method with meta-programming, so let's do it.

If you only want the default values for the leading parameters (or some other order you choose), and not an arbitrary parameter at the time of the function call, you can prioritize the patterns as described here.

If your function does have Hold attributes you will need a slightly more complicated replacement. I will make use of RuleCondition(1):

Michael E2 showed what is perhaps the most declarative method. I don't think by itself it is as easily extensible, but then again there must be a limit to how many arguments a user is going to count anyway. Nevertheless one could automate that method with meta-programming, so let's do it.

If you only want the default values for the leading parameters (or some other order you choose), and not an arbitrary parameter at the time of the function call, you can prioritize the patterns as described here.

If your function does have Hold attributes you will need a slightly more complicated replacement. I will make use of RuleCondition(1):

Michael E2 showed what is perhaps the most declarative method. I don't think by itself it is as easily extensible, but then again there must be a limit to how many arguments a user is going to count anyway. Nevertheless one could automate that method with meta-programming, so let's do it.

deleted 78 characters in body
Source Link
Mr.Wizard
  • 275.2k
  • 34
  • 606
  • 1.5k
makeDefaults[fname_Symbol, defaults_List] := With[{par =fname Table[Unique["$"],@@@ {Length @ defaults}]}Pattern[#,  With[{set = Hold[# := #2]_] &[fname& @@/@ (Pattern[##, _]#} & /@ par)Table[Unique["$"], fname{Length @@@ par]defaults},] // Do[ ReplacePart[set, {0SetDelayed ->@@ IdentityReplacePart[#, {1, {1, n} -> Null, {1, 2, n} -> defaults[[n]]}],   {n, Length @ defaults}   ]  ]   ]& 
makeDefaults[fname_Symbol, defaults_List] := With[{par = Table[Unique["$"], {Length @ defaults}]},  With[{set = Hold[# := #2] &[fname @@ (Pattern[#, _] & /@ par), fname @@ par]}, Do[ ReplacePart[set, {0 -> Identity, {1, 1, n} -> Null, {1, 2, n} -> defaults[[n]]}], {n, Length @ defaults}   ]  ]   ] 
makeDefaults[fname_Symbol, defaults_List] := fname @@@ {Pattern[#, _] & /@ #, #} & @ Table[Unique["$"], {Length @ defaults}] // Do[ SetDelayed @@ ReplacePart[#, {{1, n} -> Null, {2, n} -> defaults[[n]]}],   {n, Length @ defaults} ] & 
deleted 58 characters in body
Source Link
Mr.Wizard
  • 275.2k
  • 34
  • 606
  • 1.5k
makeDefaults[fname_Symbol, defaults_List] := With[{par = Table[Unique["$"], {Length @ defaults}]}, With[{set = Hold[# := #2] &[fname @@ (Pattern[#, _] & /@ par), fname @@ par]}, Do[ ReplacePart[ setReplacePart[set,  {0 -> Identity, {1, 1, n} -> Null, {1, 2, n} -> RuleCondition @ defaults[[n]]} ], {n, Length @ defaults} ] ] ] 
makeDefaults[fname_Symbol, defaults_List] := With[{par = Table[Unique["$"], {Length @ defaults}]}, With[{set = Hold[# := #2] &[fname @@ (Pattern[#, _] & /@ par), fname @@ par]}, Do[ ReplacePart[ set,  {0 -> Identity, {1, 1, n} -> Null, {1, 2, n} -> RuleCondition @ defaults[[n]]} ], {n, Length @ defaults} ] ] ] 
makeDefaults[fname_Symbol, defaults_List] := With[{par = Table[Unique["$"], {Length @ defaults}]}, With[{set = Hold[# := #2] &[fname @@ (Pattern[#, _] & /@ par), fname @@ par]}, Do[ ReplacePart[set, {0 -> Identity, {1, 1, n} -> Null, {1, 2, n} -> defaults[[n]]}], {n, Length @ defaults} ] ] ] 
added 44 characters in body
Source Link
Mr.Wizard
  • 275.2k
  • 34
  • 606
  • 1.5k
Loading
added 2499 characters in body
Source Link
Mr.Wizard
  • 275.2k
  • 34
  • 606
  • 1.5k
Loading
added 383 characters in body
Source Link
Mr.Wizard
  • 275.2k
  • 34
  • 606
  • 1.5k
Loading
added 655 characters in body
Source Link
Mr.Wizard
  • 275.2k
  • 34
  • 606
  • 1.5k
Loading
added 72 characters in body
Source Link
Mr.Wizard
  • 275.2k
  • 34
  • 606
  • 1.5k
Loading
Source Link
Mr.Wizard
  • 275.2k
  • 34
  • 606
  • 1.5k
Loading