Function Repository Resource:

AccumulateApply

Source Notebook

Apply a function to progressively longer sequences from a list

Contributed by: Ian Ford and Jon McLoone

ResourceFunction["AccumulateApply"][f,list]

applies f to progressively longer sequences from list.

Details

The list need not have a Head of List.

Examples

Basic Examples (2) 

Apply an arbitrary function to progressive ranges of numbers:

In[1]:=
ResourceFunction["AccumulateApply"][f, Range[4]]
Out[1]=

Find all substrings from the start of a message:

In[2]:=
ResourceFunction["AccumulateApply"][StringJoin, Characters["Message"]]
Out[2]=

Scope (1) 

The Head of the second argument need not be List:

In[3]:=
ResourceFunction["AccumulateApply"][f, g[a, b, c]]
Out[3]=

Applications (2) 

Generate a sequence of factorials:

In[4]:=
ResourceFunction["AccumulateApply"][Times, Range[9]]
Out[4]=
In[5]:=
Range[9]!
Out[5]=

Find the running maximum of a list:

In[6]:=
ResourceFunction[
 "AccumulateApply"][Max, {3, 5, 7, 8, 8, 5, 7, 9, 2, 5}]
Out[6]=

Properties and Relations (1) 

AccumulateApply[Plus,list] is equivalent to Accumulate[list]:

In[7]:=
ResourceFunction["AccumulateApply"][Plus, {1, 2, 3, 4}]
Out[7]=
In[8]:=
Accumulate[{1, 2, 3, 4}]
Out[8]=

Possible Issues (1) 

Functions that expect a list should be composed with List:

In[9]:=
ResourceFunction["AccumulateApply"][Length@*List, {a, b, c}]
Out[9]=

Publisher

Jon McLoone

Version History

  • 1.0.1 – 16 November 2021

License Information