Vec
Bases: PyoMutableSequence[T]
flowchart TD
pyochain._vec.Vec[Vec]
pyochain.abc._sequences.PyoMutableSequence[PyoMutableSequence]
pyochain.abc._sequences.PyoSequence[PyoSequence]
pyochain.abc._collection.PyoCollection[PyoCollection]
pyochain.abc._iterable.PyoIterable[PyoIterable]
pyochain.rs.Fluent[Fluent]
pyochain.rs.Pipe[Pipe]
pyochain.rs.Tap[Tap]
pyochain.rs.Checkable[Checkable]
pyochain.abc._collection.PyoContainer[PyoContainer]
pyochain.abc._collection.PyoSized[PyoSized]
pyochain.abc._sequences.PyoReversible[PyoReversible]
pyochain.abc._sequences.PyoMutableSequence --> pyochain._vec.Vec
pyochain.abc._sequences.PyoSequence --> pyochain.abc._sequences.PyoMutableSequence
pyochain.abc._collection.PyoCollection --> pyochain.abc._sequences.PyoSequence
pyochain.abc._iterable.PyoIterable --> pyochain.abc._collection.PyoCollection
pyochain.rs.Fluent --> pyochain.abc._iterable.PyoIterable
pyochain.rs.Pipe --> pyochain.rs.Fluent
pyochain.rs.Tap --> pyochain.rs.Fluent
pyochain.rs.Checkable --> pyochain.abc._iterable.PyoIterable
pyochain.abc._collection.PyoContainer --> pyochain.abc._collection.PyoCollection
pyochain.abc._collection.PyoSized --> pyochain.abc._collection.PyoCollection
pyochain.abc._sequences.PyoReversible --> pyochain.abc._sequences.PyoSequence
click pyochain._vec.Vec href "" "pyochain._vec.Vec"
click pyochain.abc._sequences.PyoMutableSequence href "" "pyochain.abc._sequences.PyoMutableSequence"
click pyochain.abc._sequences.PyoSequence href "" "pyochain.abc._sequences.PyoSequence"
click pyochain.abc._collection.PyoCollection href "" "pyochain.abc._collection.PyoCollection"
click pyochain.abc._iterable.PyoIterable href "" "pyochain.abc._iterable.PyoIterable"
click pyochain.rs.Fluent href "" "pyochain.rs.Fluent"
click pyochain.rs.Pipe href "" "pyochain.rs.Pipe"
click pyochain.rs.Tap href "" "pyochain.rs.Tap"
click pyochain.rs.Checkable href "" "pyochain.rs.Checkable"
click pyochain.abc._collection.PyoContainer href "" "pyochain.abc._collection.PyoContainer"
click pyochain.abc._collection.PyoSized href "" "pyochain.abc._collection.PyoSized"
click pyochain.abc._sequences.PyoReversible href "" "pyochain.abc._sequences.PyoReversible"
Represent a mutable sequence of elements.
Implement collections::abc::MutableSequence, and pyochain's PyoMutableSequence ABC.
Unlike Seq which is immutable, Vec allows in-place modification of elements.
As such, Vec is more suitable when you need to build up a collection incrementally, or when you need to perform many modifications on the collection.
On the other hand, Seq is more memory efficient when you have a fixed collection that doesn't require modification.
This is due to the fact that CPython don't have to allocate extra space to account for potential future modifications.
It uses a list as the underlying data structure, so it has the same performance characteristics regarding indexing, slicing, and iteration.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Iterable[T]
|
Any |
required |
Source code in src/pyochain/_vec.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 | |
inner
property
Get the underlying list data structure.
Useful when interoperating with functions that require a standard Python list.
Returns:
| Type | Description |
|---|---|
list[T]
|
list[T]: The underlying list. |
concat(other)
Concatenate another Vec or list to self and return a new Vec.
Note
This is equivalent to list_1 + list_2 for standard lists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
list[T] | Self
|
The other |
required |
Returns:
| Type | Description |
|---|---|
Vec[T]
|
Vec[T]: The new |
See Also
Vec::concat_mut which modifies self in place.
Example
>>> from pyochain import Vec
>>> v1 = Vec.from_ref([1, 2, 3])
>>> v2 = [4, 5, 6] # Can also concatenate a standard list
>>> v3 = v1.concat(v2)
>>> v3
Vec(1, 2, 3, 4, 5, 6)
>>> v1.clear() # Clean up the original vec
>>> v1
Vec()
>>> # New vec remains unaffected
>>> v3
Vec(1, 2, 3, 4, 5, 6)
Source code in src/pyochain/_vec.py
255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 | |
concat_mut(other)
Concatenate another Vec or list to self in place.
This is equivalent to list_1 += list_2 for standard lists.
Warning
This method modifies the Vec in place and returns the same instance for chaining.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
list[T] | Self
|
The other |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Self |
Self
|
The modified |
See Also
Vec::concatwhich returns a newVec(copy).Vec::extendwhich can take anyIterable.
Example
>>> from pyochain import Vec
>>> v1 = Vec.from_ref([1, 2, 3])
>>> v2 = [4, 5, 6] # Can also concatenate a standard list
>>> v1.concat_mut(v2)
Vec(1, 2, 3, 4, 5, 6)
>>> v1
Vec(1, 2, 3, 4, 5, 6)
Source code in src/pyochain/_vec.py
294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 | |
from_ref(data)
staticmethod
Create a Vec from a reference to an existing list.
This method wraps the provided list without copying it, allowing for efficient creation of a Vec.
This is the recommended way to create a Vec from foreign functions.
Warning
Since the Vec directly references the original list, any modifications made to the Vec will also affect the original list, and vice versa.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
list[V]
|
The |
required |
Returns:
| Type | Description |
|---|---|
Vec[V]
|
Vec[V]: A new Vec instance wrapping the provided |
Example
>>> from pyochain import Vec
>>> original_list = [1, 2, 3]
>>> vec = Vec.from_ref(original_list)
>>> vec
Vec(1, 2, 3)
>>> vec[0] = 10
>>> original_list
[10, 2, 3]
Source code in src/pyochain/_vec.py
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 | |
insert(index, value)
Inserts an element at position index within the vector, shifting all elements after it to the right.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
int
|
Position where to insert the element. |
required |
value
|
T
|
The element to insert. |
required |
Example
>>> from pyochain import Vec
>>> vec = Vec.from_ref(["a", "b", "c"])
>>> vec.insert(1, "d")
>>> vec
Vec('a', 'd', 'b', 'c')
>>> vec.insert(4, "e")
>>> vec
Vec('a', 'd', 'b', 'c', 'e')
Source code in src/pyochain/_vec.py
179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 | |
repeat(n)
Repeat the elements of the Vec n times and return a new Vec.
This is equivalent to list_1 * n for standard lists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
The number of times to repeat the elements. |
required |
Returns:
| Type | Description |
|---|---|
Vec[T]
|
Vec[T]: The new |
See Also
Vec::repeat_mut which modifies the Vec in place.
Example
>>> from pyochain import Vec
>>> Vec.from_ref([1, 2, 3]).repeat(2)
Vec(1, 2, 3, 1, 2, 3)
Source code in src/pyochain/_vec.py
124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 | |
repeat_mut(n)
Repeat the elements of the Vec in place.
This is equivalent to list_1 *= n for standard lists.
Warning
This method modifies the Vec in place and returns the same instance for chaining.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
The number of times to repeat the elements. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Self |
Self
|
The modified |
See Also
Vec::repeat which returns a new Vec (copy).
Example
>>> from pyochain import Vec
>>> vec = Vec.from_ref([1, 2, 3])
>>> vec.repeat_mut(2)
Vec(1, 2, 3, 1, 2, 3)
>>> vec
Vec(1, 2, 3, 1, 2, 3)
Source code in src/pyochain/_vec.py
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 | |
sort(*, reverse=False)
Sort the elements of the Vec in place.
Warning
This method modifies the Vec in place and returns the same instance for chaining.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reverse
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
Vec[U]
|
Vec[U]: The sorted |
Example
>>> from pyochain import Vec, Iter
>>> Vec.from_ref([3, 1, 2]).sort()
Vec(1, 2, 3)
Source code in src/pyochain/_vec.py
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 | |
sort_by(key, *, reverse=False)
Sort the elements of the Vec in place with a key function.
The key function is applied to each element before sorting, and the results are used for comparison.
Warning
This method modifies the Vec in place and returns the same instance for chaining.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
key
|
Callable[[T], SupportsAnyRichComparison]
|
function to extract a comparison key from each element. |
required |
reverse
|
bool
|
If True, sort in descending order. |
False
|
Returns:
| Name | Type | Description |
|---|---|---|
Self |
Self
|
The sorted |
Example
>>> from pyochain import Vec, Iter
>>> Vec.from_ref(["3", "1", "2"]).sort_by(int)
Vec('1', '2', '3')
Source code in src/pyochain/_vec.py
227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 | |