File: lambda.bs

package info (click to toggle)
storm-lang 0.7.5-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 52,028 kB
  • sloc: ansic: 261,471; cpp: 140,432; sh: 14,891; perl: 9,846; python: 2,525; lisp: 2,504; asm: 860; makefile: 678; pascal: 70; java: 52; xml: 37; awk: 12
file content (92 lines) | stat: -rw-r--r-- 1,274 bytes parent folder | download | duplicates (2)
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

Int testLambda() {
	var fn = (Int x) => x + 3;
	fn.call(10);
}

Int callLambda(fn(Int)->Int x) {
	x.call(10);
}

Int testLambdaParam() {
	callLambda((a) => a + 5);
}

Int testLambdaVar() {
	fn(Int)->Int x = (a) => a + 20;
	x.call(12);
}

Int testLambdaCapture() {
	Int outside = 20;
	callLambda((x) => x + outside);
}

Int testLambdaCapture2() {
	Int outside = 20;
	var foo = (Int x) => x + outside;
	foo.call(33);
}


fn->Int createCounter() {
	Int now = 0;
	() => now++;
}

Str testLambdaMemory() {
	var a = createCounter();
	var b = createCounter();

	Int[] result;
	result << a.call();
	result << a.call();
	result << a.call();
	result << b.call();
	result << b.call();
	result << a.call();

	result.toS();
}

class LambdaEnv {
	Int x;

	init() {
		init { x = 7; }
	}

	fn(Int)->Int captureImplicit() {
		return (Int a) => a + x;
	}

	fn(Int)->Int captureExplicit() {
		return (Int a) => a + this.x;
	}
}

Int testLambdaThisImplicit() {
	LambdaEnv e;
	var fn = e.captureImplicit();
	fn.call(13);
}

Int testLambdaThisExplicit() {
	LambdaEnv e;
	var fn = e.captureExplicit();
	fn.call(13);
}

Int testLambdaExplicitReturn() {
	var x = (Int x) => {
		if (x > 10) {
			return 75;
		} else if (x < 0) {
			return 35;
		} else {
			200;
		}
	};

	x.call(20) + x.call(-1);
}