Skip to main content

A small package introducing a new way to use higher order functions with lists

Reason this release was yanked:

invalid import in QList eager method

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(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

This syntax resembles Rust syntax:

Rust Python
let xs = vec![1, 2, 3, 4];
let double_xs: Vec<i32> = xs.iter().map(|&x| x * 2).collect();
println!("{double_xs:?}");
// [2, 4, 6, 8]
xs = QList([1, 2, 3, 4])
double_xs = xs.map(lambda x: x * 2).collect()
print(double_xs)
# [2, 4, 6, 8]

Story behind this whole idea

Prime idea? Vicious mockery!
During studying, I had to do a lot of list comprehensions in Python, alongside methods such as map or filter and although they are quite powerful, using them in Python is just annoying. Combining them makes you read the code in an unnatural order going from right to left. That is the main reason that for a long time I preferred simple for-loops as opposed to using mentioned methods. Until one day my teacher asked the whole class why no one is using list comprehensions and higher order functions.
"Do you guys know python?" he asked tendentiously.
"I would use those functions if they were nicer" I thought.
During that period I also learnt Rust and immediately fell for it. Especially with how convenient it is to replace for-loops with method calls. And that's how the idea for a python package qwlist was born.

I hereby announce that UwU, Qwery Listwu!

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

qwlist-0.1.5.tar.gz (5.8 kB view details)

Uploaded Source

Built Distribution

qwlist-0.1.5-py3-none-any.whl (7.1 kB view details)

Uploaded Python 3

File details

Details for the file qwlist-0.1.5.tar.gz.

File metadata

  • Download URL: qwlist-0.1.5.tar.gz
  • Upload date:
  • Size: 5.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for qwlist-0.1.5.tar.gz
Algorithm Hash digest
SHA256 6b3126496c6cf7cffb12e778cab9ca5507a1994ad58f7707ab3bd87e76ca5644
MD5 08a9344c5cd6c065eb4269c985120fc6
BLAKE2b-256 665e970e3cae440e62042ac9e33408a33140d92684c9409273110a11db2b1895

See more details on using hashes here.

File details

Details for the file qwlist-0.1.5-py3-none-any.whl.

File metadata

  • Download URL: qwlist-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 7.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? Yes
  • Uploaded via: twine/4.0.2 CPython/3.11.6

File hashes

Hashes for qwlist-0.1.5-py3-none-any.whl
Algorithm Hash digest
SHA256 7c89e3aad0e7f92a8df4c6aed2af08fe25de0999581542c9ca9fa2190341c7ab
MD5 0dc3d3876fe0ecb8267a80cd29bf7985
BLAKE2b-256 5d3ca19417009febe03ce5a51098378e4a5c9b28f25840b079fbaa9da09faa40

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page