Skip to content
On this page

Understanding Mutable Keyword in Rational Notebooks

The mutable keyword in Rational notebooks is a powerful tool that allows you to directly modify the value of a cell from another cell. This guide will dive deep into the usage of mutable keyword, its effects on reactivity, and how it can be used effectively in your data products.

Mutable: Breaking the Unidirectional Data Flow

Rational notebooks are built around the concept of reactivity, where data flows from one cell to other dependent cells. However, there are times when you might want to break this unidirectional flow and update a cell directly from another cell. This is where the mutable keyword comes in.

Usage of Mutable

The mutable keyword allows you to declare a cell whose value can be changed directly from another cell. To declare a mutable cell, simply prepend the cell declaration with the mutable keyword.

javascript
mutable a = 1

In the example above, a is a mutable cell with an initial value of 1.

Modifying a Mutable Cell

To modify a mutable cell, you can use the mutable keyword again in another cell. This will change the value of the mutable cell and trigger re-evaluation of any cells that depend on it.

javascript
mutable a = 2

In this example, the value of a is changed to 2, and this change will propagate to any cells that depend on a.

Mutable and Reactivity

While the mutable keyword allows you to break the unidirectional data flow in a Rational notebook, it does not break the reactivity. When a mutable cell's value changes, the reactivity still holds: any cells that depend on the mutable cell will automatically update.

Automatic Updates with Mutable

Just like regular cells, changes in the value of a mutable cell will trigger automatic updates in any dependent cells. This allows you to create dynamic and interactive notebooks where changes in one part of the notebook can propagate to other parts.

javascript
b = a * 2

In this example, b is a cell that depends on the mutable cell a. If the value of a changes, b will automatically update.

Using Mutable for Interactive Programming

The mutable keyword can be a powerful tool for creating interactive programming experiences in Rational notebooks. By using mutable, you can create notebooks where the user's actions can directly affect the state of the notebook.

Creating User Interactions with Mutable

One of the most powerful uses of the mutable keyword is in creating user interactions. By combining mutable with Rational's built-in libraries for creating interactive views, you can create notebooks where the user's actions directly change the state of the notebook.

javascript
mutable selectedData = null

view = {
  // Code for creating an interactive view
  // When the user selects data in the view, update selectedData
  onUserSelect: (data) => {
    mutable selectedData = data
  }
}

In this example, selectedData is a mutable cell that holds the data selected by the user in the view. When the user selects data, the onUserSelect function updates selectedData, and this change will propagate to any cells that depend on selectedData.

Conclusion

The mutable keyword is a powerful feature in Rational notebooks that allows you to create interactive and dynamic data products. By understanding and using mutable, you can create notebooks that not only respond to changes in data, but also allow for direct user interaction and manipulation of the notebook's state.