-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheither.py
More file actions
30 lines (23 loc) · 862 Bytes
/
either.py
File metadata and controls
30 lines (23 loc) · 862 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
class Either:
def __init__(self, left, right):
self._left = left
self._right = right
@staticmethod
def of(left, right):
return Either(left, right)
def map(self, fn):
if self._right:
return Either.of(self._left, fn(self._right))
else:
return Either.of(self._left, self._right)
def __str__(self):
return f"{self.__class__.__name__}({self._left}, {self._right})"
def main():
print(Either.of("Error", None).map(lambda x: x + 1)) # Either("Error", None)
print(Either.of("Error", 0).map(lambda x: x + 1)) # Either("Error", 1)
print(Either.of("Error", 99).map(lambda x: x + 1)) # Either("Error", 100)
print(
Either.of("Error", 99).map(lambda x: x + 1).map(lambda x: x * x)
) # Either("Error", 10000)
if __name__ == "__main__":
main()