Message Boards Message Boards

0
|
3582 Views
|
3 Replies
|
3 Total Likes
View groups...
Share
Share this post:

How do I put patterns as option for a function?

Hello community.

I have a question about how to build a function with internal option for patterns. For example, I have this function:

f0[a_, b_] := 
 Module[{tab}, tab = Table[i, {i, Range[Length[a]]}]; 
  Take[a, tab[[b ;; b + 1]]]]

In the first term inside the function ("a_") I would like to have several patterns making the function give the following results for example ( in this case below the patterns are not specified inside the source function "f0", they are outside and not with the pattern names in string… ):

pat1 = {1, 2, 3, 4, 5, 6, 7, 8};
pat2 = {11, 12, 13, 14, 15};
pat3 = {21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31};

f0[pat1, 2]
f0[pat2, 3]
f0[pat3, 7]

im1

But my question is: how do I make these patterns built into the function ( as an option in the function ) so when I do it that way below it will automatically give me the same expected result. Below is how I would like it to be the input:

f0["pat1", 2]
f0["pat2", 3]
f0["pat3", 7]

At the moment I still have lack of experience with functions. My attempt to do this failed…! Probably because I'm doing wrong ( what's the right way to do that? ). My attempt:

f1[OptionsPattern[], b_] := 
 Module[{pat, tab}, 
  Options[f1] = {"pat1" -> {1, 2, 3, 4, 5, 6, 7, 8}, 
    "pat2" -> {11, 12, 13, 14, 15}, 
    "pat3" -> {21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}}; 
  pat = OptionValue["pat1", "pat2", "pat3"]; 
  tab = Table[i, {i, Range[Length[pat]]}]; 
  Take[pat, tab[[b ;; b + 1]]]]

My attempt gave this result (I failed):

In1:=f1[“pat1”,2]

Out1=f1[pat1,2]

I would like the result to be equal to the above result (at the beginning of the post):

In1:=f1[“pat1”,2]

In2:=f1[“pat2”,3]

In3:=f1[“pat3”,7]

Out1={2,3}

Out2={13,14}

Out3={27,28}

How do I do this? Can someone help me, please?

Thank you!

POSTED BY: Claudio Chaib
3 Replies

How about:

pat1 = {1, 2, 3, 4, 5, 6, 7, 8};
pat2 = {11, 12, 13, 14, 15};
pat3 = {21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31};

f[pat_String, indx_Integer] := ToExpression[pat][[{indx, indx + 1}]]
POSTED BY: Henrik Schachner
Posted 5 years ago

Claudio,

Is there a reason why you need to use Options? How about this?

f1[option_, b_] := Module[{tab, pat,
   options = <|"pat1" -> {1, 2, 3, 4, 5, 6, 7, 8}, 
     "pat2" -> {11, 12, 13, 14, 15}, 
     "pat3" -> {21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31}|>},
  pat = options[option];
  tab = Table[i, {i, Range[Length[pat]]}];
  Take[pat, tab[[b ;; b + 1]]]]

f1["pat1", 2]
(* {2, 3} *)

f1["pat2", 3]
(* {13, 14} *)
POSTED BY: Rohit Namjoshi

Thank you very much Rohit, helped me a lot! ... Your way is simple and works great for me... I think there was no reason for me to use Options, I just didn't know how to do it that way you taught me. Thanks again!

POSTED BY: Claudio Chaib
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard

Group Abstract Group Abstract