- Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdecluttering-with-functional-programming.raku
229 lines (161 loc) · 3.85 KB
/
decluttering-with-functional-programming.raku
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
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
usev6;
## Raku: a quick introduction
subsquare ($x) {
$x*$x;
}
# anonymous subroutine
my$anon_square= sub ($x) {
$x*$x;
}
my \x=42; # sigilless
my$y=43;
sayx+$y;
my@array1=1,2,3; #=> an array because of the '@' sigil
my \array2 = [1,2,3]; #=> an array, because of the '[...]'
my \range1 =1..10; #=> a range 1 .. 10
my@array3=1..10; #=> an array from a range, because of the '@' sigil
my \list1 =1,2,3; #=> a list
my$list2= (1,2,3); #=> also a list
my \list3 =|(1..10); #=> an array from a range because of the '|' flattening operation
## A function, by any other name -- functions as values
subchoose (\t, \f, \d) {
if (d) {t} else {f}
}
# Raku
my \tstr ="True!";
my \fstr ="False!";
my \res_str = choose(tstr, fstr, True);
say res_str; #=> says "True!"
subtt (\s) { say"True {s}!" }
subff (\s) { say"False {s}!" }
my&res_f= choose(&tt, &ff, False);
say&res_f; #=> says &ff
res_f("rumour"); #=> says "False rumour!"
## Functions don't need a name
my \tt = sub (\s) { say"True {s}!" };
my \ff=-> \s { say"False {s}!" };
my&res_ff= choose(tt, ff, True);
say&res_ff; #=> says sub { }
res_ff("story"); #=> says "True story!"
## Examples: `map`, `grep` and `reduce`
### `map` : applying a function to all elements of a list
my \res =map-> \x {x*x} , 1..10;
# Raku
my \res_mut = [];
for1..10-> \x {
res_mut.push(x*x);
}
### `grep` : filtering a list
my \res_grep =grep-> \x { x%5==0 }, 1..30;
#
my \res_grep_mut = [];
for1..30-> \x {
if (x%5==0) {
res_grep_mut.push(x);
}
}
my \res_chain =grep-> \x { x%5==0 }, map-> \x {x*x}, 1..30;
say res_chain;
### `reduce` : combining all elements of a list into a single value
my \sum =reduce sub (\acc,\elt) {acc+elt}, 1..10;
saysum; #=> says 55
### Writing your own
my \assoc_func =-> \x,\y {x+y}
my \non_assoc_func =-> \x,\y { x< y ??x+y !!x }
#### Left fold
subfoldll (&f, \iacc, \lst) {
my$acc= iacc;
for lst -> \elt {
$acc= f($acc,elt);
}
$acc;
}
# # When the list is empty, return the accumulator
multisubfoldl (&f, \acc, ()) { acc }
multisubfoldl (&f, \acc, \lst) {
#'s way of splitting a list in the first elt and the rest
# The '*' is a shorthand for the end of the list
# my (\elt,\rest) = lst[0, 1 .. Inf ];
my \elt = lst[0];
my \rest = lst[1..Inf];
# The actual recursion
foldl( &f, f(acc, elt), rest);
}
#### Right fold
subfoldrl (&f, \acc, \lst) {
my$res= acc;
for lst.reverse-> \elt {
$res= f($res,elt);
}
$res;
}
#
multisubfoldr ( &f, \acc, ()) { acc }
multisubfoldr (&f, \acc, \lst) {
my (\rest,\elt) = lst[0..^*-1, * ];
foldr( &f, f(acc, elt), rest);
}
#### `map` and `grep` are folds
#
submap_ (&f,\lst) {
foldl( sub (\acc,\elt) {
(|acc,f(elt))
}, (), lst);
}
#
subgrep_ (&f,\lst) {
foldl( sub (\acc,\elt) {
if (f(elt)) {
(|acc,elt)
} else {
acc
}
}, (), lst);
}
## Functions returning functions
# Raku
subadd_1 (\x) {x+1}
subadd_2 (\x) {x+2}
subadd_3 (\x) {x+3}
subadd_4 (\x) {x+4}
subadd_5 (\x) {x+5}
say add_1(4); #=> says 5
# Raku
my \add_n =
sub (\x) {x},
sub (\x) {x+1},
sub (\x) {x+2},
sub (\x) {x+3},
sub (\x) {x+4},
sub (\x) {x+5};
say add_n[0].(4); #=> says 5
# Raku
my \add_mut = [];
for0..5-> \n {
add_mut.push(sub (\x) {x+n});
}
say add_mut[1].(4); #=> says 5
# Raku
subgen_add(\n) {
sub (\x) {x+n}
}
my \add_i =map&gen_add, 0..5;
say add_i[1].(4); #=> says 5
### Laziness
# Raku
my \add =map&gen_add, 0.. ∞;
say add[244].(7124); #=> says 7368
## Function composition
# Raku
my \res_map_chain =map-> \x { x+5 }, map-> \x {x*x}, 1..30;
my \res_map_comp =map-> \x { x+5 } ∘-> \x { x*x }, 1..30;
subf {};
subg {};
my&h=&f∘&g;
subh_ (\x) {
f(g(x))
}
# Raku
subcompose(&f,&g) {
sub (\x) { f(g(x)) }
}