-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathFunctionalProgramming.cpp
More file actions
213 lines (177 loc) · 5.56 KB
/
FunctionalProgramming.cpp
File metadata and controls
213 lines (177 loc) · 5.56 KB
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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
// =====================================================================================
// FunctionalProgramming01.cpp
// =====================================================================================
module modern_cpp:functional_programming;
namespace FunctionalProgramming {
class User
{
public:
std::string m_name;
int m_age;
std::string m_phone_number;
std::string m_email;
};
static void updateAge(User& user)
{
user.m_age = user.m_age + 1;
}
static User pureUpdateAge(const User& user) // cannot modify the input argument
{
User tmp{ user };
tmp.m_age = tmp.m_age + 1;
return tmp;
}
static User pureUpdateAge2(User user) // user is the copy of the passed object
{
user.m_age = user.m_age + 1;
return user;
}
static void test_functional_programming_01()
{
User john
{
.m_name{ "John" },
.m_age{ 25 }
};
pureUpdateAge(john);
}
static void test_functional_programming_02()
{
User john
{
.m_name{ "John" },
.m_age{ 25 }
};
auto updated{ pureUpdateAge2(john) };
std::println("Age: {}", updated.m_age);
updated = pureUpdateAge2(john);
std::println("Age: {}", updated.m_age);
}
class Function
{
private:
int m_state;
public:
Function () : Function{ 0 } {}
Function(int state) : m_state{ state } {}
void modify_state(int state) {
m_state = state;
}
int get_state() const {
return m_state;
}
void operator()() {
// do something that a function would do
std::println("Do something using state {}", m_state);
}
};
static void doSomething(Function f)
{
f();
}
static void test_functional_programming_03()
{
Function func{ 123 };
doSomething(func);
}
// =================================================================================
// testing 'Filter-Map-Reduce' Pattern
class Book
{
public:
std::string m_title;
std::string m_author;
int m_year;
double m_price;
};
static void test_functional_programming_04() {
std::list<Book> booksList
{
{ "C", "Dennis Ritchie", 1972, 11.99 },
{ "Java", "James Gosling", 1995, 19.99 },
{ "C++", "Bjarne Stroustrup", 1985, 20.00 },
{ "C#", "Anders Hejlsberg", 2000, 29.99 }
};
// filter books which appeared past 1990
std::vector<Book> booksAfter1990{};
std::copy_if (
std::begin(booksList),
std::end(booksList),
std::back_inserter(booksAfter1990),
[] (const Book& book) { return book.m_year >= 1990; }
);
// extract book title
std::vector<std::string> bookTitles{};
std::transform(
std::begin(booksAfter1990),
std::end(booksAfter1990),
std::back_inserter(bookTitles),
[] (const Book& book) { return book.m_title; } // convert Book to std::string
);
// reduce to result string, e.g. comma separated list
std::string titles{
std::accumulate(
std::begin(bookTitles),
std::end(bookTitles),
std::string{},
[](const std::string& a, const std::string& b) {
std::stringstream ss;
if (a.empty()) {
ss << b;
}
else {
ss << a << ", " << b;
}
return ss.str();
}
)
};
std::println("Titles: {}", titles);
}
static void test_functional_programming_05() {
std::list<Book> booksList
{
{ "C", "Dennis Ritchie", 1972, 11.99 } ,
{ "Java", "James Gosling", 1995, 19.99 },
{ "C++", "Bjarne Stroustrup", 1985, 20.00 },
{ "C#", "Anders Hejlsberg", 2000, 29.99 }
};
auto results {
booksList
| std::ranges::views::filter([](const Book& book) { return book.m_year >= 1990; })
| std::ranges::views::transform([](const Book& book) { return book.m_title; })
| std::ranges::views::common
};
// reduce to result string, e.g. comma separated list
std::string titles{
std::accumulate(
std::begin(results),
std::end(results),
std::string{},
[](const std::string& a, const std::string& b) {
std::stringstream ss;
if (a.empty()) {
ss << b;
}
else {
ss << a << ", " << b;
}
return ss.str();
}
)
};
std::println("Titles: {}", titles);
}
}
void main_functional_programming_ranges()
{
using namespace FunctionalProgramming;
test_functional_programming_01();
test_functional_programming_02();
test_functional_programming_03();
test_functional_programming_04();
test_functional_programming_05();
}
// =====================================================================================
// End-of-File
// =====================================================================================