-
-
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
Issue 1114 #1131
base: master
Are you sure you want to change the base?
Issue 1114 #1131
Conversation
We also need to make an interface type for |
@sobolevn I think i managed to add them, but lets say i'm not 100% sure about those. Looking forward for some feedback :) |
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.
Thanks a lot! Just one question 🙂
""" | ||
|
||
@abstractmethod | ||
def filter( |
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.
Future note: we can also make an overload
for TypeGuard
case.
See python/typeshed#6140
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.
Could you elaborate a bit what can we do with type guard here? We are not filtering for example a list with different types. Filter executed on _FilterableType
always returns _FilterableType
. Did I miss something?
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.
Nevermind, this was a self note 🙂
This is even not supported by mypy yet.
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.
It looks like mypy supports TypeGuard
now 🎉 :
returns/maybe.py
Outdated
@@ -430,10 +436,10 @@ def failure(self): | |||
raise UnwrapFailedError(self) | |||
|
|||
def filter(self, function): |
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.
Is it possible to create a common filter_
function for all possible types? Result
, IOResult
, ...?
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.
filter
is special in the sense that it 'empties' the container, which is possible in Maybe
, but not in Result
(i.e. what would the failure value of Success(4).filter(lambda x: False)
be?)
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.
Ok, I see! Than, it looks like we can use MaybeLikeN
to limit the input type of filter_
?
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.
@sobolevn I used it, please tell me if its something you were looking for. I am not entirely sure if I did it right :)
@sobolevn when i'm running |
I will take a look a bit later this week. Thanks a lot for the PR! |
returns/interfaces/filterable.py
Outdated
self: _FilterableType, | ||
predicate: Callable[[_FirstType], bool], | ||
) -> Kind1[_FilterableType, _FirstType]: | ||
"""Applies 'predicate' to the result fo a previous computation.""" |
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.
typo: fo->of
returns/interfaces/filterable.py
Outdated
>>> from returns.maybe import Nothing, Some | ||
>>> from returns.pointfree import filter_ | ||
|
||
>>> def example(argument: int) -> bool: |
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.
what would you think of naming the example function? Naming the function is_even
makes the example clearer in my mind
f4bf1be
to
aa3a303
Compare
e216c9c
to
60950f6
Compare
@sobolevn i think I found it |
Codecov ReportAll modified and coverable lines are covered by tests ✅
Additional details and impacted files@@ Coverage Diff @@
## master #1131 +/- ##
=========================================
Coverage 100.00% 100.00%
=========================================
Files 79 81 +2
Lines 2415 2450 +35
Branches 208 210 +2
=========================================
+ Hits 2415 2450 +35 ☔ View full report in Codecov by Sentry. |
I have made things!
Checklist
CHANGELOG.md
Related issues
Refs #1114
A simple implementation of filter for maybe. I am happy to implement something similar for Result, but I'm not entirely sure about the preferred approach.
In Scala it looks more or less like this
Not sure if something like this is ok for Python as well.
My second idea was to have a signature like
so user can specify exactly what happens in case of not matching predicate
🙏 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.