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 :) |
sobolevn
left a comment
There was a problem hiding this comment.
Thanks a lot! Just one question 🙂
| """ | ||
|
|
||
| @abstractmethod | ||
| def filter( |
There was a problem hiding this comment.
Future note: we can also make an overload for TypeGuard case.
See python/typeshed#6140
There was a problem hiding this comment.
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.
Nevermind, this was a self note 🙂
This is even not supported by mypy yet.
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.
Is it possible to create a common filter_ function for all possible types? Result, IOResult, ...?
There was a problem hiding this comment.
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.
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.
@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.""" |
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.
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.mdRelated 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-pythonvaluable, 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.