- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecluttering-with-functional-programming.py
172 lines (119 loc) · 3.25 KB
/
decluttering-with-functional-programming.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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
defsquare(x):
returnx*x
# anonymous subroutine
anon_square=lambdax: x*x
list1=list((1,2,3)) #=> a list from a tuple
list2= [1,2,3]; #=> a list, because of the '[...]'
range1=range(1,11) #=> a range 1 .. 10
list3=list(range(1,11)); #=> a list from a range
tuple1=1,2,3; #=> a tuple
tuple2=tuple([1,2,3]) #=> a tuple from a list
tuple3=tuple(range(1,11)) #=> creates a tuple from a range
## A function, by any other name -- functions as values
defchoose (t, f, d):
ifd:
returnt
else:
returnf
tstr="True!"
fstr="False!"
res_str=choose(tstr,fstr,True)
print(res_str) #=> says "True!"
deftt(s):
print( "True "+s+"!")
defff(s):
print( "False"+s+"!")
res_f=choose(tt,ff,True)
print(res_f) #=> says <function tt at 0x7f829c3aa310>
res_f("rumour") #=> says "False rumour!"
## Functions don't need a name
tt=lambdas : print( "True "+s+"!" )
ff=lambdas : print( "False "+s+"!" )
res_f=choose(tt, ff, True);
print( res_f) #=> says <function <lambda> at 0x7f829b298b80>
res_f("story") #=> says "True story!"
## Examples: `map`, `grep` and `reduce`
### `map` : applying a function to all elements of a list
res=tuple( map( lambdax : x*x , range(1,11)))
res= []
forxinrange(1,11):
res.append(x*x)
### `filter` : filtering a list
res=tuple(filter( lambdax : x%5==0 ,range(1,31)))
res= []
forxinrange(1,31):
if (x%5==0):
res.append(x)
res=tuple(filter( lambdax : x%5==0 ,map( lambdax : x*x ,tuple(range(1,31)))))
### `reduce` : combining all elements of a list into a single value
fromfunctoolsimportreduce
sum=reduce(lambdaacc,elt: acc+elt, range(1,11))
print( sum); #=> says 55
### Writing your own
assoc_func=lambdax,y: x+y
non_assoc_func=lambdax,y: x+yifx<yelsex
#### Left fold
deffoldll (f, iacc, lst):
acc=iacc
foreltinlst:
acc=f(acc,elt)
returnacc
deffoldl (f, acc, lst):
iflst== ():
returnacc
else:
# Python's way of splitting a tuple in the first elt and the rest
# rest will be a list, not a tuple, but we'll let that pass
(elt,*rest) =lst
# The actual recursion
returnfoldl( f, f(acc, elt), rest)
#### Right fold
deffoldlr (f, iacc, lst):
acc=iacc
foreltinlst.reverse():
acc=f(acc,elt)
returnacc
deffoldr (f, acc, lst):
iflst== ():
returnacc
else:
(*rest,elt) =lst
returnfoldr( f, f(acc, elt), rest)
#### `map` and `grep` are folds
defmap_ (f,lst):
returnfoldl(
lambdaacc,elt:(*acc, f(elt))
,()
,lst
)
deffilter_ (f,lst):
returnfoldl(
lambdaacc,elt:
(*acc,elt) iff(elt) elseacc
, (), lst)
## Functions returning functions
defadd_1 (x) : returnx+1
defadd_2 (x) : returnx+2
defadd_3 (x) : returnx+3
defadd_4 (x) : returnx+4
defadd_5 (x) : returnx+5
print( add_1(4)) #=> says 5
add= (
lambdax : x+1,
lambdax : x+2,
lambdax : x+3,
lambdax : x+4,
lambdax : x+5
)
print( add[0](4)) #=> says 5
add= []
forninrange(0,6):
add.append(lambdax: x+n)
defgen_add(n):
returnlambdax : x+n
add=tuple(map( gen_add, range(0,6)))
print( add[1](4)) #=> says 5
## Function composition
res_chain=map( lambdax : x+5, map( lambdax : x*x, range(1,31)))
defcompose(f,g):
returnlambdax: f(g(x))