Skip to content

Commit

Permalink
Merge pull request #14 from njlr/improvement/single-include
Browse files Browse the repository at this point in the history
A single header option
  • Loading branch information
nikhedonia committed Sep 27, 2017
2 parents 377aeed + ebe80e6 commit 8d0725c
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 71 deletions.
2 changes: 2 additions & 0 deletions neither/include/either.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

#include <memory>
#include <type_traits>
#include <neither/traits.hpp>
#include <neither/maybe.hpp>

#include <neither/traits.hpp>
#include <neither/maybe.hpp>
Expand Down
81 changes: 40 additions & 41 deletions neither/include/lift.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,60 +2,59 @@

namespace neither {

template<class L, class R>
constexpr bool hasValue(Either<L,R> const& e) {
return e;
}
template<class L, class R>
constexpr bool hasValue(Either<L,R> const& e) {
return e;
}


template<class T>
constexpr bool hasValue(Maybe<T> const& m) {
return m;
}
template<class T>
constexpr bool hasValue(Maybe<T> const& m) {
return m;
}

template<class T>
constexpr bool hasValue(T) {
return true;
}
template<class T>
constexpr bool hasValue(T) {
return true;
}



template<class L, class R>
constexpr R unpack(Either<L, R> const& e) {
return e.rightValue;
}

template<class L, class R>
constexpr R unpack(Either<L, R> const& e) {
return e.rightValue;
}

template<class T>
constexpr T unpack(Maybe<T> const& m) {
return m.value;
}

template<class T>
constexpr T unpack(T const& x) {
return x;
}
template<class T>
constexpr T unpack(Maybe<T> const& m) {
return m.value;
}

constexpr auto allTrue(bool x=true, bool y=true) {
return x && y;
}
template<class T>
constexpr T unpack(T const& x) {
return x;
}

constexpr auto allTrue(bool x=true, bool y=true) {
return x && y;
}

template<class X, class...Xs>
auto allTrue(X x, Xs...xs) {
return allTrue(x, allTrue(xs...));
}

template<class F>
auto lift(F const& f) {
return [f](auto...x) -> decltype(maybe(f(unpack(x)...))) {
if ( allTrue(hasValue(x)...) ) {
return f(unpack(x)...);
}
template<class X, class...Xs>
auto allTrue(X x, Xs...xs) {
return allTrue(x, allTrue(xs...));
}

return maybe();
};
}
template<class F>
auto lift(F const& f) {
return [f](auto...x) -> decltype(maybe(f(unpack(x)...))) {
if ( allTrue(hasValue(x)...) ) {
return f(unpack(x)...);
}

return maybe();
};
}

}
2 changes: 1 addition & 1 deletion neither/include/maybe.hpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
#ifndef NEITHER_MAYBE_HPP
#define NEITHER_MAYBE_HPP

#include <neither/traits.hpp>
#include <memory>
#include <neither/traits.hpp>

namespace neither {

Expand Down
10 changes: 10 additions & 0 deletions neither/include/neither.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
#ifndef NEITHER_NEITHER_HPP
#define NEITHER_NEITHER_HPP

#include <neither/either.hpp>
#include <neither/lift.hpp>
#include <neither/maybe.hpp>
#include <neither/traits.hpp>
#include <neither/try.hpp>

#endif
23 changes: 1 addition & 22 deletions neither/tests/either.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include <iostream>
#include <neither/neither.hpp>
#include <string>
#include <memory>

Expand Down Expand Up @@ -111,25 +112,3 @@ TEST(neither, either_mapToUnique) {

ASSERT_TRUE(*u == 1);
}

TEST(neither, either_comparison) {

auto a = IntOrStr::leftOf(123);
auto b = IntOrStr::leftOf(123);

EXPECT_TRUE(a == b);

auto c = IntOrStr::rightOf("abc");
auto d = IntOrStr::rightOf("abc");
auto e = IntOrStr::rightOf("def");

EXPECT_TRUE(c == d);

EXPECT_TRUE(a != c);
EXPECT_TRUE(a != d);
EXPECT_TRUE(b != c);
EXPECT_TRUE(b != d);

EXPECT_TRUE(c != e);
EXPECT_TRUE(d != e);
}
5 changes: 1 addition & 4 deletions neither/tests/lift.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#include <neither/either.hpp>
#include <neither/lift.hpp>
#include <neither/neither.hpp>
#include <gtest/gtest.h>

TEST(neither, lift_maybes) {
Expand All @@ -22,5 +21,3 @@ TEST(neither, lift_maybes) {
ASSERT_TRUE(sum1.hasValue && sum1.value == 12);
ASSERT_TRUE(!sum2.hasValue);
}


2 changes: 1 addition & 1 deletion neither/tests/maybe.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
#include <gtest/gtest.h>
#include <neither/maybe.hpp>
#include <neither/neither.hpp>

using namespace neither;

Expand Down
3 changes: 1 addition & 2 deletions neither/tests/try.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
#include <gtest/gtest.h>
#include <neither/either.hpp>
#include <neither/try.hpp>
#include <neither/neither.hpp>

TEST(neither, try_and_fail) {

Expand Down

0 comments on commit 8d0725c

Please sign in to comment.