Vec
Bases: PyoMutableSequence[T], ArgsWrapper[T]
flowchart TD
pyochain.core._vec.Vec[Vec]
pyochain.abc._sequences.PyoMutableSequence[PyoMutableSequence]
pyochain.abc._sequences.PyoSequence[PyoSequence]
pyochain.abc._sequences.PyoReversible[PyoReversible]
pyochain.abc._collection.PyoCollection[PyoCollection]
pyochain.abc._iterable.PyoIterable[PyoIterable]
pyochain.abc._collection.PyoContainer[PyoContainer]
pyochain.abc._collection.PyoSized[PyoSized]
pyochain.abc._mixins.Checkable[Checkable]
pyochain.abc._mixins.Fluent[Fluent]
pyochain.abc._mixins.Pipe[Pipe]
pyochain.abc._mixins.Tap[Tap]
pyochain.abc.constructors.ArgsWrapper[ArgsWrapper]
pyochain.abc.constructors.FromArgs[FromArgs]
pyochain.abc.constructors.FromIter[FromIter]
pyochain.abc.constructors.Wrapper[Wrapper]
pyochain.abc._sequences.PyoMutableSequence --> pyochain.core._vec.Vec
pyochain.abc._sequences.PyoSequence --> pyochain.abc._sequences.PyoMutableSequence
pyochain.abc._sequences.PyoReversible --> pyochain.abc._sequences.PyoSequence
pyochain.abc._iterable.PyoIterable --> pyochain.abc._sequences.PyoReversible
pyochain.abc._mixins.Checkable --> pyochain.abc._iterable.PyoIterable
pyochain.abc._mixins.Fluent --> pyochain.abc._iterable.PyoIterable
pyochain.abc._mixins.Pipe --> pyochain.abc._mixins.Fluent
pyochain.abc._mixins.Tap --> pyochain.abc._mixins.Fluent
pyochain.abc._collection.PyoCollection --> pyochain.abc._sequences.PyoSequence
pyochain.abc._iterable.PyoIterable --> pyochain.abc._collection.PyoCollection
pyochain.abc._mixins.Checkable --> pyochain.abc._iterable.PyoIterable
pyochain.abc._mixins.Fluent --> pyochain.abc._iterable.PyoIterable
pyochain.abc._mixins.Pipe --> pyochain.abc._mixins.Fluent
pyochain.abc._mixins.Tap --> pyochain.abc._mixins.Fluent
pyochain.abc._collection.PyoContainer --> pyochain.abc._collection.PyoCollection
pyochain.abc._mixins.Checkable --> pyochain.abc._collection.PyoContainer
pyochain.abc._collection.PyoSized --> pyochain.abc._collection.PyoCollection
pyochain.abc._mixins.Checkable --> pyochain.abc._collection.PyoSized
pyochain.abc.constructors.ArgsWrapper --> pyochain.core._vec.Vec
pyochain.abc.constructors.FromArgs --> pyochain.abc.constructors.ArgsWrapper
pyochain.abc.constructors.FromIter --> pyochain.abc.constructors.FromArgs
pyochain.abc.constructors.Wrapper --> pyochain.abc.constructors.ArgsWrapper
click pyochain.core._vec.Vec href "" "pyochain.core._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._sequences.PyoReversible href "" "pyochain.abc._sequences.PyoReversible"
click pyochain.abc._collection.PyoCollection href "" "pyochain.abc._collection.PyoCollection"
click pyochain.abc._iterable.PyoIterable href "" "pyochain.abc._iterable.PyoIterable"
click pyochain.abc._collection.PyoContainer href "" "pyochain.abc._collection.PyoContainer"
click pyochain.abc._collection.PyoSized href "" "pyochain.abc._collection.PyoSized"
click pyochain.abc._mixins.Checkable href "" "pyochain.abc._mixins.Checkable"
click pyochain.abc._mixins.Fluent href "" "pyochain.abc._mixins.Fluent"
click pyochain.abc._mixins.Pipe href "" "pyochain.abc._mixins.Pipe"
click pyochain.abc._mixins.Tap href "" "pyochain.abc._mixins.Tap"
click pyochain.abc.constructors.ArgsWrapper href "" "pyochain.abc.constructors.ArgsWrapper"
click pyochain.abc.constructors.FromArgs href "" "pyochain.abc.constructors.FromArgs"
click pyochain.abc.constructors.FromIter href "" "pyochain.abc.constructors.FromIter"
click pyochain.abc.constructors.Wrapper href "" "pyochain.abc.constructors.Wrapper"
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.
Source code in pyochain/core/_vec.pyi
11 12 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 | |
__new__(data=(), /, *more)
__new__(data: Iterable[T]) -> Self
__new__(data: T, /, *more: T) -> Self
__new__() -> Self
Create a new Vec instance.
If not arguments are provided, an empty Vec is created.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Iterable[T] | T
|
The data to initialize the |
()
|
*more
|
T
|
Additional elements to include in the |
()
|
Returns:
| Name | Type | Description |
|---|---|---|
Self |
Self
|
A new |
Example
from pyochain import Vec
py_list = [1, 2, 3]
# Create a Vec from an iterable
assert Vec(iter(py_list)) == Vec(py_list)
# Create a Vec from individual elements
assert Vec(1, 2, 3) == py_list
# Create an empty Vec
assert Vec() == Vec([]) == Vec(()) == []
# Creating a Vec from a list will copy the underlying data
vec = Vec(py_list)
vec[0] = 10
assert py_list == [1, 2, 3]
Source code in pyochain/core/_vec.pyi
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 | |
copy()
Return a shallow copy of the Vec.
This is equivalent to list_1.copy() for standard lists.
Returns:
| Name | Type | Description |
|---|---|---|
Self |
Self
|
A new |
Example
from pyochain import Vec
v1 = Vec(1, 2, 3)
v2 = v1.copy()
assert v2 == Vec(1, 2, 3)
assert v1 is not v2
Source code in pyochain/core/_vec.pyi
128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 | |
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
v = Vec(1, 2, 3).repeat(2)
assert v == Vec(1, 2, 3, 1, 2, 3)
Source code in pyochain/core/_vec.pyi
147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 | |
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(1, 2, 3).repeat_mut(2)
assert vec == Vec(1, 2, 3, 1, 2, 3)
Source code in pyochain/core/_vec.pyi
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 | |
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("a", "b", "c")
vec.insert(1, "d")
assert vec == Vec("a", "d", "b", "c")
vec.insert(4, "e")
assert vec == Vec("a", "d", "b", "c", "e")
Source code in pyochain/core/_vec.pyi
196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 | |
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
x = Vec(3, 1, 2).sort()
assert x == Vec(1, 2, 3)
Source code in pyochain/core/_vec.pyi
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 | |
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], SupportsRichComparison]
|
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
x = Vec("3", "1", "2").sort_by(int)
y = Vec("1", "2", "3")
assert x == y
Source code in pyochain/core/_vec.pyi
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 | |
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
|
IntoVec[T]
|
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(1, 2, 3)
v2 = [4, 5, 6] # Can also concatenate a standard list
expected = Vec(1, 2, 3, 4, 5, 6)
v3 = v1.concat(v2)
assert v3 == expected
v1.clear() # Clean up the original vec
assert v1 == Vec()
# New vec remains unaffected
assert v3 == expected
Source code in pyochain/core/_vec.pyi
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 | |
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
|
IntoVec[T]
|
The other |
required |
Returns:
| Name | Type | Description |
|---|---|---|
Self |
Self
|
The modified |
Example
from pyochain import Vec
v1 = Vec(1, 2, 3)
v2 = [4, 5, 6] # Can also concatenate a standard list
expected = Vec(1, 2, 3, 4, 5, 6)
assert v1.concat_mut(v2) == expected
assert v1 == expected
Source code in pyochain/core/_vec.pyi
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 | |