Skip to content

Commit 8d0725c

Browse files
authored
Merge pull request #14 from njlr/improvement/single-include
A single header option
2 parents 377aeed + ebe80e6 commit 8d0725c

8 files changed

Lines changed: 57 additions & 71 deletions

File tree

neither/include/either.hpp

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
#include <memory>
55
#include <type_traits>
6+
#include <neither/traits.hpp>
7+
#include <neither/maybe.hpp>
68

79
#include <neither/traits.hpp>
810
#include <neither/maybe.hpp>

neither/include/lift.hpp

Lines changed: 40 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -2,60 +2,59 @@
22

33
namespace neither {
44

5-
template<class L, class R>
6-
constexpr bool hasValue(Either<L,R> const& e) {
7-
return e;
8-
}
5+
template<class L, class R>
6+
constexpr bool hasValue(Either<L,R> const& e) {
7+
return e;
8+
}
99

1010

11-
template<class T>
12-
constexpr bool hasValue(Maybe<T> const& m) {
13-
return m;
14-
}
11+
template<class T>
12+
constexpr bool hasValue(Maybe<T> const& m) {
13+
return m;
14+
}
1515

16-
template<class T>
17-
constexpr bool hasValue(T) {
18-
return true;
19-
}
16+
template<class T>
17+
constexpr bool hasValue(T) {
18+
return true;
19+
}
2020

2121

2222

23-
template<class L, class R>
24-
constexpr R unpack(Either<L, R> const& e) {
25-
return e.rightValue;
26-
}
27-
23+
template<class L, class R>
24+
constexpr R unpack(Either<L, R> const& e) {
25+
return e.rightValue;
26+
}
2827

29-
template<class T>
30-
constexpr T unpack(Maybe<T> const& m) {
31-
return m.value;
32-
}
3328

34-
template<class T>
35-
constexpr T unpack(T const& x) {
36-
return x;
37-
}
29+
template<class T>
30+
constexpr T unpack(Maybe<T> const& m) {
31+
return m.value;
32+
}
3833

39-
constexpr auto allTrue(bool x=true, bool y=true) {
40-
return x && y;
41-
}
34+
template<class T>
35+
constexpr T unpack(T const& x) {
36+
return x;
37+
}
4238

39+
constexpr auto allTrue(bool x=true, bool y=true) {
40+
return x && y;
41+
}
4342

44-
template<class X, class...Xs>
45-
auto allTrue(X x, Xs...xs) {
46-
return allTrue(x, allTrue(xs...));
47-
}
4843

49-
template<class F>
50-
auto lift(F const& f) {
51-
return [f](auto...x) -> decltype(maybe(f(unpack(x)...))) {
52-
if ( allTrue(hasValue(x)...) ) {
53-
return f(unpack(x)...);
54-
}
44+
template<class X, class...Xs>
45+
auto allTrue(X x, Xs...xs) {
46+
return allTrue(x, allTrue(xs...));
47+
}
5548

56-
return maybe();
57-
};
58-
}
49+
template<class F>
50+
auto lift(F const& f) {
51+
return [f](auto...x) -> decltype(maybe(f(unpack(x)...))) {
52+
if ( allTrue(hasValue(x)...) ) {
53+
return f(unpack(x)...);
54+
}
5955

56+
return maybe();
57+
};
58+
}
6059

6160
}

neither/include/maybe.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
#ifndef NEITHER_MAYBE_HPP
22
#define NEITHER_MAYBE_HPP
33

4-
#include <neither/traits.hpp>
54
#include <memory>
5+
#include <neither/traits.hpp>
66

77
namespace neither {
88

neither/include/neither.hpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
#ifndef NEITHER_NEITHER_HPP
2+
#define NEITHER_NEITHER_HPP
3+
4+
#include <neither/either.hpp>
5+
#include <neither/lift.hpp>
6+
#include <neither/maybe.hpp>
7+
#include <neither/traits.hpp>
8+
#include <neither/try.hpp>
9+
10+
#endif

neither/tests/either.cpp

Lines changed: 1 addition & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
#include <iostream>
2+
#include <neither/neither.hpp>
23
#include <string>
34
#include <memory>
45

@@ -111,25 +112,3 @@ TEST(neither, either_mapToUnique) {
111112

112113
ASSERT_TRUE(*u == 1);
113114
}
114-
115-
TEST(neither, either_comparison) {
116-
117-
auto a = IntOrStr::leftOf(123);
118-
auto b = IntOrStr::leftOf(123);
119-
120-
EXPECT_TRUE(a == b);
121-
122-
auto c = IntOrStr::rightOf("abc");
123-
auto d = IntOrStr::rightOf("abc");
124-
auto e = IntOrStr::rightOf("def");
125-
126-
EXPECT_TRUE(c == d);
127-
128-
EXPECT_TRUE(a != c);
129-
EXPECT_TRUE(a != d);
130-
EXPECT_TRUE(b != c);
131-
EXPECT_TRUE(b != d);
132-
133-
EXPECT_TRUE(c != e);
134-
EXPECT_TRUE(d != e);
135-
}

neither/tests/lift.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
1-
#include <neither/either.hpp>
2-
#include <neither/lift.hpp>
1+
#include <neither/neither.hpp>
32
#include <gtest/gtest.h>
43

54
TEST(neither, lift_maybes) {
@@ -22,5 +21,3 @@ TEST(neither, lift_maybes) {
2221
ASSERT_TRUE(sum1.hasValue && sum1.value == 12);
2322
ASSERT_TRUE(!sum2.hasValue);
2423
}
25-
26-

neither/tests/maybe.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
#include <gtest/gtest.h>
2-
#include <neither/maybe.hpp>
2+
#include <neither/neither.hpp>
33

44
using namespace neither;
55

neither/tests/try.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
#include <gtest/gtest.h>
2-
#include <neither/either.hpp>
3-
#include <neither/try.hpp>
2+
#include <neither/neither.hpp>
43

54
TEST(neither, try_and_fail) {
65

0 commit comments

Comments
 (0)