Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- Options[saveData] = {Method -> "Addition"};
- saveData[vars_, opts : OptionsPattern[{saveData}]] :=
- saveData[vars, OptionValue[Method]]
- saveData[vars_, "Addition"] := Total @ vars
- saveData[vars_, "Multiplication"] := Times @@ vars
- saveData[{1, 2, 3}, Method->"Multiplication"]
- (* 6 *)
- saveData[{a, b, c}, Method->"Addition"]
- (* a + b + c *)
- <method-opt> = ( 'Method' , <rule-arrow> ) | ( <opt-spec> | <core-method> ) ;
- <opt-head> = 'Method' | 'WorkingPrecision' | 'Delimiter' ;
- <core-method> = 'Addition' | 'Multiplication' | 'CompensatedSummation' ;
- <opt-rule> = <opt-head> , <rule-arrow> | ( <core-method> | <opt-value> | <opt-spec> ) ;
- <opt-value> = '_String' ;
- <opt-spec> = '{' | <core-method> , [ ',' | <opt-list> ] | '}' ;
- <opt-list> = <opt-rule> , { ',' | <opt-rule> } ;
- <rule-arrow> = '->' | '[Rule]' ;
- "Method -> {Multiplication, Method -> {CompensatedSummation, WorkingPrecision -> 40}}"
- "Method -> {Addition, Method -> CompensatedSummation, WorkingPrecision -> 34}"
- <method-opt> = ( 'Method' , <rule-arrow> ) |> <opt-spec> ;
- <opt-spec> = <add-method> | <mult-method> ;
- <opt-head> = 'Method' | 'WorkingPrecision' | 'Delimiter' ;
- <add-method> = 'Addition' | '{' | 'Addition' , [ ',' | <add-opt-list> ] | '}' ;
- <mult-method> = 'Multiplication' | '{' | 'Multiplication' , [ ',' | <mult-opt-list> ] | '}' ;
- <compsum-method> = 'CompensatedSummation' | '{' | 'CompensatedSummation' , [ ',' | <compsum-opt-list> ] | '}' ;
- <add-opt-list> = <add-opt-rule> , { ',' | <add-opt-rule> } ;
- <mult-opt-list> = <mult-opt-rule> , { ',' | <mult-opt-rule> } ;
- <compsum-opt-list> = <compsum-opt-rule> , { ',' | <compsum-opt-rule> } ;
- <add-opt-rule> = ( 'Method' | 'Delimiter' ) , <rule-arrow> | ( <opt-value> | <compsum-method> ) ;
- <mult-opt-rule> = 'Delimiter', <rule-arrow> |> <opt-value> ;
- <compsum-opt-rule> = 'WorkingPrecision', <rule-arrow> | <opt-value> ;
- <opt-value> = '_String' ;
- <rule-arrow> = '->' | '[Rule]' ;
- Options[saveData] = {Method -> Automatic};
- $MethodValues = {"Addition", "Multiplication", Automatic};
- saveData::bdmtd =
- "Value of Method -> `` is not one of " <> ToString@$MethodValues;
- saveData[vars_, opts : OptionsPattern[]] :=
- Module[{mtd},
- mtd = OptionValue[Method];
- If[! MemberQ[$MethodValues, mtd],
- Message[saveData::bdmtd, mtd];
- Return[$Failed]
- ];
- Switch[mtd,
- Automatic | "Addition",
- Total@vars,
- "Multiplication",
- Times @@ vars
- ]
- ]
- saveData[{1, 2, 3}, Method -> "1"]
- saveData[{1, 2, 3}, Method -> "Multiplication"]
- saveData[{a, b, c}, Method -> "Addition"]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement