Python Forum
Review on (new) Python module: Function but Lazy Python
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Review on (new) Python module: Function but Lazy Python
#1
Hello everyone, I have recently been working on a Python module and I wanted to have some feedback on it.

I really enjoy Python's syntax, except for iterators where I prefer a Rust-like syntax (functional programming) due to its readability. To this end, I began to work on a Python module that wraps any Python iterable into an object that allows for a Rust-like syntax.

I will write down below the basic example available on my PyPI project page.

Even though this project can be largely improved (see TODOs), I think it is yet usable and this is why I am looking for feedback from the community.
It can be about anything: bad or good project structure, lack of some features, whether you find interest or not in that, whether some Python module already does that (which I did not know), some tips, etc.

In advance, thanks!

Example

Given an input sequence x, print all, but the first, squared values that are divisible by 3 and collect the result into a list.

>>> x = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]  # Input sequence

# Usual way

>>> squares = map(lambda v: v * v, x)
>>> squares_div_by_3 = filter(lambda v: v % 3 == 0, squares)
>>> y = list(squares_div_by_3)[1:]  # We skip one value
>>> for v in y:
...     print(v)
36
81
>>> y
[36, 81]

# FLPy way

>>> from flpy import It

>>> y = (
...     It(x)
...     .map('|v| v * v')  # You can also use lambda or any other callable
...     .filter('|v| v % 3 == 0')
...     .skip(1)
...     .collect()  # Collects the iterator into a list
...     .for_each('|v| print(v)')  # Yet it still returns the list to `y`
... )
36
81
>>> y
ItA<[36, 81]>
Reply
#2
I know almost no Rust but I understand the efforts to make Python resemble the language you like. However, map and filter are not used very often in Python. One would rather write this as
>>> x = range(1, 11)
>>> seq = (y for y in (v * v for v in x) if y % 3 == 0)
>>> next(seq, None)
9
>>> for v in seq:
...     print(v)
... 
36
81
Reply
#3
(Oct-27-2021, 06:23 AM)Gribouillis Wrote: I know almost no Rust but I understand the efforts to make Python resemble the language you like. However, map and filter are not used very often in Python. One would rather write this as
>>> x = range(1, 11)
>>> seq = (y for y in (v * v for v in x) if y % 3 == 0)
>>> next(seq, None)
9
>>> for v in seq:
...     print(v)
... 
36
81

Thanks for commenting! Indeed, my example does not highlight the best way to approach the problem in pure Python. It was more to show the, in my opinion, enhanced readability of the program as a whole.
Reply
#4
Toolz is another good library.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Python code review | Tkinter gui application Sr999 19 2,016 Dec-03-2023, 10:24 PM
Last Post: menator01
  First time python user - Calculator code review Steamy 1 2,173 Jul-22-2020, 05:59 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020