Skip to content

Commit e4800fc

Browse files
committed
Closes #252
1 parent 476914b commit e4800fc

File tree

6 files changed

+58
-54
lines changed

6 files changed

+58
-54
lines changed

docs/pages/converters.rst

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -53,19 +53,19 @@ You can also use ``flatten`` to merge nested containers together:
5353
>>> assert flatten(Success(Success(1))) == Success(1)
5454
5555
56-
fold
57-
----
56+
coalesce
57+
--------
5858

59-
You can use :func:`returns.converters.fold_result`
60-
and :func:`returns.converters.fold_maybe` converters
59+
You can use :func:`returns.converters.coalesce_result`
60+
and :func:`returns.converters.coalesce_maybe` converters
6161
to covert containers to a regular value.
6262

6363
These functions accept two functions:
6464
one for successful case, one for failing case.
6565

6666
.. code:: python
6767
68-
>>> from returns.converters import fold_result
68+
>>> from returns.converters import coalesce_result
6969
>>> from returns.result import Success, Failure
7070
7171
>>> def handle_success(state: int) -> float:
@@ -74,9 +74,9 @@ one for successful case, one for failing case.
7474
>>> def handle_failure(state: str) -> float:
7575
... return 0.0
7676
77-
>>> fold_result(handle_success, handle_failure)(Success(1))
77+
>>> coalesce_result(handle_success, handle_failure)(Success(1))
7878
0.5
79-
>>> fold_result(handle_success, handle_failure)(Failure(1))
79+
>>> coalesce_result(handle_success, handle_failure)(Failure(1))
8080
0.0
8181
8282

returns/_generated/fold.py renamed to returns/_generated/coalesce.py

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,34 +7,34 @@
77
and second one for failed containers like ``Nothing`` and ``Failure``.
88
99
This function is useful when you need
10-
to fold two possible container states into one type.
10+
to coalesce two possible container states into one type.
1111
1212
Note::
13-
``IOResult`` only fold to ``IO`` values.
13+
``IOResult`` only coalesce to ``IO`` values.
1414
It is impossible to unwrap ``IO`` here.
1515
1616
.. code:: python
1717
18-
>>> from returns.converters import fold_result
18+
>>> from returns.converters import coalesce_result
1919
>>> from returns.result import Success, Failure
2020
>>> f1 = lambda x: x + 1
2121
>>> f2 = lambda y: y + 'b'
22-
>>> assert fold_result(f1, f2)(Success(1)) == 2
23-
>>> assert fold_result(f1, f2)(Failure('a')) == 'ab'
22+
>>> assert coalesce_result(f1, f2)(Success(1)) == 2
23+
>>> assert coalesce_result(f1, f2)(Failure('a')) == 'ab'
2424
25-
>>> from returns.converters import fold_ioresult
25+
>>> from returns.converters import coalesce_ioresult
2626
>>> from returns.io import IO, IOSuccess, IOFailure
2727
>>> f1 = lambda x: x.map(lambda state: state + 1)
2828
>>> f2 = lambda y: y.map(lambda state: state + 'b')
29-
>>> assert fold_result(f1, f2)(IOSuccess(1)) == IO(2)
30-
>>> assert fold_result(f1, f2)(IOFailure('a')) == IO('ab')
29+
>>> assert coalesce_result(f1, f2)(IOSuccess(1)) == IO(2)
30+
>>> assert coalesce_result(f1, f2)(IOFailure('a')) == IO('ab')
3131
32-
>>> from returns.converters import fold_maybe
32+
>>> from returns.converters import coalesce_maybe
3333
>>> from returns.maybe import Some, Nothing
3434
>>> f1 = lambda x: x + 1
3535
>>> f2 = lambda _: 'a'
36-
>>> assert fold_maybe(f1, f2)(Some(1)) == 2
37-
>>> assert fold_maybe(f1, f2)(Nothing) == 'a'
36+
>>> assert coalesce_maybe(f1, f2)(Some(1)) == 2
37+
>>> assert coalesce_maybe(f1, f2)(Nothing) == 'a'
3838
3939
"""
4040

@@ -66,29 +66,29 @@ def decorator(container):
6666
return decorator
6767

6868

69-
_fold_result: Callable[
69+
_coalesce_result: Callable[
7070
[
7171
Callable[[_ValueType], _FirstType],
7272
Callable[[_ErrorType], _FirstType],
7373
],
7474
Callable[[Result[_ValueType, _ErrorType]], _FirstType],
7575
] = _fold
76-
_fold_result.__doc__ = __doc__
76+
_coalesce_result.__doc__ = __doc__
7777

78-
_fold_ioresult: Callable[
78+
_coalesce_ioresult: Callable[
7979
[
8080
Callable[[IO[_ValueType]], IO[_FirstType]],
8181
Callable[[IO[_ErrorType]], IO[_FirstType]],
8282
],
8383
Callable[[IOResult[_ValueType, _ErrorType]], IO[_FirstType]],
8484
] = _fold
85-
_fold_ioresult.__doc__ = __doc__
85+
_coalesce_ioresult.__doc__ = __doc__
8686

87-
_fold_maybe: Callable[
87+
_coalesce_maybe: Callable[
8888
[
8989
Callable[[_ValueType], _FirstType],
9090
Callable[[None], _FirstType],
9191
],
9292
Callable[[Maybe[_ValueType]], _FirstType],
9393
] = _fold
94-
_fold_maybe.__doc__ = __doc__
94+
_coalesce_maybe.__doc__ = __doc__

returns/converters.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
from typing import TypeVar
44

5-
from returns._generated import fold
5+
from returns._generated import coalesce
66
from returns._generated.flatten import _flatten as flatten # noqa: F401
77
from returns.maybe import Maybe
88
from returns.result import Failure, Result, Success
@@ -12,9 +12,9 @@
1212
_ErrorType = TypeVar('_ErrorType')
1313

1414
# Re-export from generated:
15-
fold_maybe = fold._fold_maybe # noqa: WPS437
16-
fold_result = fold._fold_result # noqa: WPS437
17-
fold_ioresult = fold._fold_ioresult # noqa: WPS437
15+
coalesce_maybe = coalesce._coalesce_maybe # noqa: WPS437
16+
coalesce_result = coalesce._coalesce_result # noqa: WPS437
17+
coalesce_ioresult = coalesce._coalesce_ioresult # noqa: WPS437
1818

1919

2020
def result_to_maybe(

returns/functions.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -22,17 +22,17 @@ def identity(instance: _FirstType) -> _FirstType:
2222
Why do we even need this?
2323
Identity functions help us with the composition.
2424
25-
Imagine, that you want to use :func:`returns.converters.fold_result`
25+
Imagine, that you want to use :func:`returns.converters.coalesce_result`
2626
like so:
2727
2828
.. code:: python
2929
3030
from returns.result import Result
31-
from returns.converters import fold_result
31+
from returns.converters import coalesce_result
3232
3333
numbers: Result[int, float]
34-
# Now you want to fold `number` into `int` type:
35-
number: int = fold_result(identity, int)(numbers)
34+
# Now you want to coalesce `number` into `int` type:
35+
number: int = coalesce_result(identity, int)(numbers)
3636
# Done!
3737
3838
See also:
Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,10 @@
11
# -*- coding: utf-8 -*-
22

3-
from returns.converters import fold_ioresult, fold_maybe, fold_result
3+
from returns.converters import (
4+
coalesce_ioresult,
5+
coalesce_maybe,
6+
coalesce_result,
7+
)
48
from returns.io import IO, IOFailure, IOSuccess
59
from returns.maybe import Nothing, Some
610
from returns.result import Failure, Success
@@ -14,27 +18,27 @@ def _failed_case(_) -> int:
1418
return 0
1519

1620

17-
_result_converter = fold_result(_success_case, _failed_case)
18-
_maybe_converter = fold_maybe(_success_case, _failed_case)
19-
_ioresult_converter = fold_ioresult(
21+
_result_converter = coalesce_result(_success_case, _failed_case)
22+
_maybe_converter = coalesce_maybe(_success_case, _failed_case)
23+
_ioresult_converter = coalesce_ioresult(
2024
IO.lift(_success_case),
2125
IO.lift(_failed_case),
2226
)
2327

2428

25-
def test_fold_result():
26-
"""Ensures that `fold` is always returning the correct type."""
29+
def test_coalesce_result():
30+
"""Ensures that `coalesce` is always returning the correct type."""
2731
assert _result_converter(Success(1)) == 2
2832
assert _result_converter(Failure(1)) == 0
2933

3034

31-
def test_fold_ioresult():
32-
"""Ensures that `fold` is always returning the correct type."""
35+
def test_coalesce_ioresult():
36+
"""Ensures that `coalesce` is always returning the correct type."""
3337
assert _ioresult_converter(IOSuccess(1)) == IO(2)
3438
assert _ioresult_converter(IOFailure(1)) == IO(0)
3539

3640

37-
def test_fold_maybe():
38-
"""Ensures that `fold` is always returning the correct type."""
41+
def test_coalesce_maybe():
42+
"""Ensures that `coalesce` is always returning the correct type."""
3943
assert _maybe_converter(Some(2)) == 3
4044
assert _maybe_converter(Nothing) == 0
Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
- case: fold_result
1+
- case: coalesce_result
22
disable_cache: true
33
main: |
4-
from returns.converters import fold_result
4+
from returns.converters import coalesce_result
55
from returns.result import Result
66
77
def first(arg: int) -> float:
@@ -11,13 +11,13 @@
1111
...
1212
1313
container: Result[int, str]
14-
reveal_type(fold_result(first, second)(container)) # N: Revealed type is 'builtins.float*'
14+
reveal_type(coalesce_result(first, second)(container)) # N: Revealed type is 'builtins.float*'
1515
1616
17-
- case: fold_ioresult_correct
17+
- case: coalesce_ioresult_correct
1818
disable_cache: true
1919
main: |
20-
from returns.converters import fold_ioresult
20+
from returns.converters import coalesce_ioresult
2121
from returns.io import IO, IOResult
2222
2323
def first(arg: IO[int]) -> IO[float]:
@@ -27,13 +27,13 @@
2727
...
2828
2929
container: IOResult[int, str]
30-
reveal_type(fold_ioresult(first, second)(container)) # N: Revealed type is 'returns.io.IO[builtins.float*]'
30+
reveal_type(coalesce_ioresult(first, second)(container)) # N: Revealed type is 'returns.io.IO[builtins.float*]'
3131
3232
33-
- case: fold_ioresult_wrong
33+
- case: coalesce_ioresult_wrong
3434
disable_cache: true
3535
main: |
36-
from returns.converters import fold_ioresult
36+
from returns.converters import coalesce_ioresult
3737
from returns.io import IO, IOResult
3838
3939
def first(arg: int) -> float:
@@ -43,17 +43,17 @@
4343
...
4444
4545
container: IOResult[int, str]
46-
fold_ioresult(first, second)(container)
46+
coalesce_ioresult(first, second)(container)
4747
out: |
4848
main:11: error: Argument 1 has incompatible type "Callable[[int], float]"; expected "Callable[[IO[<nothing>]], IO[<nothing>]]"
4949
main:11: error: Argument 1 has incompatible type "IOResult[int, str]"; expected "IOResult[<nothing>, <nothing>]"
5050
main:11: error: Argument 2 has incompatible type "Callable[[str], float]"; expected "Callable[[IO[<nothing>]], IO[<nothing>]]"
5151
5252
53-
- case: fold_maybe
53+
- case: coalesce_maybe
5454
disable_cache: true
5555
main: |
56-
from returns.converters import fold_maybe
56+
from returns.converters import coalesce_maybe
5757
from returns.maybe import Maybe
5858
5959
def first(arg: int) -> float:
@@ -63,4 +63,4 @@
6363
...
6464
6565
container: Maybe[int]
66-
reveal_type(fold_maybe(first, second)(container)) # N: Revealed type is 'builtins.float*'
66+
reveal_type(coalesce_maybe(first, second)(container)) # N: Revealed type is 'builtins.float*'

0 commit comments

Comments
 (0)