Skip to content

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
@final
class Vec[T](PyoMutableSequence[T], ArgsWrapper[T]):
    """Represent a mutable sequence of elements.

    Implement `collections::abc::MutableSequence`, and pyochain's `PyoMutableSequence` ABC.

    Unlike [`Seq`][core._seq.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`][core._seq.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.

    """
    @overload
    def __new__(cls, data: Iterable[T], /) -> Self: ...
    @overload
    def __new__(cls, data: T, /, *more: T) -> Self: ...
    @overload
    def __new__(cls, /) -> Self: ...
    def __new__(cls, data: Iterable[T] | T = (), /, *more: T) -> Self:
        """Create a new `Vec` instance.

        If not arguments are provided, an empty `Vec` is created.

        Args:
            data (Iterable[T] | T): The data to initialize the `Vec` with. Defaults to `()`.
            *more (T): Additional elements to include in the `Vec`.

        Returns:
            Self: A new `Vec` instance.

        Example:
            ```python
            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]
            ```
        """

    @override
    def __iter__(self) -> Iterator[T]: ...
    @overload
    def __getitem__(self, i: SupportsIndex, /) -> T: ...
    @overload
    def __getitem__(self, s: slice[SupportsIndex | None], /) -> Vec[T]: ...
    @override
    def __getitem__(
        self, index: SupportsIndex | slice[SupportsIndex | None]
    ) -> T | Vec[T]: ...
    @overload
    def __setitem__(self, key: SupportsIndex, value: T) -> None: ...
    @overload
    def __setitem__(
        self, key: slice[SupportsIndex | None], value: Iterable[T]
    ) -> None: ...
    @override
    def __setitem__(
        self, key: SupportsIndex | slice[SupportsIndex | None], value: T | Iterable[T]
    ) -> None: ...
    @override
    def __delitem__(self, key: SupportsIndex | slice[SupportsIndex | None]) -> None: ...
    @override
    def __len__(self) -> int: ...
    @override
    def __eq__(self, other: object) -> bool: ...
    @overload
    def __add__[V](self: Vec[V], value: IntoVec[V], /) -> Vec[V]: ...
    @overload
    def __add__[S](self, value: IntoVec[S], /) -> Vec[S | T]: ...
    def __add__[V, S](
        self: Vec[V], value: IntoVec[V] | IntoVec[S], /
    ) -> Vec[V] | Vec[S | V]: ...
    @override
    def __iadd__(self, value: Iterable[T], /) -> Vec[T]: ...
    def __mul__(self, value: SupportsIndex, /) -> Vec[T]: ...
    def __rmul__(self, value: SupportsIndex, /) -> Vec[T]: ...
    def __imul__(self, value: SupportsIndex, /) -> Vec[T]: ...
    @override
    def __contains__(self, key: object, /) -> bool: ...
    def __gt__(self, value: IntoVec[T], /) -> bool: ...
    def __ge__(self, value: IntoVec[T], /) -> bool: ...
    def __lt__(self, value: IntoVec[T], /) -> bool: ...
    def __le__(self, value: IntoVec[T], /) -> bool: ...
    @override
    def __reversed__(self) -> Iterator[T]: ...
    @override
    @staticmethod
    def of[E](*elements: E) -> Vec[E]: ...
    @override
    @staticmethod
    def from_iter[I](iterable: Iterable[I], /) -> Vec[I]: ...
    @staticmethod
    @override
    def wrap[S](iterable: list[S]) -> Vec[S]: ...  # pyright: ignore[reportIncompatibleMethodOverride]
    @override
    def reverse(self) -> None: ...
    @override
    def append(self, value: T) -> None: ...
    @override
    def extend(self, iterable: Iterable[T]) -> None: ...
    @override
    def clear(self) -> None: ...
    def copy(self) -> Self:
        """Return a shallow copy of the `Vec`.

        This is equivalent to `list_1.copy()` for standard lists.

        Returns:
            Self: A new `Vec` instance with the same elements.

        Example:
            ```python
            from pyochain import Vec

            v1 = Vec(1, 2, 3)
            v2 = v1.copy()
            assert v2 == Vec(1, 2, 3)
            assert v1 is not v2
            ```
        """

    def repeat(self, n: int) -> Vec[T]:
        """Repeat the elements of the `Vec` **n** times and return a new `Vec`.

        This is equivalent to `list_1 * n` for standard lists.

        Args:
            n (int): The number of times to repeat the elements.

        Returns:
            Vec[T]: The new `Vec` after repetition.

        See Also:
            [`Vec::repeat_mut`][repeat_mut] which modifies the `Vec` in place.

        Example:
            ```python
            from pyochain import Vec

            v = Vec(1, 2, 3).repeat(2)
            assert v == Vec(1, 2, 3, 1, 2, 3)
            ```
        """

    def repeat_mut(self, n: int) -> Self:
        """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.

        Args:
            n (int): The number of times to repeat the elements.

        Returns:
            Self: The modified `Vec` after repetition (self).

        See Also:
            [`Vec::repeat`][repeat] which returns a new `Vec` (copy).

        Example:
            ```python
            from pyochain import Vec

            vec = Vec(1, 2, 3).repeat_mut(2)
            assert vec == Vec(1, 2, 3, 1, 2, 3)
            ```
        """

    @override
    def insert(self, index: int, value: T) -> None:
        """Inserts an element at position index within the vector, shifting all elements after it to the right.

        Args:
            index (int): Position where to insert the element.
            value (T): The element to insert.

        Example:
            ```python
            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")
            ```
        """

    def sort[U: SupportsRichComparison](
        self: Vec[U], *, reverse: bool = False
    ) -> Vec[U]:
        """Sort the elements of the `Vec` in place.

        Warning:
            This method modifies the `Vec` in place and returns the same instance for chaining.

        Args:
            reverse (bool): If `True`, sort in descending order.

        Returns:
            Vec[U]: The sorted `Vec` instance (self).

        Example:
            ```python
            from pyochain import Vec, Iter

            x = Vec(3, 1, 2).sort()
            assert x == Vec(1, 2, 3)
            ```
        """

    def sort_by(
        self, key: Callable[[T], SupportsRichComparison], *, reverse: bool = False
    ) -> Self:
        """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.

        Args:
            key (Callable[[T], SupportsRichComparison]): function to extract a comparison key from each element.
            reverse (bool): If True, sort in descending order.

        Returns:
            Self: The sorted `Vec` instance (self).

        Example:
            ```python
            from pyochain import Vec, Iter

            x = Vec("3", "1", "2").sort_by(int)
            y = Vec("1", "2", "3")
            assert x == y
            ```
        """

    def concat(self, other: IntoVec[T]) -> Vec[T]:
        """Concatenate another `Vec` or `list` to **self** and return a new `Vec`.

        Note:
            This is equivalent to `list_1 + list_2` for standard lists.

        Args:
            other (IntoVec[T]): The other `Vec` to concatenate.

        Returns:
            Vec[T]: The new `Vec` after concatenation.

        See Also:
            [`Vec::concat_mut`][concat_mut] which modifies **self** in place.

        Example:
            ```python
            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
            ```
        """

    def concat_mut(self, other: IntoVec[T]) -> Self:
        """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.

        Args:
            other (IntoVec[T]): The other `Vec` to concatenate.

        Returns:
            Self: The modified `Vec` after concatenation (self).

        See Also:
            - [`concat`][concat] which returns a new `Vec` (copy).
            - [`extend`][abc._sequences.PyoMutableSequence.extend] which can take any `Iterable`.

        Example:
            ```python
            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
            ```
        """

__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 Vec with. Defaults to ().

()
*more T

Additional elements to include in the Vec.

()

Returns:

Name Type Description
Self Self

A new Vec instance.

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
def __new__(cls, data: Iterable[T] | T = (), /, *more: T) -> Self:
    """Create a new `Vec` instance.

    If not arguments are provided, an empty `Vec` is created.

    Args:
        data (Iterable[T] | T): The data to initialize the `Vec` with. Defaults to `()`.
        *more (T): Additional elements to include in the `Vec`.

    Returns:
        Self: A new `Vec` instance.

    Example:
        ```python
        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]
        ```
    """

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 Vec instance with the same elements.

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
def copy(self) -> Self:
    """Return a shallow copy of the `Vec`.

    This is equivalent to `list_1.copy()` for standard lists.

    Returns:
        Self: A new `Vec` instance with the same elements.

    Example:
        ```python
        from pyochain import Vec

        v1 = Vec(1, 2, 3)
        v2 = v1.copy()
        assert v2 == Vec(1, 2, 3)
        assert v1 is not v2
        ```
    """

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 Vec after repetition.

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
def repeat(self, n: int) -> Vec[T]:
    """Repeat the elements of the `Vec` **n** times and return a new `Vec`.

    This is equivalent to `list_1 * n` for standard lists.

    Args:
        n (int): The number of times to repeat the elements.

    Returns:
        Vec[T]: The new `Vec` after repetition.

    See Also:
        [`Vec::repeat_mut`][repeat_mut] which modifies the `Vec` in place.

    Example:
        ```python
        from pyochain import Vec

        v = Vec(1, 2, 3).repeat(2)
        assert v == Vec(1, 2, 3, 1, 2, 3)
        ```
    """

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 Vec after repetition (self).

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
def repeat_mut(self, n: int) -> Self:
    """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.

    Args:
        n (int): The number of times to repeat the elements.

    Returns:
        Self: The modified `Vec` after repetition (self).

    See Also:
        [`Vec::repeat`][repeat] which returns a new `Vec` (copy).

    Example:
        ```python
        from pyochain import Vec

        vec = Vec(1, 2, 3).repeat_mut(2)
        assert vec == Vec(1, 2, 3, 1, 2, 3)
        ```
    """

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
@override
def insert(self, index: int, value: T) -> None:
    """Inserts an element at position index within the vector, shifting all elements after it to the right.

    Args:
        index (int): Position where to insert the element.
        value (T): The element to insert.

    Example:
        ```python
        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")
        ```
    """

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 True, sort in descending order.

False

Returns:

Type Description
Vec[U]

Vec[U]: The sorted Vec instance (self).

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
def sort[U: SupportsRichComparison](
    self: Vec[U], *, reverse: bool = False
) -> Vec[U]:
    """Sort the elements of the `Vec` in place.

    Warning:
        This method modifies the `Vec` in place and returns the same instance for chaining.

    Args:
        reverse (bool): If `True`, sort in descending order.

    Returns:
        Vec[U]: The sorted `Vec` instance (self).

    Example:
        ```python
        from pyochain import Vec, Iter

        x = Vec(3, 1, 2).sort()
        assert x == Vec(1, 2, 3)
        ```
    """

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 Vec instance (self).

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
def sort_by(
    self, key: Callable[[T], SupportsRichComparison], *, reverse: bool = False
) -> Self:
    """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.

    Args:
        key (Callable[[T], SupportsRichComparison]): function to extract a comparison key from each element.
        reverse (bool): If True, sort in descending order.

    Returns:
        Self: The sorted `Vec` instance (self).

    Example:
        ```python
        from pyochain import Vec, Iter

        x = Vec("3", "1", "2").sort_by(int)
        y = Vec("1", "2", "3")
        assert x == y
        ```
    """

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 Vec to concatenate.

required

Returns:

Type Description
Vec[T]

Vec[T]: The new Vec after concatenation.

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
def concat(self, other: IntoVec[T]) -> Vec[T]:
    """Concatenate another `Vec` or `list` to **self** and return a new `Vec`.

    Note:
        This is equivalent to `list_1 + list_2` for standard lists.

    Args:
        other (IntoVec[T]): The other `Vec` to concatenate.

    Returns:
        Vec[T]: The new `Vec` after concatenation.

    See Also:
        [`Vec::concat_mut`][concat_mut] which modifies **self** in place.

    Example:
        ```python
        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
        ```
    """

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 Vec to concatenate.

required

Returns:

Name Type Description
Self Self

The modified Vec after concatenation (self).

See Also
  • concat which returns a new Vec (copy).
  • extend which can take any Iterable.
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
def concat_mut(self, other: IntoVec[T]) -> Self:
    """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.

    Args:
        other (IntoVec[T]): The other `Vec` to concatenate.

    Returns:
        Self: The modified `Vec` after concatenation (self).

    See Also:
        - [`concat`][concat] which returns a new `Vec` (copy).
        - [`extend`][abc._sequences.PyoMutableSequence.extend] which can take any `Iterable`.

    Example:
        ```python
        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
        ```
    """