A small package introducing a new way to use higher order functions with lists
Project description
Qwery List
QList is a small library introducing a new way to use higher order functions with lists, with lazy evaluation. It also aims to address ugly python list methods such as map, filter and reduce. Whoever invented this:
xs = ['1', '2', '3', '4']
s = reduce(lambda acc, x: acc + x, filter(lambda x: x < 3, map(int, xs)), 0)
must reevaluate their life choices (yes, I am being cocky and most likely dumdum) but listen to me first and look what the world of lazy evaluation has to offer!
xs = QList(['1', '2', '3', '4'])
s = xs.map(int).filter(lambda x: x < 3).fold(lambda acc, x: acc + x, 0)
As a bonus you get len()
method, so no longer will you be forced to wrapp your
lists in this type of code len(xs)
and simply call xs.len()
(I understand it is negligibly
slower but look how much nicer it looks!)
Quick tutorial
Let's say we want to read numbers from a file and choose only the even ones. No problem at all!
with open('path/to/file.txt', 'r') as file:
qlist = QList(file.readlines())
even = qlist.map(int).filter(lambda x: x % 2 == 0).collect()
Why is there this collect
at the end? Because all operations on the QList are lazy evaluated,
so in order to finally apply all the operations you need to express that. (Eager evaluated module
coming soon...)
Examples
Making QList from an iterable
>>> QList([1, 2, 3, 4])
[1, 2, 3, 4]
Making QList from a generator
>>> QList(x for x in range(3))
[0, 1, 2]
Making a list of pairs: int
and str
>>> qlist = QList([1, 2, 3])
>>> qlist.zip(qlist.map(str)).collect()
[(1, '1'), (2, '2'), (3, '3')]
Summing only the even numbers
>>> QList(x for x in range(10)).filter(lambda x: x % 2 == 0).fold(lambda acc, x: acc + x, 0)
20
Side note
I hereby announce that UwU, Qwery Listwu
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file qwlist-0.1.2.tar.gz
.
File metadata
- Download URL: qwlist-0.1.2.tar.gz
- Upload date:
- Size: 4.5 kB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ccacd8d4af272013dc0bc25c8411e1f59c52855c76a3e2a19f183d681d5092ff |
|
MD5 | 9d6e635ae2e827c482ed59edff9c6d52 |
|
BLAKE2b-256 | 9fd09f07cc82592069e63b3b730f567b7d71a94258c1a876d10cabe1d67f8699 |
File details
Details for the file qwlist-0.1.2-py3-none-any.whl
.
File metadata
- Download URL: qwlist-0.1.2-py3-none-any.whl
- Upload date:
- Size: 4.3 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/4.0.2 CPython/3.11.6
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 88d313443215d41a61d6f9ae9a151bfca881abc9606d7f12c2ed161507c05a1f |
|
MD5 | 22fb78ff83d3c146e78ba884cee3b904 |
|
BLAKE2b-256 | 6c86aaee9484feb5856ff94f819213f703d78229e68d53550d2d7838209f424c |