Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
Functionally compose a Boolean expression
ResourceFunction["BooleanCompose"][expr] represents a composition of the variables in the Boolean expression expr, where each variable is a function. |
Create a predicate that returns True if its argument is both positive and an even number:
In[1]:= |
Out[1]= |
In[2]:= |
Out[2]= |
In[3]:= |
Out[3]= |
In[4]:= |
Out[4]= |
Pure functions can be used in the composition:
In[5]:= |
Out[5]= |
In[6]:= |
Out[6]= |
The composition can be applied to multiple arguments:
In[7]:= |
Out[7]= |
In[8]:= |
Out[8]= |
In[9]:= |
Out[9]= |
In[10]:= |
Out[10]= |
Multiple identical Boolean variables are only applied once each:
In[11]:= |
In[12]:= |
Out[13]= |
In[14]:= |
Out[14]= |
If any of the Boolean variables are not functions, applying the composition may yield a symbolic result:
In[15]:= |
Out[15]= |
However, if the Boolean expression can be short-circuited, a Boolean value will be returned:
In[16]:= |
Out[16]= |
All of the basic Boolean operators are supported:
In[17]:= |
Out[17]= |
In[18]:= |
Out[18]= |
BooleanFunction is supported:
In[19]:= |
Out[19]= |
In[20]:= |
Out[20]= |
Improve code readability by using functional-style composition:
In[21]:= |
In[22]:= |
Out[22]= |
Compare to a more conventional pure function-based approach:
In[23]:= |
Out[23]= |
While slightly more verbose, some may prefer the literacy and reduced duplication of the compositional style.
Every unique function in the Boolean expression will be applied to the argument(s) that the composition is applied to, even if the expression can be short-circuited:
In[24]:= |
In[25]:= |
Out[26]= |
In[27]:= |
Out[27]= |
The exception to this is the case where the expression is automatically simplified by the parser due to presence of a raw Boolean value:
In[28]:= |
Out[29]= |
In[30]:= |
Out[30]= |
This work is licensed under a Creative Commons Attribution 4.0 International License