-
-
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
Request: Truthiness of Result, Correct way to filter over Iterable[Result] #874
Comments
Hi! Thanks a lot for the detailed issue! 👋 I agree, that I also agree that
About other features:
|
Definitely a good point.
My intuition and least-surprisal is that But I also totally understand the sentiment of not wanting to make it too easy to interleave imperative-ish code with OH! I just discovered that
Do you mean like
where |
😮
Yes, regular function. |
Lol, is that shocked face because you didn't know that function could do that? If so, that's some really good interface programming right there! To have that kind of symmetry over all containers is exactly why I am not sure adding OTOH, it seems like the way def apply(...)...
if isinstance(self, self.failure_type):
return self
if isinstance(container, self.success_type):
return self.from_result(
self._inner_value.map(
container.unwrap()._inner_value, # noqa: WPS437
),
) So I would argue there is some precedent to add a Regardless of any of that, I think a filter function would serve >80% of the needed use-case (simple and easy way to filter out Falsy values) without having to change the protocol of the container interface. Albeit, this is less efficient since it's having to call So the default
So I would expect the predicate to be Well, I decided to profile it using things = [Success('success1'),Success('success2'), Failure(Exception('fail1')), Failure(Exception('fail2'))]
acc = Success(())
@pysnooper.snoop(depth=6)
def doit():
return Fold.collect_all(things, acc)
doit() I think the real question is, do constructions like But I think this will be a bridge that'll be have to be crossed if you implement |
I will have some time to think about all the points you've made. And then return with some feedback. |
it's way too late to change the existing API, but my two cents is that it would have been ideal to have Failure (and Nothing) define |
Not sure if this is a question, feature request, recommendation, documenting request, or whatnot.
Love what you folks are doing with this library. However, I've been going through the docs and examples and I'm not sure how to handle Result/Option at the boundary with less-functional-style Python. Take the example of filtering over some failure-prone operation.
out:
Alright, now I want to obtain only the successful results. My very first instinct is to reach for something like
[el for el in results if el]
/filter(lambda x: x, results)
, however Failure and Nothing are both Truthy. Alright, I can see how that interface makes sense, but then I want to reach for[el for el in results if el.ok]
, alas that doesn't exist.Looking around the repo for inspiration, I find
pipeline.is_successful
. Alright, that basically casts an UnwrapFailedError to a bool. I suppose I could do that, but seems rather clunky and not that performant. Alternatively, I could dobool(result.unwrap_or(False))
, but...ew. Checkingisinstance(result, Failure)
fails causeFailure
is a function, and_Failure
is private and thus discouraged.Ok there's
Fold
surely there'sfrom returns.iterables import Filter
....mmm nope. A list-like container with a.filter
method would also suffice, but I'm not seeing anything built-in. Maybe I'm supposed to use something likeFold.collect(results, ??)
? But I can't get the accumulator right.Fold.collect(results, Some(()))
just gives me a single Failure. I suspect I'd need some sort ofiterable.List
which acts likelist()
but supports theContainer
interface.Pipelining is all well and good, but in some places I want to start getting my feet wet with Maybes and Results, and it's a very shallow interface between lots of traditional imperative Python.
First, a question: What is the current canonical/recommended way to filter over a sequence of Results/Maybes? (see edit below)
My recommendations:
__bool__()
for Nothing and Failure to return False. Nothing is very obviously Falsy, but Failure is a little less clear-cut, though my gut is it's Falsy. This gives the nice advantage offilter
works identically over Result and Maybe. Law of least surprisal. Etc..ok
property (preferred) or.ok()
method. I can't think of any reason not to.iterables.Filter
anditerables.ImmutableList
or eveniterables.List
if you want to be a filthy heathen, just to be able to.map()
over lists.Pair
container is actually pretty cool and I can think of a few uses. It would be nice if that were initerables
rather than just in the test suite.I'd be willing to take a stab at writing up and PR-ing any of the above if you are interested.
Edit: Ok I'm a bit of a goofus, apparently I wanted
Fold.collect_all(results, Success(()))
. This still seems a little roundabout. I still think a convenience methodFold.filter(results)
makes sense here, whether it's just sugar forFold.collect_all(foo: List[Bar[Spam]], Bar(()))
, or checking for truthiness, or whatever.Might also be good to have a bolded header for Filtering an iterable of containers just below
Collecting an iterable of containers into a single container for, erm, the more oblivious developers out there ;)
The text was updated successfully, but these errors were encountered: