-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patharrow_utils.hpp
More file actions
117 lines (100 loc) · 2.73 KB
/
Copy patharrow_utils.hpp
File metadata and controls
117 lines (100 loc) · 2.73 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
#ifndef ARROW_UTILS_HPP
#define ARROW_UTILS_HPP
#include "arrow.hpp"
#include "curry.hpp"
namespace FunctionalCpp
{
// (***) :: (a `arr` c) -> (b `arr` d) -> ((a, b) `arr` (c, d))
template < class A, class B, class C, class D >
Function1< Tuple< A, B >, Tuple< C, D > > prod3(Function1< A, C > a2c, Function1< B, D > b2d)
{
return Arrow< A, B, C, D >::prod3(a2c, b2d);
}
template < class A, class B, class C, class D >
auto prod3(C(a2c)(A), D(b2d)(B))
{
Function1< A, C > a2c_ = a2c;
Function1< B, D > b2d_ = b2d;
return prod3(a2c_, b2d_);
}
// (&&&) :: (a `arr` c) -> (a `arr` d) -> (a `arr` (c, d))
template < class A, class C, class D >
Function1< A, Tuple< C, D > > and3(Function1< A, C > a2c, Function1< A, D > a2d)
{
Function1< A, Tuple< C, D > > a2cd = [a2c, a2d](A a) {
Tuple< A, A > aa{a, a};
return Arrow< A, A, C, D >::prod3(a2c, a2d)(aa);
};
return std::move(a2cd);
}
template < class A, class C, class D >
auto and3(C(a2c)(A), D(a2d)(A))
{
Function1< A, C > a2c_ = a2c;
Function1< A, D > a2d_ = a2d;
return and3(a2c_, a2d_);
}
// ===
// class to encapsulate: first :: (a `arr` c) -> ((a, b) `arr` (c, b))
// NOTE: needed to delay the deduction of the last argument <class B>
template < class A, class C >
struct First
{
First(Function1< A, C > a2c)
: a2c_m(a2c)
{
}
template < class B >
Tuple< C, B > operator()(Tuple< A, B > ab)
{
Tuple< C, B > cb = Arrow< A, B, C, B >::prod3(a2c_m, id< B >)(ab);
return std::move(cb);
}
private:
Function1< A, C > a2c_m;
};
// first :: (a `arr` c) -> ((a, b) `arr` (c, b))
template < class A, class C >
auto first(Function1< A, C > a2c)
{
return std::move(First< A, C >(a2c));
}
template < class A, class C >
auto first(C(a2c)(A))
{
Function1< A, C > a2c_ = a2c;
return first(a2c_);
}
// ===
// class to encapsulate: second :: (c `arr` d) -> ((b, c) `arr` (b, d))
// NOTE: needed to delay the deduction of the last argument <class B>
template < class C, class D >
struct Second
{
Second(Function1< C, D > c2d)
: c2d_m(c2d)
{
}
template < class B >
Tuple< B, D > operator()(Tuple< B, C > bc)
{
Tuple< B, D > bd = Arrow< B, C, B, D >::prod3(id< B >, c2d_m)(bc);
return std::move(bd);
}
private:
Function1< C, D > c2d_m;
};
// second :: (c `arr` d) -> ((b, c) `arr` (b, d))
template < class C, class D >
auto second(Function1< C, D > c2d)
{
return std::move(Second< C, D >(c2d));
}
template < class C, class D >
auto second(D(c2d)(C))
{
Function1< C, D > c2d_ = c2d;
return second(c2d);
}
} // end namespace FunctionalCpp
#endif // ARROW_UTILS_HPP