-
-
Notifications
You must be signed in to change notification settings - Fork 123
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
1 parent
e444ea7
commit e96a354
Showing
6 changed files
with
121 additions
and
0 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
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
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 |
---|---|---|
@@ -1,4 +1,5 @@ | ||
from returns.methods.cond import cond as cond | ||
from returns.methods.partition import partition as partition | ||
from returns.methods.unwrap_or_failure import ( | ||
unwrap_or_failure as unwrap_or_failure, | ||
) |
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.unwrappable import Unwrappable | ||
from returns.primitives.exceptions import UnwrapFailedError | ||
|
||
_ValueType = TypeVar('_ValueType', covariant=True) | ||
_ErrorType = TypeVar('_ErrorType', covariant=True) | ||
|
||
|
||
def partition( | ||
containers: Iterable[ | ||
Unwrappable[_ValueType, _ErrorType], | ||
], | ||
) -> tuple[List[_ValueType], List[_ErrorType]]: | ||
""" | ||
Partition a list of unwrappables into successful and failed 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: list[_ValueType] = [] | ||
failures: list[_ErrorType] = [] | ||
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
|
||
import pytest | ||
|
||
from returns.io import IO, IOResult | ||
from returns.maybe import Nothing, Some | ||
from returns.methods import partition | ||
from returns.result import Failure, Success | ||
|
||
|
||
@pytest.mark.parametrize(('containers', 'expected'), [ | ||
( | ||
(Success(1), Success(2), Failure(None), Success(3)), | ||
([1, 2, 3], [None]), | ||
), | ||
( | ||
( | ||
IOResult.from_value(1), | ||
IOResult.from_failure(2), | ||
IOResult.from_value(3), | ||
IOResult.from_failure(4), | ||
), | ||
([IO(1), IO(3)], [IO(2), IO(4)]), | ||
), | ||
( | ||
(Some(1), Some(2), Nothing), | ||
([1, 2], [None]), | ||
), | ||
((), ([], [])), | ||
]) | ||
def test_partition(containers, expected): | ||
"""Test partition function.""" | ||
assert partition(containers) == expected |
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,32 @@ | ||
- case: partition_result | ||
disable_cache: false | ||
main: | | ||
from typing import List | ||
from returns.result import Success, Failure, Result | ||
from returns.methods import partition | ||
x: List[Result[int, str]] | ||
reveal_type(partition(x)) # N: Revealed type is "Tuple[builtins.list[builtins.int], builtins.list[builtins.str]]" | ||
- case: partition_io_results | ||
disable_cache: false | ||
main: | | ||
from typing import Tuple | ||
from returns.result import Success, Failure | ||
from returns.methods import partition | ||
from returns.io import IO, IOResult, IOSuccess | ||
x: Tuple[IOResult[int, str], IOResult[int, str]] | ||
reveal_type(partition(x)) # N: Revealed type is "Tuple[builtins.list[returns.io.IO[builtins.int]], builtins.list[returns.io.IO[builtins.str]]]" | ||
- case: partition_maybe | ||
disable_cache: false | ||
main: | | ||
from typing import List, Tuple | ||
from returns.maybe import Maybe | ||
from returns.methods import partition | ||
x: List[Maybe[int]] | ||
reveal_type(partition(x)) # N: Revealed type is "Tuple[builtins.list[builtins.int], builtins.list[None]]" | ||