Function Repository Resource:

TableQ

Source Notebook

Similar to ArrayQ, except it allows for ragged collections of nested lists

Contributed by: Richard Hennigan (Wolfram Research)

ResourceFunction["TableQ"][expr]

gives True if expr is a table, and gives False otherwise.

ResourceFunction["TableQ"][expr,patt]

requires expr to be a table with a depth that matches the pattern patt.

ResourceFunction["TableQ"][expr,patt,test]

requires also that test yield True when applied to each of the table elements in expr.

Details and Options

In a table, all parts at a particular level must not mix lists and non-lists.
ResourceFunction["TableQ"][expr,1] tests whether expr is a vector.
ResourceFunction["TableQ"][expr,_,NumberQ] tests whether expr is a numerical table at all levels.

Examples

Basic Examples (4) 

Test to see if an expression is a table:

In[1]:=
ResourceFunction["TableQ"][{1, 2, 3, 4}]
Out[1]=
In[2]:=
ResourceFunction["TableQ"][{1, 2, 3, {4}}]
Out[2]=
In[3]:=
ResourceFunction["TableQ"][{{1, 2, 3}, {4}}]
Out[3]=
In[4]:=
ResourceFunction["TableQ"][{{{1, 2}, 3, 4}, {5}}]
Out[4]=

A vector of numbers is a table:

In[5]:=
ResourceFunction["TableQ"][{1, 2, 3, 4}]
Out[5]=

At each level, lists and non-lists cannot be mixed:

In[6]:=
ResourceFunction["TableQ"][{1, 2, {3}, 4}]
Out[6]=
In[7]:=
ResourceFunction["TableQ"][{{{1, 2}, 3, 4}, {5}}]
Out[7]=

A ragged collection of nested lists is a table:

In[8]:=
ResourceFunction["TableQ"][{{1, 2}, {3}}]
Out[8]=

Scope (6) 

Test for a vector:

In[9]:=
ResourceFunction["TableQ"][{1, 2, 3, 4}, 1]
Out[9]=

Test for a matrix:

In[10]:=
ResourceFunction["TableQ"][{1, 2, 3, 4}, 2]
Out[10]=
In[11]:=
ResourceFunction["TableQ"][{{1, 2}, {3, 4}}, 2]
Out[11]=

Test for a ragged table at level 2:

In[12]:=
Table[i + j, {i, 5}, {j, i}]
Out[12]=
In[13]:=
ResourceFunction["TableQ"][%, 2]
Out[13]=

Test for a vector or a matrix:

In[14]:=
ResourceFunction["TableQ"][{1, 2, 3, 4}, 1 | 2]
Out[14]=

Test for a numeric vector:

In[15]:=
ResourceFunction["TableQ"][{1, 2, 3, x}, 1, NumericQ]
Out[15]=

Test for an array of any depth with numeric entries:

In[16]:=
ResourceFunction[
 "TableQ"][{{{E, 1}, {Pi, 2}}, {{Sin[1], Cos[2]}, {Sinh[1], Cosh[1]}}}, _, NumericQ]
Out[16]=

Applications (1) 

Define a function that only evaluates with tables of strings:

In[17]:=
stringFirsts[expr_ /; ResourceFunction["TableQ"][expr, _, StringQ]] := ResourceFunction["StringFunction"][First][expr];
In[18]:=
stringFirsts[{{"next", "example"}, {{"another"}, {"test"}}}]
Out[18]=
In[19]:=
stringFirsts[{{"next", "example"}, {{"another", 1}, {"test"}}}]
Out[19]=

Properties and Relations (5) 

VectorQ is a special case of TableQ:

In[20]:=
v = {1, 2., E, Pi + I};
In[21]:=
{VectorQ[v], ResourceFunction["TableQ"][v, 1]}
Out[21]=
In[22]:=
{VectorQ[v, NumericQ], ResourceFunction["TableQ"][v, 1, NumericQ]}
Out[22]=
In[23]:=
{VectorQ[v, Im[#] == 0 &], ResourceFunction["TableQ"][v, 1, Im[#] == 0 &]}
Out[23]=

ArrayQ implies TableQ:

In[24]:=
m1 = Array[f, {2, 3}]
Out[24]=
In[25]:=
{ArrayQ[m1], ResourceFunction["TableQ"][m1]}
Out[25]=
In[26]:=
{ArrayQ[m1, 2], ResourceFunction["TableQ"][m1, 2]}
Out[26]=
In[27]:=
{ArrayQ[m1, 2, MatchQ[_f]], ResourceFunction["TableQ"][m1, 2, MatchQ[_f]]}
Out[27]=

The converse is not necessarily true:

In[28]:=
m2 = Table[f[i, j], {i, 3}, {j, i}]
Out[28]=
In[29]:=
{ArrayQ[m2], ResourceFunction["TableQ"][m2]}
Out[29]=
In[30]:=
{ArrayQ[m2, 2], ResourceFunction["TableQ"][m2, 2]}
Out[30]=
In[31]:=
{ArrayQ[m2, 2, MatchQ[_f]], ResourceFunction["TableQ"][m2, 2, MatchQ[_f]]}
Out[31]=

TableQ[expr,_] is equivalent to ListQ[expr]:

In[32]:=
ResourceFunction["TableQ"][{1, {2, {3, {4}}}}, _]
Out[32]=
In[33]:=
ListQ[{1, {2, {3, {4}}}}]
Out[33]=
In[34]:=
ResourceFunction["TableQ"][f[1, f[2, f[3, f[4]]]], _]
Out[34]=
In[35]:=
ListQ[f[1, f[2, f[3, f[4]]]]]
Out[35]=

In TableQ[expr,_,test], the result will be True if test returns True for all elements of any level:

In[36]:=
ResourceFunction["TableQ"][{{1, 2}, {3, {4}}}, _, ListQ]
Out[36]=

This would normally return False due to mixing lists with non-lists:

In[37]:=
ResourceFunction["TableQ"][{{1, 2}, {3, {4}}}]
Out[37]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Version History

  • 1.0.0 – 11 March 2019

Related Resources

License Information