Skip to content

Commit

Permalink
Moves coalesce def into generated/
Browse files Browse the repository at this point in the history
  • Loading branch information
sobolevn committed Aug 30, 2019
1 parent 2230995 commit 6d134ce
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 64 deletions.
69 changes: 5 additions & 64 deletions returns/converters.py
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
# -*- coding: utf-8 -*-

from typing import Callable, TypeVar, overload
from typing import TypeVar, overload

from returns.generated import coalesce
from returns.io import IO
from returns.maybe import Maybe
from returns.pipeline import is_successful
from returns.result import Failure, Result, Success

# Contianer internals:
_ValueType = TypeVar('_ValueType')
_ErrorType = TypeVar('_ErrorType')

# Aliases:
_FirstType = TypeVar('_FirstType')
# Re-export from generated:
coalesce_maybe = coalesce._coalesce_maybe # noqa: WPS437
coalesce_result = coalesce._coalesce_result # noqa: WPS437


def result_to_maybe(
Expand Down Expand Up @@ -95,63 +96,3 @@ def join(container):
"""
return container._inner_value # noqa: WPS437


_coalesce_doc = """
Accepts two functions that handle different cases of containers.
First one handles successful containers like ``Some`` and ``Success``,
and second one for failed containers like ``Nothing`` and ``Failure``.
This function is useful when you need
to coalesce two possible container states into one type.
"""


def _coalesce(success_handler, failure_handler):
"""
We need this function, because we cannot use a single typed function.
.. code:: python
>>> from returns.result import Success, Failure
>>> f1 = lambda x: x + 1
>>> f2 = lambda y: y + 'b'
>>> coalesce_result(f1, f2)(Success(1)) == 2
True
>>> coalesce_result(f1, f2)(Failure('a')) == 'ab'
True
>>> from returns.maybe import Some, Nothing
>>> f1 = lambda x: x + 1
>>> f2 = lambda _: 'a'
>>> coalesce_maybe(f1, f2)(Some(1)) == 2
True
>>> coalesce_maybe(f1, f2)(Nothing) == 'a'
True
"""
def decorator(container):
if is_successful(container):
return success_handler(container.unwrap())
return failure_handler(container.failure())
return decorator


coalesce_result: Callable[
[
Callable[[_ValueType], _FirstType],
Callable[[_ErrorType], _FirstType],
],
Callable[[Result[_ValueType, _ErrorType]], _FirstType],
] = _coalesce
coalesce_result.__doc__ = _coalesce_doc

coalesce_maybe: Callable[
[
Callable[[_ValueType], _FirstType],
Callable[[None], _FirstType],
],
Callable[[Maybe[_ValueType]], _FirstType],
] = _coalesce
coalesce_maybe.__doc__ = _coalesce_doc
73 changes: 73 additions & 0 deletions returns/generated/coalesce.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
# -*- coding: utf-8 -*-

from typing import Callable, TypeVar

from returns.maybe import Maybe
from returns.pipeline import is_successful
from returns.result import Result

# Contianer internals:
_ValueType = TypeVar('_ValueType')
_ErrorType = TypeVar('_ErrorType')

# Aliases:
_FirstType = TypeVar('_FirstType')

_coalesce_doc = """
Accepts two functions that handle different cases of containers.
First one handles successful containers like ``Some`` and ``Success``,
and second one for failed containers like ``Nothing`` and ``Failure``.
This function is useful when you need
to coalesce two possible container states into one type.
"""


def _coalesce(success_handler, failure_handler):
"""
We need this function, because we cannot use a single typed function.
.. code:: python
>>> from returns.result import Success, Failure
>>> f1 = lambda x: x + 1
>>> f2 = lambda y: y + 'b'
>>> _coalesce_result(f1, f2)(Success(1)) == 2
True
>>> _coalesce_result(f1, f2)(Failure('a')) == 'ab'
True
>>> from returns.maybe import Some, Nothing
>>> f1 = lambda x: x + 1
>>> f2 = lambda _: 'a'
>>> _coalesce_maybe(f1, f2)(Some(1)) == 2
True
>>> _coalesce_maybe(f1, f2)(Nothing) == 'a'
True
"""
def decorator(container):
if is_successful(container):
return success_handler(container.unwrap())
return failure_handler(container.failure())
return decorator


_coalesce_result: Callable[
[
Callable[[_ValueType], _FirstType],
Callable[[_ErrorType], _FirstType],
],
Callable[[Result[_ValueType, _ErrorType]], _FirstType],
] = _coalesce
_coalesce_result.__doc__ = _coalesce_doc

_coalesce_maybe: Callable[
[
Callable[[_ValueType], _FirstType],
Callable[[None], _FirstType],
],
Callable[[Maybe[_ValueType]], _FirstType],
] = _coalesce
_coalesce_maybe.__doc__ = _coalesce_doc

0 comments on commit 6d134ce

Please sign in to comment.