Advertisement
Guest User

Untitled

a guest
Mar 20th, 2019
71
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.55 KB | None | 0 0
  1. Options[saveData] = {Method -> "Addition"};
  2. saveData[vars_, opts : OptionsPattern[{saveData}]] :=
  3. saveData[vars, OptionValue[Method]]
  4. saveData[vars_, "Addition"] := Total @ vars
  5. saveData[vars_, "Multiplication"] := Times @@ vars
  6.  
  7. saveData[{1, 2, 3}, Method->"Multiplication"]
  8. (* 6 *)
  9. saveData[{a, b, c}, Method->"Addition"]
  10. (* a + b + c *)
  11.  
  12. <method-opt> = ( 'Method' , <rule-arrow> ) | ( <opt-spec> | <core-method> ) ;
  13. <opt-head> = 'Method' | 'WorkingPrecision' | 'Delimiter' ;
  14. <core-method> = 'Addition' | 'Multiplication' | 'CompensatedSummation' ;
  15. <opt-rule> = <opt-head> , <rule-arrow> | ( <core-method> | <opt-value> | <opt-spec> ) ;
  16. <opt-value> = '_String' ;
  17. <opt-spec> = '{' | <core-method> , [ ',' | <opt-list> ] | '}' ;
  18. <opt-list> = <opt-rule> , { ',' | <opt-rule> } ;
  19. <rule-arrow> = '->' | '[Rule]' ;
  20.  
  21. "Method -> {Multiplication, Method -> {CompensatedSummation, WorkingPrecision -> 40}}"
  22.  
  23. "Method -> {Addition, Method -> CompensatedSummation, WorkingPrecision -> 34}"
  24.  
  25. <method-opt> = ( 'Method' , <rule-arrow> ) |> <opt-spec> ;
  26. <opt-spec> = <add-method> | <mult-method> ;
  27. <opt-head> = 'Method' | 'WorkingPrecision' | 'Delimiter' ;
  28. <add-method> = 'Addition' | '{' | 'Addition' , [ ',' | <add-opt-list> ] | '}' ;
  29. <mult-method> = 'Multiplication' | '{' | 'Multiplication' , [ ',' | <mult-opt-list> ] | '}' ;
  30. <compsum-method> = 'CompensatedSummation' | '{' | 'CompensatedSummation' , [ ',' | <compsum-opt-list> ] | '}' ;
  31. <add-opt-list> = <add-opt-rule> , { ',' | <add-opt-rule> } ;
  32. <mult-opt-list> = <mult-opt-rule> , { ',' | <mult-opt-rule> } ;
  33. <compsum-opt-list> = <compsum-opt-rule> , { ',' | <compsum-opt-rule> } ;
  34. <add-opt-rule> = ( 'Method' | 'Delimiter' ) , <rule-arrow> | ( <opt-value> | <compsum-method> ) ;
  35. <mult-opt-rule> = 'Delimiter', <rule-arrow> |> <opt-value> ;
  36. <compsum-opt-rule> = 'WorkingPrecision', <rule-arrow> | <opt-value> ;
  37. <opt-value> = '_String' ;
  38. <rule-arrow> = '->' | '[Rule]' ;
  39.  
  40. Options[saveData] = {Method -> Automatic};
  41. $MethodValues = {"Addition", "Multiplication", Automatic};
  42.  
  43. saveData::bdmtd =
  44. "Value of Method -> `` is not one of " <> ToString@$MethodValues;
  45.  
  46. saveData[vars_, opts : OptionsPattern[]] :=
  47. Module[{mtd},
  48. mtd = OptionValue[Method];
  49. If[! MemberQ[$MethodValues, mtd],
  50. Message[saveData::bdmtd, mtd];
  51. Return[$Failed]
  52. ];
  53. Switch[mtd,
  54. Automatic | "Addition",
  55. Total@vars,
  56. "Multiplication",
  57. Times @@ vars
  58. ]
  59. ]
  60.  
  61. saveData[{1, 2, 3}, Method -> "1"]
  62. saveData[{1, 2, 3}, Method -> "Multiplication"]
  63. saveData[{a, b, c}, Method -> "Addition"]
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement