From 256d62713be299defe8fd20a7fe24b2805b6e214 Mon Sep 17 00:00:00 2001 From: Bowen Fu Date: Sat, 18 Jun 2022 12:57:14 +0800 Subject: [PATCH] Dev (#4) * Foldable. * Fix tuple foldable. * Fix gcc Co-authored-by: Bowen Fu --- include/hspp.h | 481 +++++++++++++++++++++++++++++++-------------- test/hspp/test.cpp | 84 ++++++-- 2 files changed, 404 insertions(+), 161 deletions(-) diff --git a/include/hspp.h b/include/hspp.h index 0b50644..e21e6ec 100644 --- a/include/hspp.h +++ b/include/hspp.h @@ -74,7 +74,25 @@ constexpr decltype(auto) takeTuple(Tuple &&t) template using TakeTupleType = std::decay_t(std::declval()))>; +template +class Overload : Ts... +{ +public: + constexpr Overload(Ts... ts) + : Ts{ts}... + {} + using Ts::operator()...; +}; + +template +auto overload(Ts&&... ts) +{ + return Overload{std::forward(ts)...}; +} + +namespace data +{ template class Maybe; @@ -123,22 +141,6 @@ class Maybe : public MaybeBase } }; -template -class Overload : Ts... -{ -public: - constexpr Overload(Ts... ts) - : Ts{ts}... - {} - using Ts::operator()...; -}; - -template -auto overload(Ts&&... ts) -{ - return Overload{std::forward(ts)...}; -} - template constexpr bool operator== (Maybe const& lhs, Maybe const& rhs) { @@ -321,7 +323,7 @@ constexpr auto toGFunc(Func const& func) constexpr inline auto id = toGFunc<1>([](auto data) { - return std::move(data); + return data; }); constexpr auto just = toGFunc<1>([](auto d) @@ -1308,6 +1310,10 @@ namespace impl } } // namespace impl +using impl::isContainerV; +using impl::isTupleLikeV; +using impl::hasReverseIteratorsV; + constexpr auto flip = toGFunc<1>([](auto func) { return impl::flipImpl(std::move(func)); @@ -1335,9 +1341,9 @@ constexpr auto foldrRecur(Iter1 begin, Iter2 end, Init init, Func func) -> Init return init; } -constexpr auto foldr = toGFunc<3>([](auto func, auto init, auto const& list) +constexpr auto listFoldr = toGFunc<3>([](auto func, auto init, auto const& list) { - if constexpr (impl::hasReverseIteratorsV) + if constexpr (data::hasReverseIteratorsV) { return accumulate(list.rbegin(), list.rend(), init, unCurry flip | func); } @@ -1349,11 +1355,12 @@ constexpr auto foldr = toGFunc<3>([](auto func, auto init, auto const& list) constexpr auto foldl = toGFunc<3>([](auto func, auto init, auto const& list) { - return hspp::accumulate(list.begin(), list.end(), init, unCurry | func); + return data::accumulate(list.begin(), list.end(), init, unCurry | func); }); constexpr auto equalTo = toGFunc<2>(std::equal_to<>{}); +// constexpr inline auto elem = any equalTo; constexpr inline auto elem = toGFunc<2>([](auto t, auto const& c) { return std::any_of(c.begin(), c.end(), [t=std::move(t)](auto const& e){return e == t;}); @@ -1461,7 +1468,19 @@ constexpr inline auto makeTuple = toGFunc([](auto e, auto... l) return std::make_tuple(std::move(e), std::move(l)...); }); -/////////////// TypeClass ////////////////// +} // namespace data + +using data::o; +using data::_o_; +using data::_O_; +using data::flip; +using data::putStrLn; +using data::toFunc; +using data::toGFunc; +using data::unCurry; +using data::id; + +/////////////// TypeClass Traits ////////////////// template