Wolfram Function Repository
Instant-use add-on functions for the Wolfram Language
Function Repository Resource:
A version of MapThread that allows for ragged arrays and for lists with unequal depth
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. |
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]= |
![]() |
Apply to a list of lists with varying depths and lengths:
In[4]:= |
![]() |
Out[5]= |
![]() |
The final results are ragged:
In[6]:= |
![]() |
Out[6]= |
![]() |
Apply at level 1 means that {a,b} from the final list is considered a single value:
In[7]:= |
![]() |
Out[7]= |
![]() |
Apply at level 2 means interpreting {a,b} as separate values, and copying all the other values:
In[8]:= |
![]() |
Out[8]= |
![]() |
Create an operator of GeneralizedMapThread:
In[9]:= |
![]() |
Use the operator on some data:
In[10]:= |
![]() |
Out[10]= |
![]() |
Subtract the vector {a,b} from a list of vectors:
In[11]:= |
![]() |
Out[11]= |
![]() |
Apply a list of functions to a single argument without pure functions:
In[12]:= |
![]() |
Out[12]= |
![]() |
This can also be achieved using Through:
In[13]:= |
![]() |
Out[13]= |
![]() |
When the arrays are of equal length, MapThread and GeneralizedMapThread give the same result:
In[14]:= |
![]() |
Out[14]= |
![]() |
MapThread cannot handle lists with elements of unequal length (“ragged”):
In[15]:= |
![]() |
Out[15]= |
![]() |
GeneralizedMapThread can handle these cases:
In[16]:= |
![]() |
Out[16]= |
![]() |
Thread can handle arrays with different depths:
In[17]:= |
![]() |
Out[17]= |
![]() |
GeneralizedMapThread can also do this, but the syntax of GeneralizedMapThread follows the syntax of MapThread:
In[18]:= |
![]() |
Out[18]= |
![]() |
Associations must have the same keys:
In[19]:= |
![]() |
Out[19]= |
![]() |
Associations and lists cannot be mixed:
In[20]:= |
![]() |
Out[20]= |
![]() |
Values with another head, such as List or Association, or an incorrect length will get copied:
In[21]:= |
![]() |
Out[21]= |
![]() |
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[22]:= |
![]() |
Out[22]= |
![]() |
Here the first list is the longest; the last list is copied:
In[23]:= |
![]() |
Out[23]= |
![]() |
An extra bracket around the second list will make it the shortest:
In[24]:= |
![]() |
Out[24]= |
![]() |
One can also give it a different head, which prevents it from being interpreted as a list:
In[25]:= |
![]() |
Out[25]= |
![]() |
Interpret the entries as three levels deep, copying them all:
In[26]:= |
![]() |
Out[26]= |
![]() |
Create a complex ragged array with different depths:
In[27]:= |
![]() |
Out[28]= |
![]() |
Apply the function f five levels deep, with the second argument set constant to a:
In[29]:= |
![]() |
Out[29]= |
![]() |
This work is licensed under a Creative Commons Attribution 4.0 International License