Skip to content

Commit

Permalink
missing file
Browse files Browse the repository at this point in the history
  • Loading branch information
roman matveev committed Aug 9, 2024
1 parent 77f7a33 commit b7a8f56
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
38 changes: 38 additions & 0 deletions returns/methods/partition.py
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
3 changes: 1 addition & 2 deletions tests/test_result/test_result_partition.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@

import pytest

from returns.future import IOResult
from returns.io import IO
from returns.io import IO, IOResult
from returns.methods import partition
from returns.result import Failure, Success

Expand Down

0 comments on commit b7a8f56

Please sign in to comment.