Is it possible to restrict the arguments for a function to a specific entity type?












9












$begingroup$


I'm trying to wrap my head around how to work with Mathematica for building (computable) domain specific knowledge. One particular thing I didn't quite understand is how to create symbols for operations which are to be applied (computed) only for specific entity-types. I understand how I can restrict the pattern-matching to basic datatypes (i.e using x_Integer as parameter to restrict input to only integers for example). But all entities share the exact same head (Entity), so how do I make the pattern understand which specific entity-type I want to match on?



ex:



f[x_{Planet entity}]:=y


which I'd like to evaluate to y if, for example, x="Entity["Planet", "Mercury"]", but not if Entity["Country", "Sweden"]










share|improve this question









$endgroup$












  • $begingroup$
    I'm not sure if there is a specific function that'll check for entity-types, but maybe you can just use PatternTest or Condition to check if it's the 'correct' type of entity. Something like f[x_?MemberQ[EntityList["Planet"],#]&]?
    $endgroup$
    – void life
    Jan 13 at 13:47






  • 3




    $begingroup$
    x : HoldPattern[Entity["Planet", __]]?
    $endgroup$
    – Kuba
    Jan 13 at 14:02
















9












$begingroup$


I'm trying to wrap my head around how to work with Mathematica for building (computable) domain specific knowledge. One particular thing I didn't quite understand is how to create symbols for operations which are to be applied (computed) only for specific entity-types. I understand how I can restrict the pattern-matching to basic datatypes (i.e using x_Integer as parameter to restrict input to only integers for example). But all entities share the exact same head (Entity), so how do I make the pattern understand which specific entity-type I want to match on?



ex:



f[x_{Planet entity}]:=y


which I'd like to evaluate to y if, for example, x="Entity["Planet", "Mercury"]", but not if Entity["Country", "Sweden"]










share|improve this question









$endgroup$












  • $begingroup$
    I'm not sure if there is a specific function that'll check for entity-types, but maybe you can just use PatternTest or Condition to check if it's the 'correct' type of entity. Something like f[x_?MemberQ[EntityList["Planet"],#]&]?
    $endgroup$
    – void life
    Jan 13 at 13:47






  • 3




    $begingroup$
    x : HoldPattern[Entity["Planet", __]]?
    $endgroup$
    – Kuba
    Jan 13 at 14:02














9












9








9


1



$begingroup$


I'm trying to wrap my head around how to work with Mathematica for building (computable) domain specific knowledge. One particular thing I didn't quite understand is how to create symbols for operations which are to be applied (computed) only for specific entity-types. I understand how I can restrict the pattern-matching to basic datatypes (i.e using x_Integer as parameter to restrict input to only integers for example). But all entities share the exact same head (Entity), so how do I make the pattern understand which specific entity-type I want to match on?



ex:



f[x_{Planet entity}]:=y


which I'd like to evaluate to y if, for example, x="Entity["Planet", "Mercury"]", but not if Entity["Country", "Sweden"]










share|improve this question









$endgroup$




I'm trying to wrap my head around how to work with Mathematica for building (computable) domain specific knowledge. One particular thing I didn't quite understand is how to create symbols for operations which are to be applied (computed) only for specific entity-types. I understand how I can restrict the pattern-matching to basic datatypes (i.e using x_Integer as parameter to restrict input to only integers for example). But all entities share the exact same head (Entity), so how do I make the pattern understand which specific entity-type I want to match on?



ex:



f[x_{Planet entity}]:=y


which I'd like to evaluate to y if, for example, x="Entity["Planet", "Mercury"]", but not if Entity["Country", "Sweden"]







function-construction entity data-types






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 13 at 13:31









WhilWhil

483




483












  • $begingroup$
    I'm not sure if there is a specific function that'll check for entity-types, but maybe you can just use PatternTest or Condition to check if it's the 'correct' type of entity. Something like f[x_?MemberQ[EntityList["Planet"],#]&]?
    $endgroup$
    – void life
    Jan 13 at 13:47






  • 3




    $begingroup$
    x : HoldPattern[Entity["Planet", __]]?
    $endgroup$
    – Kuba
    Jan 13 at 14:02


















  • $begingroup$
    I'm not sure if there is a specific function that'll check for entity-types, but maybe you can just use PatternTest or Condition to check if it's the 'correct' type of entity. Something like f[x_?MemberQ[EntityList["Planet"],#]&]?
    $endgroup$
    – void life
    Jan 13 at 13:47






  • 3




    $begingroup$
    x : HoldPattern[Entity["Planet", __]]?
    $endgroup$
    – Kuba
    Jan 13 at 14:02
















$begingroup$
I'm not sure if there is a specific function that'll check for entity-types, but maybe you can just use PatternTest or Condition to check if it's the 'correct' type of entity. Something like f[x_?MemberQ[EntityList["Planet"],#]&]?
$endgroup$
– void life
Jan 13 at 13:47




$begingroup$
I'm not sure if there is a specific function that'll check for entity-types, but maybe you can just use PatternTest or Condition to check if it's the 'correct' type of entity. Something like f[x_?MemberQ[EntityList["Planet"],#]&]?
$endgroup$
– void life
Jan 13 at 13:47




3




3




$begingroup$
x : HoldPattern[Entity["Planet", __]]?
$endgroup$
– Kuba
Jan 13 at 14:02




$begingroup$
x : HoldPattern[Entity["Planet", __]]?
$endgroup$
– Kuba
Jan 13 at 14:02










1 Answer
1






active

oldest

votes


















11












$begingroup$

As pointed out by Kuba in comments, when we make a function definition the pattern variables are not restricted to be simple x_head. So you can create a definition like



f[ent:HoldPattern[Entity["Planet", planetName_]]] := <do something>


The HoldPattern is only needed if you want to make the definition but avoid evaluation or package autoloading. Since Entity[ "Planet", planetName_] doesn't actually evaluate, here it is used to avoid autoloading the entity framework when making the definition.



The above definition would fire for even invalid planet entities - it would work with Entity["Planet", "Bob"] for example. You could go further with your definition, and restrict it to only valid entities, if only there were a ValidEntityQ function. Evaluating ?EntityFramework`* we see there is a function EntityFramework`EntityExistsQ that seems like what we want. It is an undocumented function, so it could stop working at some point in the future, but it seems to be present in every version of the entity framework I have available to me now and it isn't a Private` function so I would think it relatively safe to use.



So you could make your definition like



f[ent:HoldPattern[Entity["Planet", planetName_]]] /; 
EntityFramework`EntityExistsQ[ent] := <do something>

f[ent___] := $Failed


to make it only fire for valid planet entities.






share|improve this answer











$endgroup$













  • $begingroup$
    But there is a Planet Bob: cybernations.wikia.com/wiki/Planet_Bob :)
    $endgroup$
    – Michael E2
    Jan 13 at 16:01










  • $begingroup$
    Nice, one follow-up question only; are there any performance considerations regarding this pattern?
    $endgroup$
    – Whil
    Jan 13 at 18:31






  • 1




    $begingroup$
    @Whil These guards are usually fairly safe to use and the performance seems fine. I was about to write an answer in line of the one above but it is already perfect :)
    $endgroup$
    – Konstantin Konstantinov
    Jan 13 at 22:18











Your Answer





StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
});
});
}, "mathjax-editing");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "387"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f189409%2fis-it-possible-to-restrict-the-arguments-for-a-function-to-a-specific-entity-typ%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









11












$begingroup$

As pointed out by Kuba in comments, when we make a function definition the pattern variables are not restricted to be simple x_head. So you can create a definition like



f[ent:HoldPattern[Entity["Planet", planetName_]]] := <do something>


The HoldPattern is only needed if you want to make the definition but avoid evaluation or package autoloading. Since Entity[ "Planet", planetName_] doesn't actually evaluate, here it is used to avoid autoloading the entity framework when making the definition.



The above definition would fire for even invalid planet entities - it would work with Entity["Planet", "Bob"] for example. You could go further with your definition, and restrict it to only valid entities, if only there were a ValidEntityQ function. Evaluating ?EntityFramework`* we see there is a function EntityFramework`EntityExistsQ that seems like what we want. It is an undocumented function, so it could stop working at some point in the future, but it seems to be present in every version of the entity framework I have available to me now and it isn't a Private` function so I would think it relatively safe to use.



So you could make your definition like



f[ent:HoldPattern[Entity["Planet", planetName_]]] /; 
EntityFramework`EntityExistsQ[ent] := <do something>

f[ent___] := $Failed


to make it only fire for valid planet entities.






share|improve this answer











$endgroup$













  • $begingroup$
    But there is a Planet Bob: cybernations.wikia.com/wiki/Planet_Bob :)
    $endgroup$
    – Michael E2
    Jan 13 at 16:01










  • $begingroup$
    Nice, one follow-up question only; are there any performance considerations regarding this pattern?
    $endgroup$
    – Whil
    Jan 13 at 18:31






  • 1




    $begingroup$
    @Whil These guards are usually fairly safe to use and the performance seems fine. I was about to write an answer in line of the one above but it is already perfect :)
    $endgroup$
    – Konstantin Konstantinov
    Jan 13 at 22:18
















11












$begingroup$

As pointed out by Kuba in comments, when we make a function definition the pattern variables are not restricted to be simple x_head. So you can create a definition like



f[ent:HoldPattern[Entity["Planet", planetName_]]] := <do something>


The HoldPattern is only needed if you want to make the definition but avoid evaluation or package autoloading. Since Entity[ "Planet", planetName_] doesn't actually evaluate, here it is used to avoid autoloading the entity framework when making the definition.



The above definition would fire for even invalid planet entities - it would work with Entity["Planet", "Bob"] for example. You could go further with your definition, and restrict it to only valid entities, if only there were a ValidEntityQ function. Evaluating ?EntityFramework`* we see there is a function EntityFramework`EntityExistsQ that seems like what we want. It is an undocumented function, so it could stop working at some point in the future, but it seems to be present in every version of the entity framework I have available to me now and it isn't a Private` function so I would think it relatively safe to use.



So you could make your definition like



f[ent:HoldPattern[Entity["Planet", planetName_]]] /; 
EntityFramework`EntityExistsQ[ent] := <do something>

f[ent___] := $Failed


to make it only fire for valid planet entities.






share|improve this answer











$endgroup$













  • $begingroup$
    But there is a Planet Bob: cybernations.wikia.com/wiki/Planet_Bob :)
    $endgroup$
    – Michael E2
    Jan 13 at 16:01










  • $begingroup$
    Nice, one follow-up question only; are there any performance considerations regarding this pattern?
    $endgroup$
    – Whil
    Jan 13 at 18:31






  • 1




    $begingroup$
    @Whil These guards are usually fairly safe to use and the performance seems fine. I was about to write an answer in line of the one above but it is already perfect :)
    $endgroup$
    – Konstantin Konstantinov
    Jan 13 at 22:18














11












11








11





$begingroup$

As pointed out by Kuba in comments, when we make a function definition the pattern variables are not restricted to be simple x_head. So you can create a definition like



f[ent:HoldPattern[Entity["Planet", planetName_]]] := <do something>


The HoldPattern is only needed if you want to make the definition but avoid evaluation or package autoloading. Since Entity[ "Planet", planetName_] doesn't actually evaluate, here it is used to avoid autoloading the entity framework when making the definition.



The above definition would fire for even invalid planet entities - it would work with Entity["Planet", "Bob"] for example. You could go further with your definition, and restrict it to only valid entities, if only there were a ValidEntityQ function. Evaluating ?EntityFramework`* we see there is a function EntityFramework`EntityExistsQ that seems like what we want. It is an undocumented function, so it could stop working at some point in the future, but it seems to be present in every version of the entity framework I have available to me now and it isn't a Private` function so I would think it relatively safe to use.



So you could make your definition like



f[ent:HoldPattern[Entity["Planet", planetName_]]] /; 
EntityFramework`EntityExistsQ[ent] := <do something>

f[ent___] := $Failed


to make it only fire for valid planet entities.






share|improve this answer











$endgroup$



As pointed out by Kuba in comments, when we make a function definition the pattern variables are not restricted to be simple x_head. So you can create a definition like



f[ent:HoldPattern[Entity["Planet", planetName_]]] := <do something>


The HoldPattern is only needed if you want to make the definition but avoid evaluation or package autoloading. Since Entity[ "Planet", planetName_] doesn't actually evaluate, here it is used to avoid autoloading the entity framework when making the definition.



The above definition would fire for even invalid planet entities - it would work with Entity["Planet", "Bob"] for example. You could go further with your definition, and restrict it to only valid entities, if only there were a ValidEntityQ function. Evaluating ?EntityFramework`* we see there is a function EntityFramework`EntityExistsQ that seems like what we want. It is an undocumented function, so it could stop working at some point in the future, but it seems to be present in every version of the entity framework I have available to me now and it isn't a Private` function so I would think it relatively safe to use.



So you could make your definition like



f[ent:HoldPattern[Entity["Planet", planetName_]]] /; 
EntityFramework`EntityExistsQ[ent] := <do something>

f[ent___] := $Failed


to make it only fire for valid planet entities.







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 13 at 14:42

























answered Jan 13 at 14:37









Jason B.Jason B.

48.2k388190




48.2k388190












  • $begingroup$
    But there is a Planet Bob: cybernations.wikia.com/wiki/Planet_Bob :)
    $endgroup$
    – Michael E2
    Jan 13 at 16:01










  • $begingroup$
    Nice, one follow-up question only; are there any performance considerations regarding this pattern?
    $endgroup$
    – Whil
    Jan 13 at 18:31






  • 1




    $begingroup$
    @Whil These guards are usually fairly safe to use and the performance seems fine. I was about to write an answer in line of the one above but it is already perfect :)
    $endgroup$
    – Konstantin Konstantinov
    Jan 13 at 22:18


















  • $begingroup$
    But there is a Planet Bob: cybernations.wikia.com/wiki/Planet_Bob :)
    $endgroup$
    – Michael E2
    Jan 13 at 16:01










  • $begingroup$
    Nice, one follow-up question only; are there any performance considerations regarding this pattern?
    $endgroup$
    – Whil
    Jan 13 at 18:31






  • 1




    $begingroup$
    @Whil These guards are usually fairly safe to use and the performance seems fine. I was about to write an answer in line of the one above but it is already perfect :)
    $endgroup$
    – Konstantin Konstantinov
    Jan 13 at 22:18
















$begingroup$
But there is a Planet Bob: cybernations.wikia.com/wiki/Planet_Bob :)
$endgroup$
– Michael E2
Jan 13 at 16:01




$begingroup$
But there is a Planet Bob: cybernations.wikia.com/wiki/Planet_Bob :)
$endgroup$
– Michael E2
Jan 13 at 16:01












$begingroup$
Nice, one follow-up question only; are there any performance considerations regarding this pattern?
$endgroup$
– Whil
Jan 13 at 18:31




$begingroup$
Nice, one follow-up question only; are there any performance considerations regarding this pattern?
$endgroup$
– Whil
Jan 13 at 18:31




1




1




$begingroup$
@Whil These guards are usually fairly safe to use and the performance seems fine. I was about to write an answer in line of the one above but it is already perfect :)
$endgroup$
– Konstantin Konstantinov
Jan 13 at 22:18




$begingroup$
@Whil These guards are usually fairly safe to use and the performance seems fine. I was about to write an answer in line of the one above but it is already perfect :)
$endgroup$
– Konstantin Konstantinov
Jan 13 at 22:18


















draft saved

draft discarded




















































Thanks for contributing an answer to Mathematica Stack Exchange!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


Use MathJax to format equations. MathJax reference.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f189409%2fis-it-possible-to-restrict-the-arguments-for-a-function-to-a-specific-entity-typ%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Probability when a professor distributes a quiz and homework assignment to a class of n students.

Aardman Animations

Are they similar matrix