Skip to content
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

Open
wants to merge 10 commits into
base: master
Choose a base branch
from
Open

Conversation

CucumisSativus
Copy link

I have made things!

Checklist

  • I have double checked that there are no unrelated changes in this pull request (old patches, accidental config files, etc)
  • I have created at least one test case for the changes I have made
  • I have updated the documentation for the changes I have made
  • I have added my changes to the 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

      if (p(value)) this else Failure(new NoSuchElementException("Predicate does not hold for " + value))

Not sure if something like this is ok for Python as well.

My second idea was to have a signature like

def filter(predicate, on_failure):
(...)

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.

@sobolevn
Copy link
Member

We also need to make an interface type for filter. This way we will be able to use with HKT.
See interfaces/ folder.

@CucumisSativus
Copy link
Author

@sobolevn I think i managed to add them, but lets say i'm not 100% sure about those. Looking forward for some feedback :)

Copy link
Member

@sobolevn sobolevn left a 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(
Copy link
Member

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

Copy link
Author

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?

Copy link
Member

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.

Copy link
Contributor

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):
Copy link
Member

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, ...?

Copy link
Contributor

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?)

Copy link
Member

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_?

Copy link
Author

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 :)

@CucumisSativus
Copy link
Author

@sobolevn when i'm running poetry run pytest typesafety -p no:cov -o addopts="" --mypy-ini-file=setup.cfg locally I get 830 passed, 1 warning. Any ideas why does it fail on github?

@sobolevn
Copy link
Member

I will take a look a bit later this week.

Thanks a lot for the PR!

self: _FilterableType,
predicate: Callable[[_FirstType], bool],
) -> Kind1[_FilterableType, _FirstType]:
"""Applies 'predicate' to the result fo a previous computation."""
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

typo: fo->of

>>> from returns.maybe import Nothing, Some
>>> from returns.pointfree import filter_

>>> def example(argument: int) -> bool:
Copy link
Contributor

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

@CucumisSativus
Copy link
Author

@sobolevn i think I found it

@codecov
Copy link

codecov bot commented Dec 12, 2021

Codecov Report

Merging #1131 (cca6894) into master (b782c2e) will not change coverage.
The diff coverage is 100.00%.

Impacted file tree graph

@@            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     
Impacted Files Coverage Δ
returns/interfaces/filterable.py 100.00% <100.00%> (ø)
returns/maybe.py 100.00% <100.00%> (ø)
returns/pointfree/__init__.py 100.00% <100.00%> (ø)
returns/pointfree/filter.py 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update b782c2e...cca6894. Read the comment docs.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

4 participants