How to specify optional string parameters in a function signature and identify the pattern

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
2
down vote

favorite












As a newbie, I've got a rather simple question.



I would like to define a function that accepts one of a few possible strings.



I.e., I wish to do the following.



f[s_"a"|"b"] := do something useful with s


But of course that syntax doesn't work. Note that the function definition is not overloaded.



I achieved this the following way.



f[key2_ /; MemberQ[Map[StringMatchQ[key2, #] &, "Cat", "Bird", "Mouse"], True]] := someAssoc["Key1"][key2]


Is there a better way to express this?







share|improve this question
























    up vote
    2
    down vote

    favorite












    As a newbie, I've got a rather simple question.



    I would like to define a function that accepts one of a few possible strings.



    I.e., I wish to do the following.



    f[s_"a"|"b"] := do something useful with s


    But of course that syntax doesn't work. Note that the function definition is not overloaded.



    I achieved this the following way.



    f[key2_ /; MemberQ[Map[StringMatchQ[key2, #] &, "Cat", "Bird", "Mouse"], True]] := someAssoc["Key1"][key2]


    Is there a better way to express this?







    share|improve this question






















      up vote
      2
      down vote

      favorite









      up vote
      2
      down vote

      favorite











      As a newbie, I've got a rather simple question.



      I would like to define a function that accepts one of a few possible strings.



      I.e., I wish to do the following.



      f[s_"a"|"b"] := do something useful with s


      But of course that syntax doesn't work. Note that the function definition is not overloaded.



      I achieved this the following way.



      f[key2_ /; MemberQ[Map[StringMatchQ[key2, #] &, "Cat", "Bird", "Mouse"], True]] := someAssoc["Key1"][key2]


      Is there a better way to express this?







      share|improve this question












      As a newbie, I've got a rather simple question.



      I would like to define a function that accepts one of a few possible strings.



      I.e., I wish to do the following.



      f[s_"a"|"b"] := do something useful with s


      But of course that syntax doesn't work. Note that the function definition is not overloaded.



      I achieved this the following way.



      f[key2_ /; MemberQ[Map[StringMatchQ[key2, #] &, "Cat", "Bird", "Mouse"], True]] := someAssoc["Key1"][key2]


      Is there a better way to express this?









      share|improve this question











      share|improve this question




      share|improve this question










      asked Aug 25 at 21:43









      CalvinDale

      1694




      1694




















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          3
          down vote



          accepted










          You can use Pattern:



          f[s : "a" | "b"] := s <> ": stuff"
          f /@ "a", "b", "c", 2



          "a: stuff", "b: stuff", f["c"], f[2]







          share|improve this answer




















          • Great thanks. As I suspected, the language has an affordance.
            – CalvinDale
            Aug 25 at 22:13










          • @CalvinDale, my pleasure. Thank you for the accept.
            – kglr
            Aug 25 at 22:15










          Your Answer




          StackExchange.ifUsing("editor", function ()
          return StackExchange.using("mathjaxEditing", function ()
          StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
          StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
          );
          );
          , "mathjax-editing");

          StackExchange.ready(function()
          var channelOptions =
          tags: "".split(" "),
          id: "387"
          ;
          initTagRenderer("".split(" "), "".split(" "), channelOptions);

          StackExchange.using("externalEditor", function()
          // Have to fire editor after snippets, if snippets enabled
          if (StackExchange.settings.snippets.snippetsEnabled)
          StackExchange.using("snippets", function()
          createEditor();
          );

          else
          createEditor();

          );

          function createEditor()
          StackExchange.prepareEditor(
          heartbeatType: 'answer',
          convertImagesToLinks: false,
          noModals: false,
          showLowRepImageUploadWarning: true,
          reputationToPostImages: null,
          bindNavPrevention: true,
          postfix: "",
          onDemand: true,
          discardSelector: ".discard-answer"
          ,immediatelyShowMarkdownHelp:true
          );



          );













           

          draft saved


          draft discarded


















          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f180650%2fhow-to-specify-optional-string-parameters-in-a-function-signature-and-identify-t%23new-answer', 'question_page');

          );

          Post as a guest






























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes








          up vote
          3
          down vote



          accepted










          You can use Pattern:



          f[s : "a" | "b"] := s <> ": stuff"
          f /@ "a", "b", "c", 2



          "a: stuff", "b: stuff", f["c"], f[2]







          share|improve this answer




















          • Great thanks. As I suspected, the language has an affordance.
            – CalvinDale
            Aug 25 at 22:13










          • @CalvinDale, my pleasure. Thank you for the accept.
            – kglr
            Aug 25 at 22:15














          up vote
          3
          down vote



          accepted










          You can use Pattern:



          f[s : "a" | "b"] := s <> ": stuff"
          f /@ "a", "b", "c", 2



          "a: stuff", "b: stuff", f["c"], f[2]







          share|improve this answer




















          • Great thanks. As I suspected, the language has an affordance.
            – CalvinDale
            Aug 25 at 22:13










          • @CalvinDale, my pleasure. Thank you for the accept.
            – kglr
            Aug 25 at 22:15












          up vote
          3
          down vote



          accepted







          up vote
          3
          down vote



          accepted






          You can use Pattern:



          f[s : "a" | "b"] := s <> ": stuff"
          f /@ "a", "b", "c", 2



          "a: stuff", "b: stuff", f["c"], f[2]







          share|improve this answer












          You can use Pattern:



          f[s : "a" | "b"] := s <> ": stuff"
          f /@ "a", "b", "c", 2



          "a: stuff", "b: stuff", f["c"], f[2]








          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Aug 25 at 21:50









          kglr

          158k8183382




          158k8183382











          • Great thanks. As I suspected, the language has an affordance.
            – CalvinDale
            Aug 25 at 22:13










          • @CalvinDale, my pleasure. Thank you for the accept.
            – kglr
            Aug 25 at 22:15
















          • Great thanks. As I suspected, the language has an affordance.
            – CalvinDale
            Aug 25 at 22:13










          • @CalvinDale, my pleasure. Thank you for the accept.
            – kglr
            Aug 25 at 22:15















          Great thanks. As I suspected, the language has an affordance.
          – CalvinDale
          Aug 25 at 22:13




          Great thanks. As I suspected, the language has an affordance.
          – CalvinDale
          Aug 25 at 22:13












          @CalvinDale, my pleasure. Thank you for the accept.
          – kglr
          Aug 25 at 22:15




          @CalvinDale, my pleasure. Thank you for the accept.
          – kglr
          Aug 25 at 22:15

















           

          draft saved


          draft discarded















































           


          draft saved


          draft discarded














          StackExchange.ready(
          function ()
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f180650%2fhow-to-specify-optional-string-parameters-in-a-function-signature-and-identify-t%23new-answer', 'question_page');

          );

          Post as a guest













































































          Comments

          Popular posts from this blog

          Long meetings (6-7 hours a day): Being “babysat” by supervisor

          Is the Concept of Multiple Fantasy Races Scientifically Flawed? [closed]

          Confectionery