Code Bucket …

Rho Lambdas

You can visit http://www.the-barn.org/codebucket.php?id=68 to view this snippet directly.

 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
// test lambda functionality
//
// all methods and functions are implemented with lambdas, which themselves are
// based on continuations
//
// lambda syntax and semantics closely resemble the implementation in C++0x

{
	// define and invoke an empty lambda. same as C++0x
	[](){}();

	// assign a lambda to a variable, invoke it
	`a := [](){};
	a();

	// create a lambda, invoke it, and store result to a
	`a := []() { return 1 + 2; }();
	assert(a == 3);

	// create a lambda, call it b
	`b := [](x,y) { return x + y; };

	// invoke b given two arguments
	assert(b(5, 6) == 11);
	assert(b("foo", "bar") == "foobar");

	// create a lambda that `captures` c at the point of invocation, and also takes two arguments
	`c := 1;
	`d := [c](a,b)
	{
		return c + a + b;
	};
	assert(d(2, 3) == 6);
	assert(d(4, 5) == 10);

	// d will re-capture c
	c = 2;
	assert(d(4, 5) == 11);

	// this function captures q and adds it to itself a given number of times
	`q := "foo";
	`c := [q](a)
	{
		for (`n := 0; n < a; ++n)
		{
			q += q;
		}
		return q;
	};
	assert(c(3) == "foofoofoofoofoofoofoofoo");
	assert(c(2) == "foofoofoofoo");
	assert(c(1) == "foofoo");
	assert(c(0) == "foo");

	// pass a lamba to a lamba
	`w1 := [](a) { return a(2, 3); };
	assert(w1([](x,y) { return x + y; }) == 5);
	assert(w1([](x,y) { return x * y; }) == 6);

	// more elaborate examples:
	`w2 := [](a,b) { return a(b); };
	assert(w2([](x){return x*x;}, 3) == 9);

	 same for this...
	`e := [c](a,b)
	{
		`q := "grok";
		return a("spam") + c(b);
	};
	`f := e([](x){ return x+"bar";  }, 3);
	assert(f == "spambargrokgrokgrok");
}

//EOF
  • Posted on 03.06.2009 at 2:38 PM by spam
  • Language: C++

Snippets

 
36 Results
Page 1 of 3
TitleLanguagePosted ByPostedExpiresActions
Python first-class classes for KodenPythonstoffle04.07.2010 8:19 AM (64 days 3h 49m ago)-view
mLAN Activation ScriptPlain Textstoffle23.06.2010 6:42 AM (75 days 5h 26m ago)-view
areaC#stoffle27.02.2010 3:51 AM (191 days 9h 17m ago)-view
list extensionsC#stoffle09.02.2010 11:12 PM (208 days 13h 56m ago)-view
possible IComponent intererface version 95C#stoffle09.02.2010 11:04 PM (208 days 14h 4m ago)-view
Render SystemC++spam05.06.2009 12:14 PM (457 days 23h 54m ago)-view
Effect SampleC++spam03.06.2009 2:40 PM (459 days 21h 28m ago)-view
Rho LambdasC++spam03.06.2009 2:38 PM (459 days 21h 30m ago)-view
Example VSM implC++spam02.06.2009 1:21 PM (460 days 22h 47m ago)-view
Vector3 Interface (public)C++kalin11.02.2008 9:53 PM (937 days 15h 15m ago)-view
TimesTablesC++kalin09.02.2008 6:04 PM (939 days 19h 4m ago)-view
Skruje Account Search PagePHPstoffle03.02.2008 5:12 PM (945 days 19h 56m ago)-view
C++ is still bork...C++spam26.01.2008 5:58 PM (953 days 19h 10m ago)-view
C++ is bork #3C++spam26.01.2008 5:47 PM (953 days 19h 21m ago)-view
C++ is bork #2C++spam26.01.2008 5:46 PM (953 days 19h 22m ago)-view
36 Results
Page 1 of 3