-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathhof.val
More file actions
58 lines (44 loc) · 666 Bytes
/
hof.val
File metadata and controls
58 lines (44 loc) · 666 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
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
fn map(l, f) {
result = []
for item in l {
result = append(result, f(item))
}
return result
}
fn double(x) {
return x * 2
}
fn even(x) {
return x % 2 == 0
}
fn filter(l, f) {
result = []
for item in l {
if (f(item)) {
result = append(result, item)
}
}
return result
}
fn reduce(l, f, initial) {
result = initial
for item in l {
result = f(result, item)
}
return result
}
fn sum(a, b) {
return a + b
}
fn max(a, b) {
if (a > b) {
return a
} else {
return b
}
}
l = [1, 2, 3, 4, 5]
println(map(l, double))
println(filter(l, even))
println(reduce(l, sum, 0))
println(reduce(l, max, l[0]))