Skip to content

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 Iterable of elements to initialize the Vec with.

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
class Vec[T](PyoMutableSequence[T]):  # noqa: PLW1641
    """Represent a mutable sequence of elements.

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

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

    Args:
        data (Iterable[T]): Any `Iterable` of elements to initialize the `Vec` with.
    """

    __slots__ = ("_inner",)  # pyright: ignore[reportUnannotatedClassAttribute, reportIncompatibleUnannotatedOverride]
    _inner: list[T]

    def __init__(self, data: Iterable[T]) -> None:
        self._inner = list(data)

    @override
    def __repr__(self) -> str:
        return f"{self.__class__.__name__}({get_repr(self._inner)})"

    @overload
    def __getitem__(self, index: int) -> T: ...
    @overload
    def __getitem__(self, index: slice) -> MutableSequence[T]: ...
    @override
    def __getitem__(self, index: int | slice) -> T | MutableSequence[T]:
        return self._inner[index]

    # NOTE: typeshed typing makes it hard to satisfy both overloads of list and MutableSequence, I haven't found a way yet
    @overload
    def __setitem__(self, index: int, value: T) -> None: ...
    @overload
    def __setitem__(self, index: slice, value: Iterable[T]) -> None: ...
    @override
    def __setitem__(self, index: int | slice, value: T | Iterable[T]) -> None:
        # pyrefly: ignore[no-matching-overload]
        return self._inner.__setitem__(index, value)  # pyright: ignore[reportCallIssue, reportUnknownVariableType, reportArgumentType]

    @override
    def __delitem__(self, index: int | slice) -> None:
        del self._inner[index]

    @override
    def __len__(self) -> int:
        return len(self._inner)

    @override
    def __eq__(self, other: object) -> bool:
        match other:
            case Vec():
                return self._inner == other._inner  # pyright: ignore[reportUnknownMemberType]
            case list():
                return self._inner == other
            case _:
                return False

    @property
    @no_doctest
    def inner(self) -> list[T]:
        """Get the underlying `list` data structure.

        Useful when interoperating with functions that require a standard Python `list`.

        Returns:
            list[T]: The underlying list.
        """
        return self._inner

    @staticmethod
    def from_ref[V](data: list[V]) -> Vec[V]:
        """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.

        Args:
            data (list[V]): The `list` to wrap.

        Returns:
            Vec[V]: A new Vec instance wrapping the provided `list`.

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

            ```
        """
        instance: Vec[V] = Vec.__new__(Vec)  # pyright: ignore[reportUnknownVariableType]
        instance._inner = data
        return instance

    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
            >>> Vec.from_ref([1, 2, 3]).repeat(2)
            Vec(1, 2, 3, 1, 2, 3)

            ```
        """
        return self.from_ref(self._inner * n)

    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.from_ref([1, 2, 3])
            >>> vec.repeat_mut(2)
            Vec(1, 2, 3, 1, 2, 3)
            >>> vec
            Vec(1, 2, 3, 1, 2, 3)

            ```
        """
        self._inner *= n
        return self

    @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.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')

            ```
        """
        self._inner.insert(index, value)

    def sort[U: SupportsAnyRichComparison](
        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
            >>> Vec.from_ref([3, 1, 2]).sort()
            Vec(1, 2, 3)

            ```
        """
        self._inner.sort(reverse=reverse)
        return self

    def sort_by(
        self, key: Callable[[T], SupportsAnyRichComparison], *, 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], SupportsAnyRichComparison]): 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
            >>> Vec.from_ref(["3", "1", "2"]).sort_by(int)
            Vec('1', '2', '3')

            ```
        """
        self._inner.sort(key=key, reverse=reverse)
        return self

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

            ```
        """
        match other:
            case Vec():
                data = self._inner + other._inner
            case list():
                data = self._inner + other
        return Vec.from_ref(data)

    def concat_mut(self, other: list[T] | Self) -> 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 (list[T] | Self): The other `Vec` to concatenate.

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

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

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

            ```
        """
        match other:
            case Vec():
                self._inner += other._inner
            case list():
                self._inner += other
        return self

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

        ```
    """
    match other:
        case Vec():
            data = self._inner + other._inner
        case list():
            data = self._inner + other
    return Vec.from_ref(data)

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

required

Returns:

Name Type Description
Self Self

The modified Vec after concatenation (self).

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

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

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

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

        ```
    """
    match other:
        case Vec():
            self._inner += other._inner
        case list():
            self._inner += other
    return self

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 list to wrap.

required

Returns:

Type Description
Vec[V]

Vec[V]: A new Vec instance wrapping the provided list.

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
@staticmethod
def from_ref[V](data: list[V]) -> Vec[V]:
    """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.

    Args:
        data (list[V]): The `list` to wrap.

    Returns:
        Vec[V]: A new Vec instance wrapping the provided `list`.

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

        ```
    """
    instance: Vec[V] = Vec.__new__(Vec)  # pyright: ignore[reportUnknownVariableType]
    instance._inner = data
    return instance

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
@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.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')

        ```
    """
    self._inner.insert(index, value)

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
>>> 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
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
        >>> Vec.from_ref([1, 2, 3]).repeat(2)
        Vec(1, 2, 3, 1, 2, 3)

        ```
    """
    return self.from_ref(self._inner * n)

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.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
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.from_ref([1, 2, 3])
        >>> vec.repeat_mut(2)
        Vec(1, 2, 3, 1, 2, 3)
        >>> vec
        Vec(1, 2, 3, 1, 2, 3)

        ```
    """
    self._inner *= n
    return self

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
>>> 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
def sort[U: SupportsAnyRichComparison](
    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
        >>> Vec.from_ref([3, 1, 2]).sort()
        Vec(1, 2, 3)

        ```
    """
    self._inner.sort(reverse=reverse)
    return self

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

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
def sort_by(
    self, key: Callable[[T], SupportsAnyRichComparison], *, 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], SupportsAnyRichComparison]): 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
        >>> Vec.from_ref(["3", "1", "2"]).sort_by(int)
        Vec('1', '2', '3')

        ```
    """
    self._inner.sort(key=key, reverse=reverse)
    return self