-
-
Notifications
You must be signed in to change notification settings - Fork 120
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Adds transducers support #904
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work!
returns/transducers/tfilter.py
Outdated
.. code:: python | ||
|
||
>>> from typing import List | ||
>>> from returns.transducers import tfilter, reduce |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's import reduce
from functools
, because user might be confused: is it our reduce
or default one?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We need to use our reduce
since we have more features than the built-in reduce
>>> assert reduce(xform, my_list, []) == [0, 2, 4, 6] | ||
|
||
""" | ||
def reducer( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We tend to use decorator
/ factory
naming when defining nested decorators
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yep, but I think using that pattern might be weird:
def tmap(function):
def reducer(step):
def map_(acc, value):
...
return map_
return reducer
def tmap(function):
def decorator(step):
def factory(acc, value):
...
return map_
return reducer
In fact transducers is not intended to be use as a decorator and I think tmap -> reducer -> map_
is more meaningful! 🤔
Callable[[_AccValueType, _ValueType], _AccValueType], | ||
]: | ||
""" | ||
:py:func:`filter <filter>` implementation on a transducer form. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Let's make the first line more user-friendly by removing :py:func:
stuff.
It can be later in the body.
120a9fa
to
c6b4222
Compare
c6b4222
to
f589867
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great work 👏
|
||
|
||
#: A singleton representing any missing value | ||
Missing = _Missing() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Missing = _Missing() | |
Missing: Final = _Missing() |
from returns.transducers.transducers import _Missing | ||
|
||
|
||
def test_missing_singleton(): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
def test_missing_singleton(): | |
def test_missing_singleton() -> None: |
And all other tests as well.
|
||
|
||
def test_reduce(): | ||
"""Should fail when iterable is empty and non initial value is given.""" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Should we? Raising exceptions is not something we do in this library (at least we try to).
Can we just return the empty sequence? Or do anything else?
I have made things!
Checklist
CHANGELOG.md
Related issues
🙏 Please, if you or your company finds
dry-python
valuable, help us sustain the project by sponsoring it transparently on https://github.com/sponsors/dry-python. As a thank you, your profile/company logo will be added to our main README which receives hundreds of unique visitors per day.