Lazy
Bases: Generic[T]
Object representing lazy evaluation of called methods.
Calling any method consumes the current Lazy
object. Using the same object
again may cause errors due to the draining of the generator.
Found in qwlist.Lazy
Examples:
>>> qlist = QList([1, 2, 3, 4])
>>> filtered = qlist.filter(lambda x: x < 3)
>>> mapped_str = filtered.map(str)
>>> mapped_float = filtered.map(float)
>>> print(mapped_float.qlist()) # prints [1.0, 2.0]
>>> print(mapped_str.qlist()) # prints []
Source code in src\qwlist\qwlist.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 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 |
|
__init__(gen)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
gen |
Iterable[T]
|
generator used to yield values on collecting items. |
required |
Source code in src\qwlist\qwlist.py
25 26 27 28 29 30 |
|
collect()
Evaluates the Lazy
object into QList
.
Same as calling qlist()
Returns: QList[T]
Source code in src\qwlist\qwlist.py
158 159 160 161 162 163 164 165 166 |
|
cycle()
Returns a Lazy[T]
that cycles through the elements of the Lazy
object, that means
on achieving the last element the iteration starts from the beginning. The
returned Lazy
object has no end (infinite iterator) unless the Lazy
object is empty
in which case cycle returns an empty Lazy
object (empty iterator).
Returns: Lazy[T]
Examples:
>>> Lazy([1, 2, 3]).cycle().take(7).collect()
[1, 2, 3, 1, 2, 3, 1]
Source code in src\qwlist\qwlist.py
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 |
|
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:
>>> Lazy(['a', 'b', 'c']).enumerate().collect()
[(0, 'a'), (1, 'b'), (2, 'c')]
Source code in src\qwlist\qwlist.py
247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 |
|
filter(pred)
Returns a Lazy
object containing all values from this Lazy
object for which
the predicate holds true.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
pred |
Callable[[T], bool]
|
|
required |
Returns: Lazy[T]
Examples:
>>> Lazy([0, 1, 2, 3]).filter(lambda x: x < 2).collect()
[0, 1]
Source code in src\qwlist\qwlist.py
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 |
|
flatmap(mapper)
Applies the mapper function to each of the yielded elements and flattens the results.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mapper |
Callable[[T], Iterable[K]]
|
|
required |
Returns: Lazy[K]
Examples:
>>> Lazy([1, 2]).flatmap(lambda x: [x, x]).qlist()
[1, 1, 2, 2]
Source code in src\qwlist\qwlist.py
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
|
flatten()
If self
is a Lazy
object of Iterable[T]
, flatten concatenates all iterables into a
single list and returns a Lazy[T]
object.
Returns: Lazy[T]
Examples:
>>> Lazy([[1, 2], [3, 4]]).flatten().collect()
[1, 2, 3, 4]
Source code in src\qwlist\qwlist.py
208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 |
|
fold(operation, init)
Given the combination operator reduces the Lazy
object by processing
its constituent parts, building up the final value.
Other names: fold_left, reduce, accumulate, aggregate
Parameters:
Name | Type | Description | Default |
---|---|---|---|
operation |
Callable[[K, T], K]
|
|
required |
init |
K
|
initial value for the combination operator. |
required |
Returns: K
Examples:
>>> Lazy([1, 2, 3]).fold(lambda acc, x: acc + x, 0)
6
Source code in src\qwlist\qwlist.py
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 |
|
foreach(action)
Applies the given function to each of yielded elements.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
action |
Callable[[T], None]
|
|
required |
Returns: None
Source code in src\qwlist\qwlist.py
111 112 113 114 115 116 117 118 119 120 121 |
|
list()
Evaluates the Lazy
object into list
.
Returns: list[T]
Source code in src\qwlist\qwlist.py
35 36 37 38 39 40 41 |
|
map(mapper)
Returns a Lazy
object containing all values from this Lazy
object with
the mapping function applied on them.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
mapper |
Callable[[T], K]
|
|
required |
Returns: Lazy[K]
Source code in src\qwlist\qwlist.py
72 73 74 75 76 77 78 79 80 81 82 83 84 85 |
|
qlist()
Evaluates the Lazy
object into QList
.
Same as calling collect()
Returns: QList[T]
Source code in src\qwlist\qwlist.py
43 44 45 46 47 48 49 50 |
|
skip(n)
Skips n first elements of the Lazy
object.
Args:
n: numbers of elements to skip. Should be non-negative
Returns: Lazy[T]
Examples:
>>> Lazy(range(10)).skip(2).collect()
[2, 3, 4, 5, 6, 7, 8, 9]
Source code in src\qwlist\qwlist.py
171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 |
|
take(n)
Takes n first elements of the Lazy
object.
Args:
n: numbers of elements to skip. Should be non-negative
Returns: Lazy[T]
Examples:
>>> Lazy(range(10)).take(2).collect()
[0, 1]
Source code in src\qwlist\qwlist.py
189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 |
|
zip(other)
Combines this Lazy
object with the given Iterable
elementwise as tuples.
The returned Lazy
objects yields at most the number of elements of
the shorter sequence (Lazy
or Iterable
).
Parameters:
Name | Type | Description | Default |
---|---|---|---|
other |
Iterable[K]
|
iterable to zip with this |
required |
Returns: Lazy[tuple[T, K]]
Examples:
>>> Lazy([1, 2, 3]).zip(['a', 'b', 'c']).collect()
[(1, 'a'), (2, 'b'), (3, 'c')]
Source code in src\qwlist\qwlist.py
141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
|