Function Repository Resource:

A version of MapThread that allows for ragged arrays and for lists with unequal depth

Contributed by: Sander Huisman
 ResourceFunction["GeneralizedMapThread"][f,{{a1,a2,…},{b1,b2,…},…}] gives {f[a1,b1,…],f[a2,b2,…],…}. ResourceFunction["GeneralizedMapThread"][f,{expr1,expr2,…},n] applies f to the parts of the expri at level n. ResourceFunction["GeneralizedMapThread"][f] represents an operator form of ResourceFunction["GeneralizedMapThread"] that can be applied to an expression.

## Details and Options

If the lists do not have equal length, the smallest lists are made equal length by making copies using ConstantArray.

## Examples

### Basic Examples (3)

Have the second argument be a constant:

 In[1]:=
 Out[1]=

It works on expressions with unequal lengths ("ragged"):

 In[2]:=
 Out[2]=

Apply f to the corresponding values of associations, copying values where necessary:

 In[3]:=
 Out[3]=

### Scope (3)

Apply to a list of lists with varying depths and lengths:

 In[4]:=
 Out[4]=

The final results are ragged:

 In[5]:=
 Out[5]=

Apply at level 1 means that {a,b} from the final list is considered a single value:

 In[6]:=
 Out[6]=

Apply at level 2 means interpreting {a,b} as separate values, and copying all the other values:

 In[7]:=
 Out[7]=

 In[8]:=

Use the operator on some data:

 In[9]:=
 Out[9]=

### Applications (2)

Subtract the vector {a,b} from a list of vectors:

 In[10]:=
 Out[10]=

Apply a list of functions to a single argument without pure functions:

 In[11]:=
 Out[11]=

This can also be achieved using Through:

 In[12]:=
 Out[12]=

### Properties and Relations (3)

When the arrays are of equal length, MapThread and GeneralizedMapThread give the same result:

 In[13]:=
 Out[13]=

MapThread cannot handle lists with elements of unequal length ("ragged"):

 In[14]:=
 Out[14]=

 In[15]:=
 Out[15]=

Thread can handle arrays with different depths:

 In[16]:=
 Out[16]=

 In[17]:=
 Out[17]=

### Possible Issues (4)

Associations must have the same keys:

 In[18]:=
 Out[18]=

Associations and lists can not be mixed:

 In[19]:=
 Out[19]=

Values with another head, such as List or Association, or an incorrect length will get copied:

 In[20]:=
 Out[20]=

For the case of unequal length, the longest list is taken as the actual length. Here the last list is the longest; the first list is copied:

 In[21]:=
 Out[21]=

Here the first list is the longest; the last list is copied:

 In[22]:=
 Out[22]=

An extra bracket around the second list will make it the shortest:

 In[23]:=
 Out[23]=

One can also give it a different head, which prevent it from being interpreted as a list:

 In[24]:=
 Out[24]=

### Neat Examples (2)

Interpret the entries as three levels deep, copying them all:

 In[25]:=
 Out[25]=

Create a complex ragged array with different depths:

 In[26]:=
 Out[26]=

Apply the function f five levels deep, with the second argument set constant to a:

 In[27]:=
 Out[27]=

SHuisman

## Version History

• 1.0.0 – 02 August 2019