Function Repository Resource:

ExpressionBag

Source Notebook

A data structure meant for accumulating items efficiently

Contributed by: Richard Hennigan (Wolfram Research)

ResourceFunction["ExpressionBag"]["id"]

retrieves the expression bag specified by "id".

ResourceFunction["ExpressionBag"][list]

creates an expression bag using list as the initial data.

ResourceFunction["ExpressionBag"][][prop]

returns the specified property of the bag.

ResourceFunction["ExpressionBag"][]["Add",expr]

adds expr to the bag.

ResourceFunction["ExpressionBag"][]["Part",part]

gets the expression specified by part from the bag.

Details and Options

In ResourceFunction["ExpressionBag"]["id"], the "id" can be the name or UUID of an expression bag.
The following operations on an expression bag are supported:
Length[bag]the number of items contained in the bag
Append[bag,expr]adds expr to the bag
bag[[part]]gets items from the bag specified by Part specification part
Get[bag]gets all items from the bag
Information[bag]returns a summary of information about the bag
DeleteObject[bag]permanently delete bag
ResourceFunction["ExpressionBag"] is a single interface to the internal functions Internal`Bag, Internal`StuffBag, Internal`BagLength and Internal`BagPart.
Using ResourceFunction["ExpressionBag"][]["Add",expr] is a more efficient option for situations where one might consider using constructs such as AppendTo[list,expr] as a way of accumulating data.

Examples

Basic Examples (3) 

Create an empty expression bag:

In[1]:=
bag = ResourceFunction["ExpressionBag"][]
Out[1]=

Add values to the expression bag:

In[2]:=
Append[bag, 1234]
Out[2]=

Add another value:

In[3]:=
Append[bag, 5678]
Out[3]=

Retrieve all values from the bag:

In[4]:=
Get[bag]
Out[4]=

Create an expression bag from some initial data:

In[5]:=
bag = ResourceFunction[
  "ExpressionBag"][{"this", "that", "another thing"}]
Out[5]=

Retrieve parts of the expression bag:

In[6]:=
bag[[3]]
Out[6]=
In[7]:=
bag[[1 ;; 2]]
Out[7]=

Create a named expression bag:

In[8]:=
ResourceFunction["ExpressionBag"][Range[10], "MyBagOfStuff"]
Out[8]=

Reference the bag by name:

In[9]:=
Get[ResourceFunction["ExpressionBag"]["MyBagOfStuff"]]
Out[9]=

Scope (3) 

Get information about an expression bag:

In[10]:=
bag = ResourceFunction["ExpressionBag"][Range[100], "Some Integers"]
Out[10]=
In[11]:=
Information[bag]
Out[11]=

Delete an expression bag:

In[12]:=
bag = ResourceFunction["ExpressionBag"][{1, 2, 3}, "Delete Me"]
Out[12]=
In[13]:=
DeleteObject[bag]

The expression bag can no longer be referenced by name:

In[14]:=
ResourceFunction["ExpressionBag"]["Delete Me"]
Out[14]=

Get all expression bags in the current session:

In[15]:=
ResourceFunction["ExpressionBag"][All]
Out[15]=

Delete them:

In[16]:=
DeleteObject /@ ResourceFunction["ExpressionBag"][All]
Out[16]=

Applications (5) 

Keep a running history of inputs in the current session:

In[17]:=
inputs = ResourceFunction["ExpressionBag"][{}, "Inputs"]
Out[17]=
In[18]:=
$PreRead = (inputs["Add", #]; #) &
Out[18]=

Evaluate some inputs:

In[19]:=
1 + 1
Out[19]=
In[20]:=
Table[n^2, {n, 10}]
Out[20]=
In[21]:=
Reverse[%]
Out[21]=

Get the list of inputs entered so far:

In[22]:=
Get[inputs]
Out[22]=

Create a notebook showing input history:

In[23]:=
NotebookPut[
 Notebook[Cell[BoxData[#], "Input"] & /@ Get[inputs], WindowTitle -> "Input History"]]
Out[23]=

Restore $PreRead to its default:

In[24]:=
$PreRead =.

Properties and Relations (2) 

An expression bag can be used as a collection that allows efficient insertion of items:

In[25]:=
bag = ResourceFunction["ExpressionBag"][]
Out[25]=
In[26]:=
AbsoluteTiming[Do[bag["Add", i], {i, 100000}]]
Out[26]=

Compare to using AppendTo and a List:

In[27]:=
list = {};
AbsoluteTiming[Do[AppendTo[list, i], {i, 100000}]]
Out[28]=

The results are equivalent:

In[29]:=
Get[bag] === list
Out[29]=

The insertion time for an expression bag is not affected by the item count:

In[30]:=
bag = ResourceFunction["ExpressionBag"][]
Out[30]=
In[31]:=
bagTimings = Table[First[AbsoluteTiming[Do[bag["Add", i], {i, 20000}]]], 10]
Out[31]=

Compare to using AppendTo and List:

In[32]:=
list = {};
listTimings = Table[First[AbsoluteTiming[Do[AppendTo[list, i], {i, 20000}]]], 10]
Out[20]=
In[33]:=
ListLinePlot[{bagTimings, listTimings}, PlotLegends -> {"bag", "list"}]
Out[33]=

Requirements

Wolfram Language 11.3 (March 2018) or above

Version History

  • 1.0.0 – 19 February 2019

Related Resources

License Information