How to specify optional string parameters in a function signature and identify the pattern
Clash 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?
pattern-matching string-manipulation
add a comment |Â
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?
pattern-matching string-manipulation
add a comment |Â
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?
pattern-matching string-manipulation
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?
pattern-matching string-manipulation
asked Aug 25 at 21:43
CalvinDale
1694
1694
add a comment |Â
add a comment |Â
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]
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
add a comment |Â
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]
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
add a comment |Â
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]
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
add a comment |Â
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]
You can use Pattern
:
f[s : "a" | "b"] := s <> ": stuff"
f /@ "a", "b", "c", 2
"a: stuff", "b: stuff", f["c"], f[2]
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
add a comment |Â
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
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
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
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password