From 9422d9780b1ff0c0ced2a734b42abe138fac8315 Mon Sep 17 00:00:00 2001 From: njlr Date: Wed, 27 Sep 2017 14:31:26 +0100 Subject: [PATCH] * Added a single header --- BUCK | 8 +++- neither/include/either.hpp | 4 +- neither/include/lift.hpp | 81 ++++++++++++++++++------------------- neither/include/maybe.hpp | 2 +- neither/include/neither.hpp | 10 +++++ neither/tests/either.cpp | 3 +- neither/tests/lift.cpp | 5 +-- neither/tests/maybe.cpp | 2 +- neither/tests/try.cpp | 3 +- 9 files changed, 63 insertions(+), 55 deletions(-) create mode 100644 neither/include/neither.hpp diff --git a/BUCK b/BUCK index b00181f..395ea55 100644 --- a/BUCK +++ b/BUCK @@ -4,13 +4,17 @@ cxx_library( exported_headers = subdir_glob([ ('neither/include', '**/*.hpp'), ]), - visibility = ['PUBLIC'] + visibility = [ + 'PUBLIC' + ], ) cxx_test( name = 'test', - deps = [':neither'], srcs = glob([ 'neither/tests/**/*.cpp', ]), + deps = [ + ':neither', + ], ) diff --git a/neither/include/either.hpp b/neither/include/either.hpp index 81339dc..003657e 100644 --- a/neither/include/either.hpp +++ b/neither/include/either.hpp @@ -1,10 +1,10 @@ #ifndef NEITHER_EITHER_HPP #define NEITHER_EITHER_HPP -#include -#include #include #include +#include +#include namespace neither { diff --git a/neither/include/lift.hpp b/neither/include/lift.hpp index a0e6096..09e534a 100644 --- a/neither/include/lift.hpp +++ b/neither/include/lift.hpp @@ -2,60 +2,59 @@ namespace neither { -template -constexpr bool hasValue(Either const& e) { - return e; -} + template + constexpr bool hasValue(Either const& e) { + return e; + } -template -constexpr bool hasValue(Maybe const& m) { - return m; -} + template + constexpr bool hasValue(Maybe const& m) { + return m; + } -template -constexpr bool hasValue(T) { - return true; -} + template + constexpr bool hasValue(T) { + return true; + } -template -constexpr R unpack(Either const& e) { - return e.rightValue; -} - + template + constexpr R unpack(Either const& e) { + return e.rightValue; + } -template -constexpr T unpack(Maybe const& m) { - return m.value; -} -template -constexpr T unpack(T const& x) { - return x; -} + template + constexpr T unpack(Maybe const& m) { + return m.value; + } -constexpr auto allTrue(bool x=true, bool y=true) { - return x && y; -} + template + constexpr T unpack(T const& x) { + return x; + } + constexpr auto allTrue(bool x=true, bool y=true) { + return x && y; + } -template -auto allTrue(X x, Xs...xs) { - return allTrue(x, allTrue(xs...)); -} -template -auto lift(F const& f) { - return [f](auto...x) -> decltype(maybe(f(unpack(x)...))) { - if ( allTrue(hasValue(x)...) ) { - return f(unpack(x)...); - } + template + auto allTrue(X x, Xs...xs) { + return allTrue(x, allTrue(xs...)); + } - return maybe(); - }; -} + template + auto lift(F const& f) { + return [f](auto...x) -> decltype(maybe(f(unpack(x)...))) { + if ( allTrue(hasValue(x)...) ) { + return f(unpack(x)...); + } + return maybe(); + }; + } } diff --git a/neither/include/maybe.hpp b/neither/include/maybe.hpp index 5be5155..fe4801a 100644 --- a/neither/include/maybe.hpp +++ b/neither/include/maybe.hpp @@ -1,8 +1,8 @@ #ifndef NEITHER_MAYBE_HPP #define NEITHER_MAYBE_HPP -#include #include +#include namespace neither { diff --git a/neither/include/neither.hpp b/neither/include/neither.hpp new file mode 100644 index 0000000..29ec75d --- /dev/null +++ b/neither/include/neither.hpp @@ -0,0 +1,10 @@ +#ifndef NEITHER_NEITHER_HPP +#define NEITHER_NEITHER_HPP + +#include +#include +#include +#include +#include + +#endif diff --git a/neither/tests/either.cpp b/neither/tests/either.cpp index 73271a3..8db18fa 100644 --- a/neither/tests/either.cpp +++ b/neither/tests/either.cpp @@ -1,6 +1,6 @@ #include #include -#include +#include #include #include @@ -110,4 +110,3 @@ TEST(neither, mapToUnique) { ASSERT_TRUE(*u == 1); } - diff --git a/neither/tests/lift.cpp b/neither/tests/lift.cpp index 7530978..d6ad09f 100644 --- a/neither/tests/lift.cpp +++ b/neither/tests/lift.cpp @@ -1,5 +1,4 @@ -#include -#include +#include #include TEST(neither, lift_maybes) { @@ -22,5 +21,3 @@ TEST(neither, lift_maybes) { ASSERT_TRUE(sum1.hasValue && sum1.value == 12); ASSERT_TRUE(!sum2.hasValue); } - - diff --git a/neither/tests/maybe.cpp b/neither/tests/maybe.cpp index 12570cb..3019984 100644 --- a/neither/tests/maybe.cpp +++ b/neither/tests/maybe.cpp @@ -1,5 +1,5 @@ #include -#include +#include using namespace neither; diff --git a/neither/tests/try.cpp b/neither/tests/try.cpp index 5340e58..3084fc3 100644 --- a/neither/tests/try.cpp +++ b/neither/tests/try.cpp @@ -1,6 +1,5 @@ #include -#include -#include +#include TEST(neither, try_and_fail) {