forked from furkankirac/cs321-2019-20-fall
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathweek10-app1.cpp
91 lines (74 loc) · 1.82 KB
/
week10-app1.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
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
// Higher order functions, lifting a function:
// call_twice
// greater_than -> count_if(begin, end, greater_than(5))
// compose functions
#include <iostream>
#include <vector>
template<typename FUNC>
void call_twice(FUNC func)
{
func();
func();
}
template<typename CONTAINER, typename LAMBDA>
inline size_t count_if(const CONTAINER& container, LAMBDA lambda)
{
auto sum = size_t{};
for(const auto& value : container)
if(lambda(value))
++sum;
return sum;
}
inline auto greater_than(int value)
{
return [value](int k) { return k > value; };
// struct Lambda
// {
// int value;
// Lambda(int value) : value{value} { }
// auto operator() (int k) const { return k > value; }
// };
// return Lambda{value};
}
template<typename F, typename G>
auto compose(F f, G g)
{
return [=]() {
return f(g());
};
}
auto func_f = [](int k) { return k*2; };
//int func_f(int k) { return k*2; }
auto func_g = []() { return 10; };
//int func_g() { return 10; }
template<typename F, typename G>
auto operator |(F f, G g)
{
return [=]() {
return f(g());
};
}
int main(int argc, char* argv[])
{
auto composed = func_f | func_g;
// auto composed = compose(func_f, func_g);
auto z = composed();
std::cout << z << std::endl;
auto v = std::vector<int>{10, 20, 100, 1000};
// range-for does smt. below
// for(auto it=v.begin(); it!=v.end(); ++it)
// {
// auto a = *it;
// }
// auto sum = 0;
// for(int a : v)
// {
// if(a > 50)
// ++sum;
// }
// auto sum = count_if(v, [](int value) { return value > 50; });
auto sum = count_if(v, greater_than(50));
// auto sum = std::count_if(v.begin(), v.end(), [](int value) { return value > 50; });
std::cout << sum << std::endl;
return 0;
}