From 6d134cefe2dec697674f8d6c923efe321dfe9f90 Mon Sep 17 00:00:00 2001 From: sobolevn Date: Fri, 30 Aug 2019 14:33:42 +0300 Subject: [PATCH] Moves coalesce def into generated/ --- returns/converters.py | 69 +++------------------------------ returns/generated/coalesce.py | 73 +++++++++++++++++++++++++++++++++++ 2 files changed, 78 insertions(+), 64 deletions(-) create mode 100644 returns/generated/coalesce.py diff --git a/returns/converters.py b/returns/converters.py index 5666e121c..50db2ebcc 100644 --- a/returns/converters.py +++ b/returns/converters.py @@ -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( @@ -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 diff --git a/returns/generated/coalesce.py b/returns/generated/coalesce.py new file mode 100644 index 000000000..0c1cc8277 --- /dev/null +++ b/returns/generated/coalesce.py @@ -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