Message Boards Message Boards

0
|
1718 Views
|
2 Replies
|
2 Total Likes
View groups...
Share
Share this post:

Specifying Pattern for Dataset argument

Hello all. I've been looking at the documentation on patterns and I have the following question. Say I have a Dataset with keys "a", "b" and "c". The "a" key is associated with an Integer, the "b" key is associated with a point which can be described by the pattern {Real,Real} and the "c" key is a String. I want to write a function F that takes such a Dataset as its only argument. I know that I could do the following:

A) F[arg_] Which would be the most general. More specifically, I can do:

B) F[arg_Dataset] which would accept any Dataset.

So, how can I specify that the argument a must a Dataset of the form described earlier.

Dataset[<| "a"->_Integer, "b"->{__Real,__Real} , "c"->_String |>]
POSTED BY: Henrick Jeanty
2 Replies

Hello Eric. Thank you for your suggestion. After thinking it over, I tend to agree with you that my isValidDatasetQ logic should be somewhere else rather than in checking the arguments during the function call.

I mark your suggestion as an answer. Again, thnak you.

POSTED BY: Henrick Jeanty
Posted 1 year ago

This is very general advice, so it might not pertain. A Dataset is often the result of some other processing, and that other processing should ensure that the Dataset is valid. So, I would actually suggest you look at putting your validation logic somewhere else.

Second, your example is a sort of "degenerate" Dataset. The typical Dataset has a list of associations. It would be much harder to validate that list-of-associations form with this kind of pattern matching, and it's a diminishing returns situation.

Furthermore, whatever queries you do on your Dataset will generate validation messages that will help you track down problems.

If you do really want to validate the Dataset itself (rather than put more effort into constructing a valid Dataset in the first place), then rather than pattern matching you might want to build your own MyDatasetValidQ function. It can map over all of the associations and check key names and value structure. It might leverage KeyExistsQ. Maybe it will use pattern matching at the value level, but that'll be easier than trying to do an all-in-one validation on the entire Dataset.

POSTED BY: Eric Rimbey
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