array

class guppylang.std.builtins.array(*args: _T)[source]

Sequence of homogeneous values with statically known fixed length.

__getitem__(idx: int) L[source]

x.__getitem__(y) <==> x[y]

__iter__() SizedIter[ArrayIter[L, n], n][source]

Implement iter(self).

__len__() int[source]

Return len(self).

static __new__(cls, *args: _T) list[_T][source]
__new_guppy__()
__setitem__(idx: int, value: L @ owned) None[source]

Set self[key] to value.

copy() array[T, n][source]

Copy an array instance. Will only work if T is a copyable type.

is_borrowed(idx: int) bool[source]

Checks if an element has been taken out of the array.

This is the case whenever a non-copyable element is borrowed, or when an element is manually taken out of the array via the take method.

# Example

` qs = array(qubit() for _ in range(10)) h(qs[3]) result("a", qs.is_borrowed(3))  # False q = qs.take(3).unwrap() result("a", qs.is_borrowed(3))  # True qs.put(qubit(), 3).unwrap() result("a", qs.is_borrowed(3))  # False `

put(elem: L @ owned, idx: int) None[source]

Puts an element back into the array if it has been taken out previously.

This is the complement of array.take. It may be used to fill the “hole” left by array.take with a new element.

Panics if the provided index is negative or out of bounds, or if there is already an element at the given index.

Also see array.try_put for a version of this function that does not panic if if there is already an element at the given index.

# Example

` qs = array(qubit() for _ in range(10)) q = qubit() # qs.put(q, 3)  # Would panic as there is already a qubit at index 3 measure(qs.take(3))  # Take it out to make space for the new one qs.put(q, 3) h(qs[3]) `

take(idx: int) L[source]

Takes an element out of the array.

While regular indexing into an array only allows borrowing of elements, take actually extracts the element and transfers ownership to the caller. This makes this operation inherently unsafe: elements may no longer be accessed after they are taken out. Attempting to do so will result in a runtime panic.

The complementary array.put method may be used to return an element back into the array to make it accessible again.

Panics if the provided index is negative or out of bounds, or if the element has already been taken out.

Also see array.try_take for a version of this function that does not panic if the element has already been taken out.

# Example

` qs = array(qubit() for _ in range(10)) h(qs[3]) q = qs.take(3) measure(q)  # We're allowed to deallocate since we own `q` # h(qs[3])  # Would panic since qubit 3 has been taken out qs.put(qubit(), 3) # Put a fresh qubit back into the array h(qs[3]) `

try_put(elem: L @ owned, idx: int) Result[None, L][source]

Tries to put an element back into the array if it has been taken out previously.

This is the complement of array.take. It may be used to fill the “hole” left by array.take with a new element.

If there is already an element at the given index, then the array will not be mutated and the passed replacement element will be returned back in an err variant. Panics if the provided index is negative or out of bounds.

# Example

` qs = array(qubit() for _ in range(10)) q = qubit() qs.try_put(q, 3).unwrap_nothing()  # Is `nothing` since there's a qubit at idx 3 measure(qs.take(3))  # Take it out to make space for the new one qs.try_put(q, 3).unwrap() h(qs[3]) `

try_take(idx: int) Option[L][source]

Tries to take an element out of the array.

While regular indexing into an array only allows borrowing of elements, take actually extracts the element and transfers ownership to the caller. This makes this operation inherently unsafe: elements may no longer be accessed after they are taken out. Attempting to do so will result in a runtime panic.

The complementary array.put method may be used to return an element back into the array to make it accessible again.

Returns the extracted element or nothing if the element has already been taken out. Panics if the provided index is negative or out of bounds.

# Example

` qs = array(qubit() for _ in range(10)) h(qs[3]) q = qs.try_take(3).unwrap() measure(q)  # We're allowed to deallocate since we own `q` # h(qs[3])  # Would panic since qubit 3 has been taken out qs.put(qubit(), 3)  # Put a fresh qubit back into the array h(qs[3]) `