1
1
from __future__ import annotations
2
2
3
- from typing import Any , Callable , Generic , TypeVar
3
+ from typing import Any , Callable , Generic , Tuple , TypeVar
4
4
5
5
from typing_extensions import Protocol
6
6
7
7
from boiling_learning .io import json
8
8
from boiling_learning .utils .descriptions import describe
9
9
from boiling_learning .utils .functional import Pack
10
+ from boiling_learning .utils .lazy import Lazy
10
11
11
12
_X_contra = TypeVar ('_X_contra' , contravariant = True )
12
13
_Y_co = TypeVar ('_Y_co' , covariant = True )
@@ -31,6 +32,29 @@ def __str__(self) -> str:
31
32
arguments = f'{ args } , { kwargs } ' if args and kwargs else args or kwargs
32
33
return f'<{ self .__class__ .__name__ } ({ arguments } )>'
33
34
35
+ def __ror__ (self , arg : _X ) -> _Y :
36
+ return _Transformed (arg , self )
37
+
38
+
39
+ class _Transformed (Lazy [_Y ]):
40
+ def __init__ (self , arg : _X , transformer : Transformer [_X , _Y ]) -> None :
41
+ if isinstance (arg , _Transformed ):
42
+ super ().__init__ (lambda : transformer (arg ()))
43
+ else :
44
+ super ().__init__ (lambda : transformer (arg ))
45
+
46
+ self ._arg = arg
47
+ self ._transformer = transformer
48
+
49
+ def __describe__ (self ) -> json .JSONDataType :
50
+ return describe (self ._pipe ())
51
+
52
+ def _pipe (self ) -> Tuple [Any , ...]:
53
+ if isinstance (self ._arg , _Transformed ):
54
+ return (* self ._arg ._pipe (), self ._transformer ) # pylint: disable=protected-access
55
+ else :
56
+ return (self ._arg , self ._transformer )
57
+
34
58
35
59
# the concept of mathematical operator as a function mapping a set to itself
36
60
Operator = Transformer [_X , _X ]
0 commit comments