-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy path04_monads.py
More file actions
70 lines (47 loc) · 1.36 KB
/
04_monads.py
File metadata and controls
70 lines (47 loc) · 1.36 KB
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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
class Nothing:
def map(self, fn):
return Nothing()
def flatmap(self, fn):
return Nothing()
def __str__(self):
return "Nothing()"
class Just:
def __init__(self, val):
self.val = val
def map(self, fn):
return Just(fn(self.val))
def flatmap(self, fn):
return fn(self.val)
def __str__(self):
return f"Just({self.val})"
def inc(x):
return x + 1
def inverse(x):
if x != 0:
return Just(1/x)
return Nothing()
print(inverse(1).map(inc).flatmap(inverse).map(inc)) # Just(1.5)
print(inverse(-1).map(inc).flatmap(inverse).map(inc)) # Nothing()
from itertools import chain
class MultiValue:
def __init__(self, values):
self.values = set(values)
def map(self, fn):
out = {fn(val) for val in self.values}
return MultiValue(out)
def flatmap(self, fn):
out = (fn(val).values for val in self.values)
return MultiValue(chain(*out))
def __repr__(self):
return f'MultiValue({self.values})'
import math
def sqrt(x):
if x == 0:
return MultiValue({0})
y = math.sqrt(x)
return MultiValue({y, -y})
from operator import add
from functools import partial
add2 = partial(add, 2)
print(sqrt(4).map(add2).flatmap(sqrt).map(add2).flatmap(sqrt))
# MultiValue({0, 1.4142135623730951, 2.0, -2.0, -1.4142135623730951})