C++ Library Extensions 2022.12.09
To help learn modern C++ programming
lambda_evaluation.cpp
Go to the documentation of this file.
1/*
2 Author: Thomas Kim
3 First Edit: Feb. 03, 2021
4
5 Requirements: C++ compilers that supports C++17 Standards
6
7 clang++ -std=c++17 filename.cpp -ltbb -o output.exe
8 g++ -std=c++17 filename.cpp -ltbb -o output.exe
9 cl /EHsc /std:c++17 filename.cpp /Fe: output.exe
10 dpcpp -Qstd=c++17 /EHsc filename.cpp -o output.exe
11*/
12
13#include <tpf_output.hpp>
14
15namespace types = tpf::types;
16
18auto& endl = tpf::endl; // one carriage-return and flush out to console
19auto& endL = tpf::endL; // two carriage-returns and flush out to console
20
22{
23 stream <<"--- File Contents Summary ---\n\n---We define lambdas as elements of std::tuple\n";
24 stream <<" then we evaluate the lambdas at one function call\n";
25 stream <<" and get the evaluation result through the container of our choice." << endL;
26 stream <<" Example: types::evaluate_lambdas<std::vector>(lambdas, 1.0, 2.0, 3.0) " << endL;
27}
28
30{
31 std::tuple lambdas
32 {
33 // function 0
34 [](auto&& x, auto&& y, auto&& z){ return x + y + z; } ,
35
36 // function 1
37 [](auto&& x, auto&& y, auto&& z){ return x * y + z; } ,
38
39 // function 2
40 [](auto&& x, auto&& y, auto&& z){ return x + y * z; } ,
41
42 // function 3
43 [](auto&& x, auto&& y, auto&& z){ return x - y + z; } ,
44
45 // function 4
46 [](auto&& x, auto&& y, auto&& z){ return x + y - z; } ,
47
48 // function 5
49 [](auto&& x, auto&& y, auto&& z){ return x / y + z; } ,
50
51 // function 6
52 [](auto&& x, auto&& y, auto&& z){ return x + y / z; } ,
53
54 // function 7
55 [](auto&& x, auto&& y, auto&& z){ return x / y * z; } ,
56
57 // function 8
58 [](auto&& x, auto&& y, auto&& z){ return x * y / z; } ,
59
60 // function 9
61 [](auto&& x, auto&& y, auto&& z){ return x - y * z; } ,
62
63 // function 10
64 [](auto&& x, auto&& y, auto&& z){ return x * y - z; } ,
65
66 // function 11
67 [](auto&& x, auto&& y, auto&& z){ return x * y * z; }
68 };
69
70 auto v_as_vector1 = types::evaluate_lambdas<std::vector>(lambdas, 1.0, 2.0, 3.0);
71 stream <<"lambdas(1.0, 2.0, 3.0) as vector =\n\t" << v_as_vector1 << endL;
72
73 auto v_as_vector2 = types::evaluate_lambdas<std::vector>(lambdas, 2.0, 1.0, 3.0);
74 stream <<"lambdas(2.0, 1.0, 3.0) as vector =\n\t" << v_as_vector2 << endL;
75
76 auto v_as_deque1 = types::evaluate_lambdas<std::deque>(lambdas, 1.0, 2.0, 3.0);
77 stream <<"lambdas(1.0, 2.0, 3.0) as deque =\n\t" << v_as_deque1 << endL;
78
79 auto v_as_deque2 = types::evaluate_lambdas<std::deque>(lambdas, 2.0, 1.0, 3.0);
80 stream <<"lambdas(2.0, 1.0, 3.0) as deque =\n\t" << v_as_deque2 << endL;
81
82 auto v_as_array1 = types::evaluate_lambdas<std::array>(lambdas, 1.0, 2.0, 3.0);
83 stream <<"lambdas(1.0, 2.0, 3.0) as array =\n\t" << v_as_array1 << endL;
84
85 auto v_as_array2 = types::evaluate_lambdas<std::array>(lambdas, 2.0, 1.0, 3.0);
86 stream <<"lambdas(2.0, 1.0, 3.0) as array =\n\t" << v_as_array2 << endL;
87
88 auto v_as_list1 = types::evaluate_lambdas<std::list>(lambdas, 1.0, 2.0, 3.0);
89 stream <<"lambdas(1.0, 2.0, 3.0) as list =\n\t" << v_as_list1 << endL;
90
91 auto v_as_list2 = types::evaluate_lambdas<std::list>(lambdas, 2.0, 1.0, 3.0);
92 stream <<"lambdas(2.0, 1.0, 3.0) as list =\n\t" << v_as_list2 << endL;
93
94 auto v_as_multiset1 = types::evaluate_lambdas<std::multiset>(lambdas, 1.0, 2.0, 3.0);
95 stream <<"lambdas(1.0, 2.0, 3.0) as multiset =\n\t" << v_as_multiset1 << endL;
96
97 auto v_as_multiset2 = types::evaluate_lambdas<std::multiset>(lambdas, 2.0, 1.0, 3.0);
98 stream <<"lambdas(2.0, 1.0, 3.0) as multiset =\n\t" << v_as_multiset2 << endL;
99
100 stream <<"\t\t--- By Thomas Kim, Feb. 05, 2020. ---" << endl;
101}
102
104{
105 std::tuple lambdas
106 {
107 // function 0
108 [](auto&& x, auto&& y, auto&& z){ return x + y + z; } ,
109
110 // function 1
111 [](auto&& x, auto&& y, auto&& z){ return x * y + z; } ,
112
113 // function 2
114 [](auto&& x, auto&& y, auto&& z){ return x + y * z; } ,
115
116 // function 3
117 [](auto&& x, auto&& y, auto&& z){ return x - y + z; } ,
118
119 // function 4
120 [](auto&& x, auto&& y, auto&& z){ return x + y - z; } ,
121
122 // function 5
123 [](auto&& x, auto&& y, auto&& z){ return x / y + z; } ,
124
125 // function 6
126 [](auto&& x, auto&& y, auto&& z){ return x + y / z; } ,
127
128 // function 7
129 [](auto&& x, auto&& y, auto&& z){ return x / y * z; } ,
130
131 // function 8
132 [](auto&& x, auto&& y, auto&& z){ return x * y / z; } ,
133
134 // function 9
135 [](auto&& x, auto&& y, auto&& z){ return x - y * z; } ,
136
137 // function 10
138 [](auto&& x, auto&& y, auto&& z){ return x * y - z; } ,
139
140 // function 11
141 [](auto&& x, auto&& y, auto&& z){ return x * y * z; }
142 };
143
144 std::tuple arguments1{1.0, 2.0, 3.0};
145 std::tuple arguments2{1.0, 2.0, 3.0};
146
147 auto v_as_vector1 = types::evaluate_lambdas<std::vector>(lambdas, arguments1);
148 stream <<"lambdas "<< arguments1 <<" as vector =\n\t" << v_as_vector1 << endL;
149
150 auto v_as_vector2 = types::evaluate_lambdas<std::vector>(lambdas, arguments2);
151 stream <<"lambdas "<< arguments2 <<" as vector =\n\t" << v_as_vector2 << endL;
152}
153
154int main()
155{
156 // summary_evaluate_lambdas();
157
158 // test_evaluate_lambdas();
159
161}
void test_evaluate_lambdas()
void summary_evaluate_lambdas()
auto & endl
tpf::sstream stream
auto & endL
void test_evaluate_lambdas_with_tuple()
int main()
Type to string name conversions are defined.
Definition: 31-visit.cpp:7
constexpr auto endL
Definition: tpf_output.hpp:974
constexpr auto endl
Definition: tpf_output.hpp:973
Stream output operators << are implemented.