Function Repository Resource:

InterpretedTrueQ

Source Notebook

Determine if a value should be interpreted as True

Contributed by: Bob Sandheinrich

ResourceFunction["InterpretedTrueQ"][expr]

gives True if expr should be interpreted as True.

ResourceFunction["InterpretedTrueQ"][list]

only gives True if all elements of list are interpreted as True.

ResourceFunction["InterpretedTrueQ"][assoc,key]

checks whether the value of key in assoc is interpreted as True.

Details and Options

ResourceFunction["InterpretedTrueQ"] is helpful when handling raw data from non–Wolfram Language environments.

Examples

Basic Examples (2) 

Check a string value:

In[1]:=
ResourceFunction["InterpretedTrueQ"]["true"]
Out[1]=

Check several values:

In[2]:=
AssociationMap[ResourceFunction[
 "InterpretedTrueQ"], {1, 0, "Yes", True, "Grapefruit"}]
Out[2]=

Scope (3) 

Check a value from an Association:

In[3]:=
ResourceFunction["InterpretedTrueQ"][<|"bool" -> "true"|>, "bool"]
Out[3]=

Use InterpretedTrueQ along with HTTPRequestData within a deployed APIFunction:

In[4]:=
url = First@
  CloudDeploy[
   APIFunction[{}, ResourceFunction["InterpretedTrueQ"][
      HTTPRequestData["Parameters"], "test"] &], Permissions -> "Public"]
Out[4]=

This works like a “Boolean” Interpreter when the parameter is given in the specified part of the request:

In[5]:=
URLExecute@URLBuild[url, {"test" -> "yes"}]
Out[5]=
In[6]:=
URLExecute@URLBuild[url, {"test" -> "potato"}]
Out[6]=
In[7]:=
URLExecute@URLBuild[url, {}]
Out[7]=

Check a value in an http header:

In[8]:=
url = First@
  CloudDeploy[
   APIFunction[{}, ResourceFunction["InterpretedTrueQ"][HTTPRequestData["Headers"], "encrypt-response"] &], Permissions -> "Public"]
Out[8]=
In[9]:=
URLExecute@
 HTTPRequest[url, <|"Headers" -> <|"encrypt-response" -> "1"|>|>]
Out[9]=
In[10]:=
URLExecute@
 HTTPRequest[
  url, <|"Headers" -> <|"encrypt-response" -> "Sorry, I lost my keys"|>|>]
Out[10]=

Properties and Relations (1) 

Missing keys in an Association give False:

In[11]:=
ResourceFunction[
 "InterpretedTrueQ"][<|"bool" -> "true"|>, "missingkey"]
Out[11]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Version History

  • 1.0.0 – 17 April 2019

License Information