0
$\begingroup$

times is a list of times i.e. matching {{hour_, min_, sec_Real}..}.

I want to call f[Dynamic@times].

The following works for a single argument to the function; f[Dynamic@(times: {{_, _, _Real}..})]

Edited; The single argument version above works. The multiple argument version below which is closer to the actual code I am using doesn't.

timestamps = {{2008, 1, 2, 14., 31., 0.}, {2008, 1, 2, 14., 32., 0.}};
bars = {{36.67, 36.69, 36.61, 36.65, 20800.}, {36.68, 36.71, 36.68, 36.68, 
  2600.}};
dates = {{2008, 1, 2}, {2008, 1, 2}};
times = {{14., 31., 0.}, {14., 32., 0.}};
indicators = {36.69, 36.71};

In[203]:= Clear@FormatCoordinates;
FormatCoordinates[pt_, timestamps_, bars_, indicators_] := 
 Block[{}, "Matched non-dynamic version 1."]
FormatCoordinates[pt_, dates_, times : {{_, _, _Real} ..}, 
  indicators_] := Block[{}, "Matched non-dynamic version 2."]
FormatCoordinates[pt_, Dynamic@timestamps_, Dynamic@bars_, 
  Dynamic@indicators_] := Block[{}, "Matched dynamic version 1."]
FormatCoordinates[pt_, Dynamic@dates_, 
  Dynamic@(times : {{_, _, _Real} ..}), Dynamic@indicators_] := 
 Block[{}, "Matched dynamic version 2."]

In[208]:= FormatCoordinates[dummy, timestamps, bars, indicators]
Out[208]= "Matched non-dynamic version 1."

In[209]:= FormatCoordinates[dummy, Dynamic@timestamps, Dynamic@bars, 
 Dynamic@indicators]
Out[209]= "Matched dynamic version 1."

In[210]:= FormatCoordinates[dummy, dates, times, indicators]
Out[210]= "Matched non-dynamic version 2."

In[211]:= FormatCoordinates[dummy, Dynamic@dates, Dynamic@times, 
 Dynamic@indicators]
Out[211]= "Matched dynamic version 1."

UPDATE: Read @Kuba's comment in the answer below for the solution.

$\endgroup$
4
  • 1
    $\begingroup$ Would you please add a usage example: an input along with a desired output. It is not clear what you require. If it helps, please add more than one example. $\endgroup$
    – Syed
    Commented Nov 11, 2021 at 16:52
  • $\begingroup$ @Syed - thanks, I've added an example. $\endgroup$ Commented Nov 11, 2021 at 19:10
  • $\begingroup$ Could you provide complete code that shows the issue? Looks like it ought to work to me. $\endgroup$
    – Carl Woll
    Commented Nov 11, 2021 at 20:00
  • $\begingroup$ @Carl - provided more complete code. $\endgroup$ Commented Nov 12, 2021 at 11:09

1 Answer 1

2
$\begingroup$

To match f[Dynamic@times] you do not need the double brackets and the Repeated. E.g. you could define:

ClearAll[f];
t = {hour_, min_, sec_Real};
f[Dynamic[{hour_, min_, sec_Real} ]] = "Worked";

If you now say:

f[Dynamic[{11, 11, 11.}]]

MMA responds with "Worked"

It is more complex with repeated dates. Names should not be used with "Repeated" as this implies that the corresponding values are the same:

ClearAll[f];
t = {hour_, min_, sec_Real};
f[Dynamic[{{_, _, _Real} .. }]] = "Worked";
f[Dynamic[{{11, 11, 11.}, {12, 12, 12.}}]]
(*Worked*)

If you want to get at the individual values you can give the whole input a name and get the values by indexing. E.g. to get the first value:

ClearAll[f];
t = {hour_, min_, sec_Real};
f[Dynamic[x : {{_, _, _Real} ..}]] := x[[1, 1]];
f[Dynamic[{{11, 12, 12.}, {12, 12, 12.}}]]
(*11*)
$\endgroup$
8
  • $\begingroup$ thanks for your answer but it looks like I was premature in approving it. It doesn't work for a list of times as stated in the original question. f[Dynamic[{{11, 11, 11.}, {12, 12, 12.}}]] will fail to match. $\endgroup$ Commented Nov 11, 2021 at 18:44
  • $\begingroup$ This is obvious because f[Dynamic[{{11, 11, 11.}, {12, 12, 12.}}]] is not the same as f[Dynamic[{11, 11, 11.}]] I added this case to my answer. $\endgroup$ Commented Nov 11, 2021 at 20:24
  • $\begingroup$ I amended the question to provide a more complete example. If what you say is true about using names with Repeated, then how come it works with the non-dynamic example but not with Dynamic. Also, how would I then be able to refer to the repeated argument in the function definition without a name? $\endgroup$ Commented Nov 12, 2021 at 11:13
  • $\begingroup$ You gave a name to the whole list and get elements by indexing. $\endgroup$ Commented Nov 12, 2021 at 11:48
  • $\begingroup$ I still don't understand. Please could you look at the amended question again and provide me a working example if possible. Thanks. $\endgroup$ Commented Nov 13, 2021 at 11:23

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.