Function Repository Resource:

WhenNot

Source Notebook

The opposite of When

Contributed by: Taliesin Beynon

ResourceFunction["WhenNot"][test1,value1,test2,value2,][e]

evaluates each of the testi[e] in turn, returning the value of the valuei corresponding to the first test that does not yield True; otherwise, it yields e unchanged.

Details and Options

ResourceFunction["WhenNot"] must have at least one test and value.
ResourceFunction["WhenNot"] has attribute HoldRest.

Examples

Basic Examples (1) 

WhenNot evaluates the corresponding value only when the specified condition is not met:

In[1]:=
ResourceFunction["WhenNot"][# < 3 &, Print["not small"]][5]
In[2]:=
ResourceFunction["WhenNot"][# < 3 &, Print["not small"]][1]
Out[2]=

Scope (2) 

Use a single test with WhenNot:

In[3]:=
ResourceFunction["WhenNot"][IntegerQ, "not integer"][1.]
Out[3]=
In[4]:=
ResourceFunction["WhenNot"][IntegerQ, "not integer"][1]
Out[4]=

Try several tests in turn:

In[5]:=
ResourceFunction["WhenNot"][# == 1 &, "not one", # == 2 &, "not two"][1]
Out[5]=

Properties and Relations (1) 

When executes its body and returns the corresponding value if the test yields True:

In[6]:=
When[# == 1 &, x][1]
Out[6]=
In[7]:=
ResourceFunction["WhenNot"][# == 1 &, x][1]
Out[7]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Version History

  • 1.0.0 – 16 January 2019

Related Resources

License Information