# Wolfram Function Repository

Instant-use add-on functions for the Wolfram Language

Function Repository Resource:

Provide the part specifications to a mapped function as a sequence of arguments after the first one

Contributed by:
Seth J. Chandler

ResourceFunction["MapSlice"][ applies | |

ResourceFunction["MapSlice"][ applies | |

ResourceFunction["MapSlice"][ represents an operator form of ResourceFunction["MapSlice"] that can be applied to an expression at its first level. |

ResourceFunction["MapSlice"] is a variant of MapIndexed that gives the part specifications as a sequence of arguments rather than a single List argument.

ResourceFunction["MapSlice"] uses standard level specifications just like MapIndexed.

The default value for *levelspec* in ResourceFunction["MapSlice"] is {1}.

A positive level *n* consists of all parts of *expr* specified by *n* indices.

A negative level -*n* consists of all parts of *expr* with depth *n*.

Level –1 consists of numbers, symbols and other objects that do not have subparts.

Level 0 corresponds to the whole expression.

ResourceFunction["MapSlice"] traverses the parts of *expr* in a depth-first order, with leaves visited before roots.

ResourceFunction["MapSlice"] always effectively constructs a complete new expression and then evaluates it.

ResourceFunction["MapSlice"] works on SparseArray objects, effectively by applying Normal to them.

ResourceFunction["MapSlice"] works on Association objects, giving part specifications in the form Key[*k*].

ResourceFunction["MapSlice"][*f*][*expr*] is equivalent to ResourceFunction["MapSlice"][*f*,*expr*,{1}].

Apply *f* to a list with four parts:

In[1]:= |

Out[1]= |

Argument #2 gives the index of each part:

In[2]:= |

Out[2]= |

Changing the level specification affects the output:

In[3]:= |

Out[3]= |

Use the operator form of MapSlice:

In[4]:= |

Out[4]= |

Use MapSlice with an association:

In[5]:= |

Out[5]= |

Functions used inside MapSlice may contain slot values as high as one more than the level specification:

In[6]:= |

Out[6]= |

Map onto all the elements of an expression:

In[7]:= |

Out[7]= |

Map only onto the "leaves" of the expression:

In[8]:= |

Out[8]= |

Map on levels 0 through 1; the head has index {}:

In[9]:= |

Out[9]= |

MapSlice can be used on expressions with any head:

In[10]:= |

Out[10]= |

Label parts by position:

In[11]:= |

Out[11]= |

In[12]:= |

Out[12]= |

Rotate lists based on position:

In[13]:= |

Out[13]= |

Show where various elements of a three-dimensional array might be stored in memory:

In[14]:= |

Out[14]= |

There is no Heads option to MapSlice as there is for MapIndexed.

The following two pieces of code are equivalent, but the version with MapSlice is arguably cleaner because there is no need for a Part function within the first argument; this will often be the case for functions that map at level 1:

In[16]:= |

Out[16]= |

- 1.0.0 – 15 July 2019

This work is licensed under a Creative Commons Attribution 4.0 International License