Skip to content

Commit 377aeed

Browse files
authored
Merge pull request #13 from njlr/improvement/comparison
improvement/comparison
2 parents 2632bf4 + b7973a6 commit 377aeed

File tree

8 files changed

+169
-62
lines changed

8 files changed

+169
-62
lines changed

.buckconfig

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,6 @@
1+
[project]
2+
ignore = .git, .buckd
3+
14
[cxx]
5+
cxxflags = -std=c++14
26
gtest_dep = google.gtest//:gtest

.travis.yml

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
language: cpp
2+
sudo: true
3+
dist: trusty
4+
5+
addons:
6+
apt:
7+
sources:
8+
- ubuntu-toolchain-r-test
9+
packages:
10+
- g++-5
11+
12+
before_install:
13+
- cd /usr/bin/ && sudo rm g++ && sudo ln -s g++-5 g++ && cd -
14+
- g++ --version
15+
- sudo apt-get install default-jdk
16+
- wget -O buck.deb https://github.com/facebook/buck/releases/download/v2017.09.04.02/buck-2017.09.04.02_all.deb
17+
- sudo dpkg -i buck.deb
18+
- wget -O buckaroo.deb https://github.com/LoopPerfect/buckaroo/releases/download/v1.3.1/buckaroo_1.3.1_amd64.deb
19+
- sudo dpkg -i buckaroo.deb
20+
21+
script:
22+
- buckaroo install
23+
- buck build //:neither
24+
- buck build //:test#linux-x86_64
25+
- buck run //:test#linux-x86_64

BUCK

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,20 @@ cxx_library(
44
exported_headers = subdir_glob([
55
('neither/include', '**/*.hpp'),
66
]),
7-
visibility = ['PUBLIC']
7+
visibility = [
8+
'PUBLIC',
9+
],
810
)
911

1012
cxx_test(
1113
name = 'test',
12-
deps = [':neither'],
1314
srcs = glob([
1415
'neither/tests/**/*.cpp',
1516
]),
17+
platform_compiler_flags = [
18+
('^linux.*', [ '-lpthread' ]),
19+
],
20+
deps = [
21+
':neither',
22+
],
1623
)

neither/include/either.hpp

Lines changed: 36 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,12 @@
11
#ifndef NEITHER_EITHER_HPP
22
#define NEITHER_EITHER_HPP
33

4-
#include <neither/traits.hpp>
5-
#include <neither/maybe.hpp>
64
#include <memory>
75
#include <type_traits>
86

7+
#include <neither/traits.hpp>
8+
#include <neither/maybe.hpp>
9+
910
namespace neither {
1011

1112
template<class T>
@@ -25,7 +26,7 @@ constexpr Left<T> left(T const& x) {
2526

2627
template<class T>
2728
Left<T> left(T&& x) {
28-
return {std::move(x)};
29+
return { std::move(x) };
2930
}
3031

3132
template<class T>
@@ -41,12 +42,9 @@ constexpr Right<T> right(T const& x) {
4142

4243
template<class T>
4344
Right<T> right(T&& x) {
44-
return {std::move(x)};
45+
return { std::move(x) };
4546
}
4647

47-
48-
49-
5048
template<class L, class R>
5149
struct Either {
5250

@@ -104,18 +102,17 @@ struct Either {
104102
}
105103

106104
constexpr auto left() const -> Maybe<L> {
107-
if (!isLeft)
108-
return maybe();
109-
return maybe(leftValue);
105+
return isLeft ?
106+
maybe(leftValue) :
107+
maybe();
110108
}
111109

112110
constexpr auto right() const -> Maybe<R> {
113-
if(isLeft)
114-
return maybe();
115-
return maybe(rightValue);
111+
return isLeft ?
112+
maybe() :
113+
maybe(rightValue);
116114
}
117115

118-
119116
static constexpr auto leftOf( L const& l ) {
120117
return Either<L, R>{ neither::left(l) };
121118
}
@@ -124,7 +121,6 @@ struct Either {
124121
return Either<L, R>{ neither::right(r) };
125122
}
126123

127-
128124
static constexpr auto leftOf( L && l ) {
129125
return Either<L, R>{ neither::left(std::move(l)) };
130126
}
@@ -133,7 +129,6 @@ struct Either {
133129
return Either<L, R>{ neither::right(std::move(r)) };
134130
}
135131

136-
137132
template<
138133
class L2 = L,
139134
class R2 = R>
@@ -142,26 +137,25 @@ struct Either {
142137
isCopyable((L2)leftValue, (R2)rightValue),
143138
std::declval<std::common_type_t<L2, R2>>()
144139
) {
145-
return isLeft? leftValue : rightValue;
140+
return isLeft ? leftValue : rightValue;
146141
}
147142

148-
149143
template<
150144
class L2 = L,
151145
class R2 = R>
152146
auto join()&&
153147
-> std::common_type_t<L2, R2> {
154-
return isLeft? std::move(leftValue) : std::move(rightValue);
148+
return isLeft ? std::move(leftValue) : std::move(rightValue);
155149
}
156150

157151
template<class LeftF, class RightF>
158152
constexpr auto join(LeftF const& leftCase, RightF const& rightCase) const
159153
-> decltype( isLeft? leftCase( leftValue ) : rightCase( rightValue ) ) {
160-
return isLeft? leftCase( leftValue ) : rightCase( rightValue );
154+
return isLeft ? leftCase( leftValue ) : rightCase( rightValue );
161155
}
162156

163157
template<class F, class L2=L, class R2=R>
164-
constexpr auto leftMap(F const& leftCase) const&
158+
constexpr auto leftMap(F const& leftCase) const&
165159
-> Either<decltype(leftCase( isCopyable((L2)leftValue, (R2)rightValue) )), R2> {
166160
using NextEither = Either<decltype(leftCase(leftValue)), R2>;
167161
return isLeft ?
@@ -205,7 +199,7 @@ struct Either {
205199
return NextEither::rightOf(rightValue);
206200
}
207201

208-
template<class RightCase, class L2=L, class R2=R>
202+
template<class RightCase, class L2 = L, class R2 = R>
209203
constexpr auto rightFlatMap(RightCase const& rightCase) const&
210204
-> decltype( ensureEitherLeft(rightCase(isCopyable((R2)rightValue)), isCopyable((L2)leftValue))) {
211205
using NextEither = decltype(rightCase(rightValue));
@@ -217,9 +211,7 @@ struct Either {
217211
return NextEither::leftOf(leftValue);
218212
}
219213

220-
221-
222-
template<class LeftCase, class L2=L, class R2=R>
214+
template<class LeftCase, class L2 = L, class R2 = R>
223215
auto leftFlatMap(LeftCase const& leftCase)&&
224216
-> decltype( ensureEitherRight(leftCase(std::move(leftValue)), std::move(rightValue))) {
225217
using NextEither = decltype(leftCase(std::move(leftValue)));
@@ -246,6 +238,25 @@ struct Either {
246238
constexpr operator bool()const { return !isLeft; }
247239
};
248240

241+
template <typename L, typename R>
242+
bool operator == (Either<L, R> const& a, Either<L, R> const& b) {
243+
if (a.isLeft) {
244+
if (b.isLeft) {
245+
return a.left() == b.left();
246+
}
247+
} else {
248+
if (!b.isLeft) {
249+
return a.right() == b.right();
250+
}
251+
}
252+
return false;
253+
}
254+
255+
template <typename L, typename R>
256+
bool operator != (Either<L, R> const& a, Either<L, R> const& b) {
257+
return !(a == b);
258+
}
259+
249260
}
250261

251262
#endif

neither/include/maybe.hpp

Lines changed: 20 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@ template <class T> struct Maybe {
1515
union {
1616
T value;
1717
};
18+
1819
bool const hasValue = 0;
1920

2021
constexpr Maybe() : hasValue{0} {}
@@ -85,12 +86,28 @@ template <class T> struct Maybe {
8586
return f(std::move(value));
8687
}
8788

88-
constexpr operator bool()const { return hasValue; }
89+
constexpr operator bool() const { return hasValue; }
8990
};
9091

91-
template <class T> auto maybe(T value) -> Maybe<T> { return {value}; }
92+
template <typename T>
93+
auto maybe(T value) -> Maybe<T> { return {value}; }
94+
95+
template <typename T = void>
96+
auto maybe() -> Maybe<T> { return {}; }
97+
98+
template <typename T>
99+
bool operator == (Maybe<T> const& a, Maybe<T> const& b) {
100+
if (a.hasValue) {
101+
return b.hasValue && a.value == b.value;
102+
}
103+
return !b.hasValue;
104+
}
105+
106+
template <typename T>
107+
bool operator != (Maybe<T> const& a, Maybe<T> const& b) {
108+
return !(a == b);
109+
}
92110

93-
template <class T = void> auto maybe() -> Maybe<T> { return {}; }
94111
}
95112

96113
#endif

neither/include/traits.hpp

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -8,50 +8,50 @@ struct Either;
88

99
template<class T>
1010
struct Maybe;
11-
11+
1212
template<class L,class...Xs>
1313
auto isCopyable (L l, Xs...) -> L {
1414
return l;
1515
}
16-
16+
1717
template<class L, class R>
1818
auto ensureEither ( Either<L,R> const& e) -> Either<L,R> {
1919
return e;
2020
}
21-
21+
2222
template<class L, class R>
2323
auto ensureEither ( Either<L,R> && e) -> Either<L,R> {
2424
return e;
2525
}
26-
26+
2727
template<class L, class R>
2828
auto ensureEitherRight ( Either<L,R> const& e, R) -> Either<L, R> {
2929
return e;
3030
}
31-
32-
31+
32+
3333
template<class L, class R>
3434
auto ensureEitherRight ( Either<L,R>&& e, R&&) -> Either<L, R> {
3535
return e;
3636
}
37-
38-
37+
38+
3939
template<class L, class R>
4040
auto ensureEitherLeft ( Either<L,R> const& e, L) -> Either<L, R> {
4141
return e;
4242
}
43-
43+
4444
template<class L, class R>
4545
auto ensureEitherLeft ( Either<L,R>&& e, L&& ) -> Either<L, R> {
4646
return e;
4747
}
4848

49-
49+
5050
template<class T>
5151
auto ensureMaybe ( Maybe<T> const& e) -> Maybe<T> {
5252
return e;
5353
}
54-
54+
5555
}
5656

5757
#endif

0 commit comments

Comments
 (0)