Collections

%reload_ext rubberize
from rubberize.config import config

Lists

Lists are rendered with Python syntax:

%%tap
[]
[1.23, 4.56, 7.89]
[[1, 2], [3, 4]]
\( \displaystyle \left[\right] \)
\( \displaystyle \left[ 1.23,\, 4.56,\, 7.89 \right] \)
\( \displaystyle \left[ \left[ 1,\, 2 \right],\, \left[ 3,\, 4 \right] \right] \)

Unpacking is rendered as a union:

%%tap
a = [1, 2]
[4, 5, *a]
\( \displaystyle a = \left[ 1,\, 2 \right] \)
\( \displaystyle \left[ 4,\, 5 \right] \cup a = \left[ 4,\, 5 \right] \cup \left[ 1,\, 2 \right] = \left[ 4,\, 5,\, 1,\, 2 \right] \)

Aligning Vertically

If the list is longer than @max_inline_elts, it will be arranged vertically for readability.

config.max_inline_elts
5
%%tap
[1, 2, 3, 4, 5, 6]
\( \displaystyle \left[ \begin{array}{c} 1 \\ 2 \\ 3 \\ 4 \\ 5 \\ 6 \end{array} \right] \)

As an Array

@show_list_as_array controls the rendering style:

config.show_list_as_array
False

If set to True, lists will be rendered to look like an array, if possible:

%%tap @show_list_as_array=True
[]
[1.23, 4.56, 7.89]
[[1, 2], [3, 4]]
\( \displaystyle \left[\right] \)
\( \displaystyle \left[ 1.23,\, 4.56,\, 7.89 \right] \)
\( \displaystyle \left[ \left[ 1,\, 2 \right],\, \left[ 3,\, 4 \right] \right] \)

1D list-arrays can be shown as a row vector by setting @show_1d_as_col=False

%%tap @show_list_as_array=True @show_1d_as_col=False
[1.23, 4.56, 7.89]
\( \displaystyle \left[ 1.23,\, 4.56,\, 7.89 \right] \)

@array_delimiter controls the appearance of delimiters used in a list-array. Use "pmatrix" for parentheses and "bmatrix" for square brackets.

config.array_delimiter
'bmatrix'
%%tap @show_list_as_array=True @array_delimiter="pmatrix"
[]
[1.23, 4.56, 7.89]
[[1, 2], [3, 4]]
\( \displaystyle \left[\right] \)
\( \displaystyle \left[ 1.23,\, 4.56,\, 7.89 \right] \)
\( \displaystyle \left[ \left[ 1,\, 2 \right],\, \left[ 3,\, 4 \right] \right] \)

List Element Access

List element access appears as a subscript:

%%tap
v = [1, 2, 3, 4]

v[0]   # Single element access
v[:2]  # Slice access
\( \displaystyle v = \left[ 1,\, 2,\, 3,\, 4 \right] \)
\( \displaystyle v_{\left( 0 \right)} = 1 \)
Single element access
\( \displaystyle v_{\left( : 2 \right)} = \left[ 1,\, 2 \right] \)
Slice access

Whether or not the subscript is enclosed in parentheses is controlled by @wrap_indices:

config.wrap_indices
True
%%tap @wrap_indices=False
v = [1, 2, 3, 4]

v[0]   # Single element access
v[:2]  # Slice access
\( \displaystyle v = \left[ 1,\, 2,\, 3,\, 4 \right] \)
\( \displaystyle v_{\left( 0 \right)} = 1 \)
Single element access
\( \displaystyle v_{\left( : 2 \right)} = \left[ 1,\, 2 \right] \)
Slice access

List Comprehensions

List comprehensions are rendered like so:

%%tap
a = [1, 2, 3, 4]
x = 3  # x is shadowed by the comprehension scope

[x**2 for x in a if x % 2 == 0]
\( \displaystyle a = \left[ 1,\, 2,\, 3,\, 4 \right] \)
\( \displaystyle x = 3 \)
x is shadowed by the comprehension scope
\( \displaystyle \left[\, x^{2} \;\middle|\; x \in a \land x \mathbin{\%} 2 = 0 \,\right] = \left[ 4,\, 16 \right] \)

Tuples

Tuples are rendered with Python syntax:

%%tap
()
(1.23, 4.56, 7.89)
((1, 2), (3, 4))
\( \displaystyle \left(\right) \)
\( \displaystyle \left( 1.23,\, 4.56,\, 7.89 \right) \)
\( \displaystyle \left( \left( 1,\, 2 \right),\, \left( 3,\, 4 \right) \right) \)

Unpacking is rendered as a union:

%%tap
a = (1, 2)
(4, 5, *a)
\( \displaystyle a = \left( 1,\, 2 \right) \)
\( \displaystyle \left( 4,\, 5 \right) \cup a = \left( 4,\, 5 \right) \cup \left( 1,\, 2 \right) = \left( 4,\, 5,\, 1,\, 2 \right) \)

Aligning Vertically

If the tuple is longer than @max_inline_elts, it will be arranged vertically for readability.

config.max_inline_elts
5
%%tap
(1, 2, 3, 4, 5, 6)
\( \displaystyle \left( \begin{array}{c} 1 \\ 2 \\ 3 \\ 4 \\ 5 \\ 6 \end{array} \right) \)

As an Array

@show_tuple_as_array controls the rendering style:

config.show_tuple_as_array
False

If set to True, tuples will be rendered to look like an array, if possible:

%%tap @show_tuple_as_array=True
()
(1.23, 4.56, 7.89)
((1, 2), (3, 4))
\( \displaystyle \left(\right) \)
\( \displaystyle \left( 1.23,\, 4.56,\, 7.89 \right) \)
\( \displaystyle \left( \left( 1,\, 2 \right),\, \left( 3,\, 4 \right) \right) \)

1D tuple-arrays can be shown as a row vector by setting @show_1d_as_col=False

%%tap @show_tuple_as_array=True @show_1d_as_col=False
(1.23, 4.56, 7.89)
\( \displaystyle \left( 1.23,\, 4.56,\, 7.89 \right) \)

@array_delimiter also controls the appearance of delimiters used in a tuple-array. Use "pmatrix" for parentheses and "bmatrix" for square brackets.

Tuple Element Access

Tuple element access appears as a subscript:

%%tap
v = (1, 2, 3, 4)

v[-1]   # Single element access
v[1:3]  # Slice access
\( \displaystyle v = \left( 1,\, 2,\, 3,\, 4 \right) \)
\( \displaystyle v_{\left( -1 \right)} = 4 \)
Single element access
\( \displaystyle v_{\left( 1 : 3 \right)} = \left( 2,\, 3 \right) \)
Slice access

Whether or not the subscript is enclosed in parentheses is controlled by @wrap_indices.

%%tap @wrap_indices=False
v = (1, 2, 3, 4)

v[-1]   # Single element access
v[1:3]  # Slice access
\( \displaystyle v = \left( 1,\, 2,\, 3,\, 4 \right) \)
\( \displaystyle v_{\left( -1 \right)} = 4 \)
Single element access
\( \displaystyle v_{\left( 1 : 3 \right)} = \left( 2,\, 3 \right) \)
Slice access

Sets

Sets are rendered with Python syntax:

%%tap
set()
{1.23, 4.56, 7.89}
\( \displaystyle \left\{\right\} \)
\( \displaystyle \left\{ 1.23,\, 4.56,\, 7.89 \right\} \)

Unpacking is rendered as a union:

%%tap
a = {1, 2}
{4, 5, *a}
\( \displaystyle a = \left\{ 1,\, 2 \right\} \)
\( \displaystyle \left\{ 4,\, 5 \right\} \cup a = \left\{ 4,\, 5 \right\} \cup \left\{ 1,\, 2 \right\} = \left\{ 1,\, 2,\, 4,\, 5 \right\} \)

Aligning Vertically

If the set is longer than @max_inline_elts, it will be arranged vertically for readability.

config.max_inline_elts
5
%%tap
{1, 2, 3, 4, 5, 6}
\( \displaystyle \left\{ \begin{array}{c} 1 \\ 2 \\ 3 \\ 4 \\ 5 \\ 6 \end{array} \right\} \)

Set Comprehensions

Set comprehensions are rendered like so:

%%tap
a = {1, 2, 3, 4}
x = 3  # x is shadowed by the comprehension scope

{x**2 for x in a if x % 2 == 0}
\( \displaystyle a = \left\{ 1,\, 2,\, 3,\, 4 \right\} \)
\( \displaystyle x = 3 \)
x is shadowed by the comprehension scope
\( \displaystyle \left\{\, x^{2} \;\middle|\; x \in a \land x \mathbin{\%} 2 = 0 \,\right\} = \left\{ 16,\, 4 \right\} \)

Dictionaries

Dictionary objects are rendered like so:

%%tap
{}
{"a": 1, "b": 2, "c": 3}
{1: 7.89, 2: 4.56, 3: 1.23}
\( \displaystyle \left\{\right\} \)
\( \displaystyle \left\{ \text{“a”} \to 1,\, \text{“b”} \to 2,\, \text{“c”} \to 3 \right\} \)
\( \displaystyle \left\{ 1 \to 7.89,\, 2 \to 4.56,\, 3 \to 1.23 \right\} \)

Unpacking is rendered as a union:

%%tap @stack
b = {"c": 3}
{"a": 1, "b": 2, **b}
\( \displaystyle b = \left\{ \text{“c”} \to 3 \right\} \)
\( \displaystyle \left\{ \text{“a”} \to 1,\, \text{“b”} \to 2 \right\} \cup b = \left\{ \text{“a”} \to 1,\, \text{“b”} \to 2 \right\} \cup \left\{ \text{“c”} \to 3 \right\} = \left\{ \text{“a”} \to 1,\, \text{“b”} \to 2,\, \text{“c”} \to 3 \right\} \)

Aligning Vertically

If the dictionary is longer than @max_inline_elts, it will be arranged vertically for readability.

config.max_inline_elts
5
%%tap
{"a": 1, "b": 2, "c": 3, "d": 4, "e": 5, "f": 6}
\( \displaystyle \left\{ \begin{aligned} \text{“a”} &\to 1 \\ \text{“b”} &\to 2 \\ \text{“c”} &\to 3 \\ \text{“d”} &\to 4 \\ \text{“e”} &\to 5 \\ \text{“f”} &\to 6 \end{aligned} \right\} \)

Dictionary Element Access

The dictionary element access appears as a subscript:

%%tap
dct = {"a": 1, "b": 2, "c": 3}
dct["a"]
\( \displaystyle \mathrm{dct} = \left\{ \text{“a”} \to 1,\, \text{“b”} \to 2,\, \text{“c”} \to 3 \right\} \)
\( \displaystyle \mathrm{dct}_{\left( \text{“a”} \right)} = 1 \)

Whether or not the subscript is enclosed in parentheses is controlled by @wrap_indices.

%%tap @wrap_indices=False
dct = {"a": 1, "b": 2, "c": 3}
dct["a"]
\( \displaystyle \mathrm{dct} = \left\{ \text{“a”} \to 1,\, \text{“b”} \to 2,\, \text{“c”} \to 3 \right\} \)
\( \displaystyle \mathrm{dct}_{\left( \text{“a”} \right)} = 1 \)

Dictionary Comprehensions

Dictionary comprehensions are rendered like so:

%%tap
a = [1, 2, 3, 4]
x = 3  # x is shadowed by the comprehension scope

{x: x**2 for x in a if x % 2 == 0} 
\( \displaystyle a = \left[ 1,\, 2,\, 3,\, 4 \right] \)
\( \displaystyle x = 3 \)
x is shadowed by the comprehension scope
\( \displaystyle \left\{\, x \to x^{2} \;\middle|\; x \in a \land x \mathbin{\%} 2 = 0 \,\right\} = \left\{ 2 \to 4,\, 4 \to 16 \right\} \)