-
Notifications
You must be signed in to change notification settings - Fork 9
/
heap_fun.nt
52 lines (44 loc) · 1019 Bytes
/
heap_fun.nt
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
module heap_fun;
import neat.runtime : assert, print;
void main() {
{
int i = 5;
auto lambda = () => i;
assert(lambda() == i);
}
{
int i = 5;
auto lambda = new () => i;
assert(lambda() == i);
}
test_delegates;
test_lambdas;
}
void test_delegates() {
mut int delegate()[] dgs;
int delegate() test(int i) {
int nest() => i;
return new &nest;
}
for (i in 0 .. 10) dgs ~= test(i);
for (i in 0 .. 10) assert(dgs[i]() == i);
}
void test_lambdas() {
mut Producer!int[] lambdas;
Producer!int test(int i) {
return lambdaAction(new () => i);
}
for (i in 0 .. 10) lambdas ~= test(i);
for (i in 0 .. 10) assert(lambdas[i].call() == i);
}
interface Producer(T) {
T call();
}
LambdaAction!T lambdaAction(T)(T lambda) {
return new LambdaAction!T(lambda);
}
class LambdaAction(T) : Producer!int {
T lambda;
this(this.lambda) {}
override int call() { return lambda(); }
}