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]
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!