Skip to content

Commit c75f04c

Browse files
Bind filter to maybe n
1 parent 4ca8bbf commit c75f04c

File tree

2 files changed

+30
-15
lines changed

2 files changed

+30
-15
lines changed

returns/interfaces/filterable.py

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,17 @@
11
from abc import abstractmethod
2-
from typing import Callable, Generic, TypeVar
2+
from typing import Callable, NoReturn, TypeVar
33

4+
from returns.interfaces.specific.maybe import MaybeLikeN
45
from returns.primitives.hkt import Kind1
56

6-
_InnerType = TypeVar('_InnerType')
7+
_FirstType = TypeVar('_FirstType')
8+
_SecondType = TypeVar('_SecondType')
9+
_ThirdType = TypeVar('_ThirdType')
710

8-
_FilterableType = TypeVar('_FilterableType', bound='Filterable')
11+
_FilterableType = TypeVar('_FilterableType', bound='FilterableN')
912

1013

11-
class Filterable(Generic[_InnerType]):
14+
class FilterableN(MaybeLikeN[_FirstType, _SecondType, _ThirdType]):
1215
"""
1316
Represents container that can apply filter over inner value.
1417
@@ -36,6 +39,16 @@ class Filterable(Generic[_InnerType]):
3639
@abstractmethod
3740
def filter(
3841
self: _FilterableType,
39-
predicate: Callable[[_InnerType], bool],
40-
) -> Kind1[_FilterableType, _InnerType]:
42+
predicate: Callable[[_FirstType], bool],
43+
) -> Kind1[_FilterableType, _FirstType]:
4144
"""Applies 'predicate' to the result fo a previous computation."""
45+
46+
47+
#: Type alias for kinds with one type argument.
48+
Filterable1 = FilterableN[_FirstType, NoReturn, NoReturn]
49+
50+
#: Type alias for kinds with two type arguments.
51+
Filterable2 = FilterableN[_FirstType, _SecondType, NoReturn]
52+
53+
#: Type alias for kinds with three type arguments.
54+
Filterable3 = FilterableN[_FirstType, _SecondType, _ThirdType]

returns/pointfree/filter.py

Lines changed: 11 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,19 @@
11
from typing import Callable, TypeVar
22

3-
from returns.interfaces.filterable import Filterable
4-
from returns.primitives.hkt import Kind1, Kinded, kinded
3+
from returns.interfaces.filterable import FilterableN
4+
from returns.primitives.hkt import Kinded, KindN, kinded
55

6-
_InnerType = TypeVar('_InnerType')
7-
_FilterableKind = TypeVar('_FilterableKind', bound=Filterable)
6+
_FirstType = TypeVar('_FirstType')
7+
_SecondType = TypeVar('_SecondType')
8+
_ThirdType = TypeVar('_ThirdType')
9+
_FilterableKind = TypeVar('_FilterableKind', bound=FilterableN)
810

911

1012
def filter_(
11-
predicate: Callable[[_InnerType], bool],
13+
predicate: Callable[[_FirstType], bool],
1214
) -> Kinded[Callable[
13-
[Kind1[_FilterableKind, _InnerType]],
14-
Kind1[_FilterableKind, _InnerType],
15+
[KindN[_FilterableKind, _FirstType, _SecondType, _ThirdType]],
16+
KindN[_FilterableKind, _FirstType, _SecondType, _ThirdType],
1517
]]:
1618
"""
1719
Applies predicate over container.
@@ -32,8 +34,8 @@ def filter_(
3234

3335
@kinded
3436
def factory(
35-
container: Kind1[_FilterableKind, _InnerType],
36-
) -> Kind1[_FilterableKind, _InnerType]:
37+
container: KindN[_FilterableKind, _FirstType, _SecondType, _ThirdType],
38+
) -> KindN[_FilterableKind, _FirstType, _SecondType, _ThirdType]:
3739
return container.filter(predicate)
3840

3941
return factory

0 commit comments

Comments
 (0)