-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path10_closures.mno
More file actions
38 lines (34 loc) · 822 Bytes
/
Copy path10_closures.mno
File metadata and controls
38 lines (34 loc) · 822 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
# Lesson 10 — First-class functions and closures
# Lambdas capture by value; captured vars are read-only (mutate via struct/array).
fn map_ints(xs: [int], f: fn(int) -> int) -> [int] {
let out: [int] = []
for i in 0..len(xs) {
out = push(out, f(xs[i]))
}
return out
}
fn make_adder(n: int) -> fn(int) -> int {
return fn(x: int) -> int {
return x + n
}
}
fn main() {
let xs = [1, 2, 3]
let doubled = map_ints(xs, fn(x: int) -> int {
return x * 2
})
print(doubled[0])
print(doubled[2])
let add10 = make_adder(10)
print(add10(5))
}
test "closures" {
let add3 = make_adder(3)
assert add3(4) == 7
let xs = [1, 2]
let ys = map_ints(xs, fn(x: int) -> int {
return x + 1
})
assert ys[0] == 2
assert ys[1] == 3
}