Function Repository Resource:

DifferencesBy

Apply a function to neighboring pairs in a list

Contributed by: Bob Sandheinrich
 ResourceFunction["DifferencesBy"][list,f] applies f to successive neighboring pairs of element in list. ResourceFunction["DifferencesBy"][f] represents an operator form of ResourceFunction["DifferencesBy"].

Details and Options

The result of ResourceFunction["DifferencesBy"][{e1,e2,e3,},f] is {f[e2,e1],f[e3,e2],}.
The output of ResourceFunction["DifferencesBy"] contains one fewer element than list.

Examples

Basic Examples

Find differences between images:

 In[1]:=
 Out[1]=
 In[2]:=
 Out[2]=

Find the elements of each sublist that are absent from the proceeding sublist:

 In[3]:=
 Out[3]=

Scope

Create an operator:

 In[4]:=
 Out[4]=
 In[5]:=
 Out[5]=

Applications

Find vocabulary differences:

 In[6]:=
 Out[6]=

Look at the growth of the string-based SubstitutionSystem:

 In[7]:=
 Out[7]=
 In[8]:=
 Out[8]=

Properties and Relations

DifferencesBy is a generalization of Differences:

 In[9]:=
 Out[9]=

Using Subtract as f gives the same result:

 In[10]:=
 Out[10]=

The function passed to DifferencesBy does not need to be subtraction:

 In[11]:=
 Out[11]=
 In[12]:=
 Out[12]=

Author Notes

This could be extended to allow the Part-like arguments in Differences.