-
-
Notifications
You must be signed in to change notification settings - Fork 119
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
roman matveev
committed
Aug 9, 2024
1 parent
77f7a33
commit b7a8f56
Showing
2 changed files
with
39 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
|
||
from typing import Iterable, List, TypeVar | ||
|
||
from returns.interfaces.specific import result | ||
from returns.primitives.exceptions import UnwrapFailedError | ||
|
||
_ValueType = TypeVar('_ValueType', covariant=True) | ||
_ErrorType = TypeVar('_ErrorType', covariant=True) | ||
_AdditionalType = TypeVar('_AdditionalType') | ||
|
||
|
||
def partition( | ||
containers: Iterable[ | ||
result.ResultBasedN[_ValueType, _ErrorType, _AdditionalType] | ||
], | ||
) -> tuple[List[_ValueType], List[_ErrorType]]: | ||
""" | ||
Partition a list of results into successful and failed unwrapped values. | ||
Preserves order. | ||
.. code:: python | ||
>>> from returns.result import Failure, Success | ||
>>> from returns.methods import partition | ||
>>> results = [Success(1), Failure(2), Success(3), Failure(4)] | ||
>>> partition(results) | ||
([1, 3], [2,4]) | ||
""" | ||
successes = [] | ||
failures = [] | ||
for container in containers: | ||
try: | ||
successes.append(container.unwrap()) | ||
except UnwrapFailedError: | ||
failures.append(container.failure()) | ||
return successes, failures |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters