Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Pass a value to a function if the value satisfies a condition
ResourceFunction["IfSatisfies"][expr,condition,f,o] applies f to expr if condition[expr] evaluates to True; otherwise, applies o to expr. |
The condition, as well as f and o, can be the name of a function:
In[2]:= | ![]() |
Out[2]= | ![]() |
In[3]:= | ![]() |
Out[3]= | ![]() |
Or they can be pure functions, or a mixture of both names of functions and pure functions:
In[4]:= | ![]() |
Out[4]= | ![]() |
Symbolic expressions are allowed. This example applies f to the expression if it has two items; otherwise, it applies g:
In[5]:= | ![]() |
Out[5]= | ![]() |
If a word has conjugations or forms other than itself, return them; otherwise, return Missing[]:
In[6]:= | ![]() |
In[7]:= | ![]() |
Out[7]= | ![]() |
If the condition evaluates to something other than True or False, the "otherwise" function (the last argument) is evaluated. Here, the condition returns an image of a hot dog:
In[8]:= | ![]() |
Out[8]= | ![]() |
All arguments to this function except the first are required to be functions. Therefore, the following will not work:
In[9]:= | ![]() |
Out[9]= | ![]() |
Rather, make the arguments into pure functions like so:
In[10]:= | ![]() |
Out[10]= | ![]() |
This work is licensed under a Creative Commons Attribution 4.0 International License