-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFunctor.hpp
66 lines (58 loc) · 1.58 KB
/
Functor.hpp
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
/**
* A functor type in C++ implemented using std::function<outType(inType)>.
* Note: this is not the same typeclass as in Haskell! Just an instantiation.
* To make a proper typeclass in C++, we need concept from C++ 20, which is
* not of our concern here in this repository.
*/
#include <vector>
#include <functional>
#include <iostream>
template <typename T1>
class Functor
{
private:
std::vector<T1> dataContainer;
public:
auto push(const T1 &data)
{
this->dataContainer.push_back(data);
}
template <typename T2>
auto fmap(const std::function<T2(T1)> &func) const
{
Functor<T2> functor;
for (const T1 &data : this->dataContainer)
{
functor.push(func(data));
}
return functor;
}
template <typename T2>
static auto fmap(const std::function<T2(T1)> &func, const Functor<T1> &f)
{
return f.fmap(func);
}
void printContainer() const
{
for (auto &data : this->dataContainer)
{
std::cout << data << ", ";
}
std::cout << std::endl;
}
};
// Example Functor Test
void testFunctor(const std::vector<int> list)
{
Functor<int> myFunctor;
for (const int &data : list)
{
myFunctor.push(data);
}
myFunctor.fmap<std::string>((std::function<std::string(int)>)[](const int &a) { return std::to_string(a); }).printContainer();
Functor<int>::fmap<std::string>((std::function<std::string(int)>)[](const int &a) { return std::to_string(a); }, myFunctor).printContainer();
}
void test()
{
testFunctor({1, 2, 3, 4, 5});
}