EagerQList - Qwery List

Skip to content

# EagerQList

Bases: `list`

`EagerQList` is a python list extension that adds several chainable, methods to the standard `list`.

Found in `qwlist.eager.EagerQList`

Source code in `src\qwlist\eager.py`

      8
      9
     10
     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
    class EagerQList(list):
        """
        `EagerQList` is a python list extension that adds several chainable, methods to the standard `list`.

        Found in `qwlist.eager.EagerQList`
        """
        @overload
        def __getitem__(self, item: int) -> T:
            ...

        @overload
        def __getitem__(self, item: slice) -> "EagerQList[T]":
            ...

        def __getitem__(self, item):
            if isinstance(item, slice):
                return EagerQList(super().__getitem__(item))
            return super().__getitem__(item)

        def list(self) -> list[T]:
            """
            Changes `EagerQList` into `list`.

            Returns: `list[T]`
            """
            return list(self)

        def qlist(self) -> "QList[T]":
            """
            Changes `EagerQList` into `Qlist`.

            Returns: `QList[T]`
            """
            from qwlist import QList
            return QList(self)

        def filter(self, pred: Callable[[T], bool]) -> "EagerQList[T]":
            """
            Returns an `EagerQList` containing all values from this `EagerQList` for which
            the predicate holds true.

            Args:
                 pred: `function (T) -> bool`

            Returns: `EagerQList[T]`

            Examples:
                >>> EagerQList([1, 2, 3, 4]).filter(lambda x: x < 3)
                [1, 2]
            """
            return EagerQList(elem for elem in self if pred(elem))

        def map(self, mapper: Callable[[T], K]) -> "EagerQList[K]":
            """
            Returns an `EagerQList` containing all values from this `EagerQList` with
            the mapping function applied on them.

            Args:
                mapper: `function: (T) -> K`

            Returns: `EagerQList[K]`
            """
            return EagerQList(mapper(elem) for elem in self)

        def foreach(self, action: Callable[[T], None]):
            """
            Applies the given function to each of the `EagerQList` elements.

            Args:
                action: `function (T) -> None`

            Returns: `None`
            """
            for elem in self:
                action(elem)

        def fold(self, operation: Callable[[K, T], K], init: K) -> K:
            """
            Given the combination operator reduces the `EagerQList` by processing
            its values, building up the final value.

            **Other names:** fold_left, reduce, accumulate, aggregate

            Args:
                operation: `function: (K, T) -> K`
                    Given the initial value `init` applies the
                    given combination operator on each element of the EagerQList,
                    treating the result as a first argument in the next step.
                init: initial value for the combination operator.

            Returns: `K`

            Examples:
                >>> s = EagerQList([1, 2, 3]).fold(lambda acc, x: acc + x, 0)
                6
            """
            acc = init
            for elem in self:
                acc = operation(acc, elem)
            return acc

        def fold_right(self, operation: Callable[[K, T], K], init: K) -> K:
            """
            Given the combination operator reduces the `EagerQList` by processing
            its values, building up the final value.

            Args:
                operation: `function: (K, T) -> K`
                    Given the initial value `init` applies the
                    given combination operator on each element of the QList, starting from the
                    last element, treating the result as a first argument in the next step.
                init: initial value for the combination operator.

            Returns: `K`

            Examples:
                >>> s = EagerQList([1, 2, 3]).fold_right(lambda acc, x: acc + x, 0)
                6
            """
            acc = init
            for elem in self[::-1]:
                acc = operation(acc, elem)
            return acc

        def len(self):
            return len(self)

        def flatmap(self, mapper: Callable[[T], Iterable[K]]) -> "EagerQList[K]":
            """
            Applies the mapper function to each element of the `EagerQList` and flattens the results.

            Args:
                mapper: `function (T) -> Iterable[K]`

            Returns: `EagerQList[K]`

            Examples:
                >>> EagerQList([1, 2]).flatmap(lambda x: [x, x])
                [1, 1, 2, 2]
            """
            return EagerQList(x for elem in self for x in mapper(elem))

        def zip(self, other: Iterable[K]) -> "EagerQList[tuple[T, K]]":
            """
            Combines this `EagerQList` with the given `Iterable` elementwise as tuples.
             The returned `EagerQList` objects has at most the number of elements of
             the shorter sequence (`self` or `Iterable`).

            Args:
                other: iterable to zip with this `EagerQList`.

            Returns: `EagerQList[tuple[T, K]]`
            """
            return EagerQList(zip(self, other))

        def sorted(self, key: Callable[[T], SupportsLessThan] = None, reverse: bool = False) -> "EagerQList[T]":
            """
            Returns a new `EagerQList` containing all items from the original list in ascending order.

            A custom key function can be supplied to customize the sort order, and the reverse
            flag can be set to request the result in descending order.

            Args:
                key: `function (T) -> SupportsLessThan`. Defaults to `None`
                reverse: if set to `True` sorts values in descending order. Defaults to `False`

            Returns: `EagerQList[T]`
            """
            return EagerQList(sorted(self, key=key, reverse=reverse))

        def flatten(self) -> "EagerQList[T]":
            """
            If self is a `EagerQList` of `Iterable[T]` flatten concatenates all iterables into a
            single list and returns a new `EagerQList[T]`.
            Returns: `EagerQList[T]`
            """
            def inner():
                for elem in self:
                    yield from elem
            return EagerQList(inner())

        def enumerate(self, start: int = 0) -> "EagerQList[tuple[int, T]]":
            """
            Returns a `Lazy` object with index-value pairs as its elements. Index starts at
            the given position `start` (defaults to 0).

            Returns: Lazy[tuple[int, T]]

            Examples:
                >>> EagerQList(['a', 'b', 'c']).enumerate()
                [(0, 'a'), (1, 'b'), (2, 'c')]
            """
            def inner():
                for i, elem in enumerate(self, start=start):
                    yield i, elem
            return EagerQList(inner())

## `enumerate(start=0)`

Returns a `Lazy` object with index-value pairs as its elements. Index starts at the given position `start` (defaults to 0).

Returns: Lazy[tuple[int, T]]

Examples:

    >>> EagerQList(['a', 'b', 'c']).enumerate()
    [(0, 'a'), (1, 'b'), (2, 'c')]

Source code in `src\qwlist\eager.py`

    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    def enumerate(self, start: int = 0) -> "EagerQList[tuple[int, T]]":
        """
        Returns a `Lazy` object with index-value pairs as its elements. Index starts at
        the given position `start` (defaults to 0).

        Returns: Lazy[tuple[int, T]]

        Examples:
            >>> EagerQList(['a', 'b', 'c']).enumerate()
            [(0, 'a'), (1, 'b'), (2, 'c')]
        """
        def inner():
            for i, elem in enumerate(self, start=start):
                yield i, elem
        return EagerQList(inner())

## `filter(pred)`

Returns an `EagerQList` containing all values from this `EagerQList` for which the predicate holds true.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`pred` | `Callable[[T], bool]` | `function (T) -> bool` | required

Returns: `EagerQList[T]`

Examples:

    >>> EagerQList([1, 2, 3, 4]).filter(lambda x: x < 3)
    [1, 2]

Source code in `src\qwlist\eager.py`

    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    def filter(self, pred: Callable[[T], bool]) -> "EagerQList[T]":
        """
        Returns an `EagerQList` containing all values from this `EagerQList` for which
        the predicate holds true.

        Args:
             pred: `function (T) -> bool`

        Returns: `EagerQList[T]`

        Examples:
            >>> EagerQList([1, 2, 3, 4]).filter(lambda x: x < 3)
            [1, 2]
        """
        return EagerQList(elem for elem in self if pred(elem))

## `flatmap(mapper)`

Applies the mapper function to each element of the `EagerQList` and flattens the results.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`mapper` | `Callable[[T], Iterable[K]]` | `function (T) -> Iterable[K]` | required

Returns: `EagerQList[K]`

Examples:

    >>> EagerQList([1, 2]).flatmap(lambda x: [x, x])
    [1, 1, 2, 2]

Source code in `src\qwlist\eager.py`

    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    def flatmap(self, mapper: Callable[[T], Iterable[K]]) -> "EagerQList[K]":
        """
        Applies the mapper function to each element of the `EagerQList` and flattens the results.

        Args:
            mapper: `function (T) -> Iterable[K]`

        Returns: `EagerQList[K]`

        Examples:
            >>> EagerQList([1, 2]).flatmap(lambda x: [x, x])
            [1, 1, 2, 2]
        """
        return EagerQList(x for elem in self for x in mapper(elem))

## `flatten()`

If self is a `EagerQList` of `Iterable[T]` flatten concatenates all iterables into a single list and returns a new `EagerQList[T]`. Returns: `EagerQList[T]`

Source code in `src\qwlist\eager.py`

    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    def flatten(self) -> "EagerQList[T]":
        """
        If self is a `EagerQList` of `Iterable[T]` flatten concatenates all iterables into a
        single list and returns a new `EagerQList[T]`.
        Returns: `EagerQList[T]`
        """
        def inner():
            for elem in self:
                yield from elem
        return EagerQList(inner())

## `fold(operation, init)`

Given the combination operator reduces the `EagerQList` by processing its values, building up the final value.

Other names: fold_left, reduce, accumulate, aggregate

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`operation` | `Callable[[K, T], K]` | `function: (K, T) -> K` Given the initial value `init` applies the given combination operator on each element of the EagerQList, treating the result as a first argument in the next step. | required
`init` | `K` | initial value for the combination operator. | required

Returns: `K`

Examples:

    >>> s = EagerQList([1, 2, 3]).fold(lambda acc, x: acc + x, 0)
    6

Source code in `src\qwlist\eager.py`

     84
     85
     86
     87
     88
     89
     90
     91
     92
     93
     94
     95
     96
     97
     98
     99
    100
    101
    102
    103
    104
    105
    106
    107
    def fold(self, operation: Callable[[K, T], K], init: K) -> K:
        """
        Given the combination operator reduces the `EagerQList` by processing
        its values, building up the final value.

        **Other names:** fold_left, reduce, accumulate, aggregate

        Args:
            operation: `function: (K, T) -> K`
                Given the initial value `init` applies the
                given combination operator on each element of the EagerQList,
                treating the result as a first argument in the next step.
            init: initial value for the combination operator.

        Returns: `K`

        Examples:
            >>> s = EagerQList([1, 2, 3]).fold(lambda acc, x: acc + x, 0)
            6
        """
        acc = init
        for elem in self:
            acc = operation(acc, elem)
        return acc

## `fold_right(operation, init)`

Given the combination operator reduces the `EagerQList` by processing its values, building up the final value.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`operation` | `Callable[[K, T], K]` | `function: (K, T) -> K` Given the initial value `init` applies the given combination operator on each element of the QList, starting from the last element, treating the result as a first argument in the next step. | required
`init` | `K` | initial value for the combination operator. | required

Returns: `K`

Examples:

    >>> s = EagerQList([1, 2, 3]).fold_right(lambda acc, x: acc + x, 0)
    6

Source code in `src\qwlist\eager.py`

    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    def fold_right(self, operation: Callable[[K, T], K], init: K) -> K:
        """
        Given the combination operator reduces the `EagerQList` by processing
        its values, building up the final value.

        Args:
            operation: `function: (K, T) -> K`
                Given the initial value `init` applies the
                given combination operator on each element of the QList, starting from the
                last element, treating the result as a first argument in the next step.
            init: initial value for the combination operator.

        Returns: `K`

        Examples:
            >>> s = EagerQList([1, 2, 3]).fold_right(lambda acc, x: acc + x, 0)
            6
        """
        acc = init
        for elem in self[::-1]:
            acc = operation(acc, elem)
        return acc

## `foreach(action)`

Applies the given function to each of the `EagerQList` elements.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`action` | `Callable[[T], None]` | `function (T) -> None` | required

Returns: `None`

Source code in `src\qwlist\eager.py`

    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    def foreach(self, action: Callable[[T], None]):
        """
        Applies the given function to each of the `EagerQList` elements.

        Args:
            action: `function (T) -> None`

        Returns: `None`
        """
        for elem in self:
            action(elem)

## `list()`

Changes `EagerQList` into `list`.

Returns: `list[T]`

Source code in `src\qwlist\eager.py`

    27
    28
    29
    30
    31
    32
    33
    def list(self) -> list[T]:
        """
        Changes `EagerQList` into `list`.

        Returns: `list[T]`
        """
        return list(self)

## `map(mapper)`

Returns an `EagerQList` containing all values from this `EagerQList` with the mapping function applied on them.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`mapper` | `Callable[[T], K]` | `function: (T) -> K` | required

Returns: `EagerQList[K]`

Source code in `src\qwlist\eager.py`

    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    def map(self, mapper: Callable[[T], K]) -> "EagerQList[K]":
        """
        Returns an `EagerQList` containing all values from this `EagerQList` with
        the mapping function applied on them.

        Args:
            mapper: `function: (T) -> K`

        Returns: `EagerQList[K]`
        """
        return EagerQList(mapper(elem) for elem in self)

## `qlist()`

Changes `EagerQList` into `Qlist`.

Returns: `QList[T]`

Source code in `src\qwlist\eager.py`

    35
    36
    37
    38
    39
    40
    41
    42
    def qlist(self) -> "QList[T]":
        """
        Changes `EagerQList` into `Qlist`.

        Returns: `QList[T]`
        """
        from qwlist import QList
        return QList(self)

## `sorted(key=None, reverse=False)`

Returns a new `EagerQList` containing all items from the original list in ascending order.

A custom key function can be supplied to customize the sort order, and the reverse flag can be set to request the result in descending order.

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`key` | `Callable[[T], SupportsLessThan]` | `function (T) -> SupportsLessThan`. Defaults to `None` | `None`
`reverse` | `bool` | if set to `True` sorts values in descending order. Defaults to `False` | `False`

Returns: `EagerQList[T]`

Source code in `src\qwlist\eager.py`

    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    def sorted(self, key: Callable[[T], SupportsLessThan] = None, reverse: bool = False) -> "EagerQList[T]":
        """
        Returns a new `EagerQList` containing all items from the original list in ascending order.

        A custom key function can be supplied to customize the sort order, and the reverse
        flag can be set to request the result in descending order.

        Args:
            key: `function (T) -> SupportsLessThan`. Defaults to `None`
            reverse: if set to `True` sorts values in descending order. Defaults to `False`

        Returns: `EagerQList[T]`
        """
        return EagerQList(sorted(self, key=key, reverse=reverse))

## `zip(other)`

Combines this `EagerQList` with the given `Iterable` elementwise as tuples. The returned `EagerQList` objects has at most the number of elements of the shorter sequence (`self` or `Iterable`).

Parameters:

Name | Type | Description | Default
--- | --- | --- | ---
`other` | `Iterable[K]` | iterable to zip with this `EagerQList`. | required

Returns: `EagerQList[tuple[T, K]]`

Source code in `src\qwlist\eager.py`

    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    def zip(self, other: Iterable[K]) -> "EagerQList[tuple[T, K]]":
        """
        Combines this `EagerQList` with the given `Iterable` elementwise as tuples.
         The returned `EagerQList` objects has at most the number of elements of
         the shorter sequence (`self` or `Iterable`).

        Args:
            other: iterable to zip with this `EagerQList`.

        Returns: `EagerQList[tuple[T, K]]`
        """
        return EagerQList(zip(self, other))