-
Notifications
You must be signed in to change notification settings - Fork 1
/
Either.py
48 lines (38 loc) · 1.04 KB
/
Either.py
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
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
class Either(object):
def __init__(self, value, is_right):
self.is_right = is_right
self.value = value
@staticmethod
def right(value):
return Either(value, True)
@staticmethod
def left(value):
return Either(value, False)
def map(self, mapping):
if self.is_right:
return Either.right(mapping(self.value))
else:
return Either.left(self.value)
def bind(self, binding):
if self.is_right:
return binding(self.value)
else:
return Either.left(self.value)
def __str__(self):
if self.is_right:
return "Right " + str(self.value)
else:
return "Left " + str(self.value)
right_two = Either.right(2)
print(right_two)
right_four = right_two.map(lambda x: x + 2)
print(right_four)
def divide (x,y):
if y == 0:
return Either.left("Cannot divide by 0")
else:
return Either.right(x / y)
right_one = right_four.bind(lambda x: divide(x, 4))
print(right_one)
div_by_zero = right_four.bind(lambda x: divide(x, 0))
print(div_by_zero)