-
Notifications
You must be signed in to change notification settings - Fork 0
/
tests.cpp
62 lines (51 loc) · 1.9 KB
/
tests.cpp
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
#include <iostream>
#include "curry.h"
int addThree(int a, int b, int c)
{
return a + b + c;
}
int addTwo(int a, int b, int& out)
{
static int numCalled = 0;
out = ++numCalled;
return a + b;
}
int main() {
CurriedFunction<int(int, int, int)> func;
std::cout << "function pointer:\n";
func = &addThree;
std::cout << func(1)(2)(3) << std::endl;
std::cout << func(1)(2, 3) << std::endl;
std::cout << func(1, 2)(3) << std::endl;
std::cout << func(1, 2, 3) << std::endl;
std::cout << func()(1)()(2)()()()(3) << std::endl;
std::cout << std::endl;
std::cout << "lambda:\n";
func = [](int a, int b, int c) { return a + b + c; };
std::cout << func(1)(2)(3) << std::endl;
std::cout << func(1)(2, 3) << std::endl;
std::cout << func(1, 2)(3) << std::endl;
std::cout << func(1, 2, 3) << std::endl;
std::cout << func()(1)()(2)()()()(3) << std::endl;
std::cout << std::endl;
std::cout << "captures:\n";
int count1 = 0;
func = [&count1](int a, int b, int c) { ++count1; return a + b + c; };
std::cout << func(1)(2)(3) << std::endl;
std::cout << func(1)(2, 3) << std::endl;
std::cout << func(1, 2)(3) << std::endl;
std::cout << func(1, 2, 3) << std::endl;
std::cout << func()(1)()(2)()()()(3) << std::endl;
std::cout << "count: " << count1 << std::endl;
std::cout << std::endl;
std::cout << "forwarding:\n";
int count2 = 0;
CurriedFunction<int(int, int, int&)> fptr2 = &addTwo;
std::cout << fptr2(1)(2)(count2) << ", count: " << count2 << std::endl;
std::cout << fptr2(1)(2, count2) << ", count: " << count2 << std::endl;
std::cout << fptr2(1, 2)(count2) << ", count: " << count2 << std::endl;
std::cout << fptr2(1, 2, count2) << ", count: " << count2 << std::endl;
std::cout << fptr2()(1)()(2)()()()(count2) << ", count: " << count2 << std::endl;
std::cout << std::endl;
return 0;
}