From 85c6bb3cd81b77d8e9cc58a39f7da35423adec50 Mon Sep 17 00:00:00 2001 From: Sy Brand Date: Mon, 9 Dec 2024 11:01:13 +0000 Subject: [PATCH] Change predicate names --- include/tl/functional/predicates.hpp | 22 ++++++++++------ tests/predicates.cpp | 38 ++++++++++++++++------------ 2 files changed, 36 insertions(+), 24 deletions(-) diff --git a/include/tl/functional/predicates.hpp b/include/tl/functional/predicates.hpp index 3cf00be..b176bb6 100644 --- a/include/tl/functional/predicates.hpp +++ b/include/tl/functional/predicates.hpp @@ -2,32 +2,38 @@ #define TL_RANGES_PREDICATES_HPP namespace tl { - inline constexpr auto equal_to = [](auto&& x) { + inline constexpr auto proj = [](auto&& f, auto&& p) { + return [f = std::forward(f), p = std::forward(p)](auto&&... args) { + return std::invoke(f, std::invoke(p, std::forward(args)...)); + }; + }; + + inline constexpr auto eq = [](auto&& x) { return [x = std::forward(x)](auto&& y) { return x == y; }; }; - inline constexpr auto not_equal_to = [](auto&& x) { + inline constexpr auto neq = [](auto&& x) { return [x = std::forward(x)](auto&& y) { return x != y; }; }; - inline constexpr auto less_than = [](auto&& x) { + inline constexpr auto lt = [](auto&& x) { return [x = std::forward(x)](auto&& y) { return y < x; }; }; - inline constexpr auto greater_than = [](auto&& x) { + inline constexpr auto gt = [](auto&& x) { return [x = std::forward(x)](auto&& y) { return y > x; }; }; - inline constexpr auto less_than_or_equal_to = [](auto&& x) { + inline constexpr auto lte = [](auto&& x) { return [x = std::forward(x)](auto&& y) { return y <= x; }; }; - inline constexpr auto greater_than_or_equal_to = [](auto&& x) { + inline constexpr auto gte = [](auto&& x) { return [x = std::forward(x)](auto&& y) { return y >= x; }; @@ -68,14 +74,14 @@ namespace tl { inline constexpr auto is_not_empty = [](auto&& x) { return !x.empty(); }; - inline constexpr auto conjunction = [](auto&&... predicates) { + inline constexpr auto both = [](auto&&... predicates) { return [predicates = std::make_tuple(std::forward(predicates)...)](auto&& x) { return std::apply([&x](auto&&... predicates) { return (predicates(x) && ...); }, predicates); }; }; - inline constexpr auto disjunction = [](auto&&... predicates) { + inline constexpr auto either = [](auto&&... predicates) { return [predicates = std::make_tuple(std::forward(predicates)...)](auto&& x) { return std::apply([&x](auto&&... predicates) { return (predicates(x) || ...); diff --git a/tests/predicates.cpp b/tests/predicates.cpp index 23296b8..f0e5fb7 100644 --- a/tests/predicates.cpp +++ b/tests/predicates.cpp @@ -1,41 +1,41 @@ #include #include "tl/functional/predicates.hpp" -TEST_CASE("equal_to") { - auto eq = tl::equal_to(5); +TEST_CASE("eq") { + auto eq = tl::eq(5); REQUIRE(eq(5)); REQUIRE_FALSE(eq(6)); } -TEST_CASE("not_equal_to") { - auto neq = tl::not_equal_to(5); +TEST_CASE("neq") { + auto neq = tl::neq(5); REQUIRE_FALSE(neq(5)); REQUIRE(neq(6)); } -TEST_CASE("less_than") { - auto lt = tl::less_than(5); +TEST_CASE("lt") { + auto lt = tl::lt(5); REQUIRE(lt(4)); REQUIRE_FALSE(lt(5)); REQUIRE_FALSE(lt(6)); } -TEST_CASE("greater_than") { - auto gt = tl::greater_than(5); +TEST_CASE("gt") { + auto gt = tl::gt(5); REQUIRE_FALSE(gt(4)); REQUIRE_FALSE(gt(5)); REQUIRE(gt(6)); } -TEST_CASE("less_than_or_equal_to") { - auto lte = tl::less_than_or_equal_to(5); +TEST_CASE("lte") { + auto lte = tl::lte(5); REQUIRE(lte(4)); REQUIRE(lte(5)); REQUIRE_FALSE(lte(6)); } -TEST_CASE("greater_than_or_equal_to") { - auto gte = tl::greater_than_or_equal_to(5); +TEST_CASE("gte") { + auto gte = tl::gte(5); REQUIRE_FALSE(gte(4)); REQUIRE(gte(5)); REQUIRE(gte(6)); @@ -121,16 +121,16 @@ TEST_CASE("is_not_null") { REQUIRE(inn(&inn)); } -TEST_CASE("conjunction") { - auto c = tl::conjunction(tl::is_even, tl::is_positive); +TEST_CASE("both") { + auto c = tl::both(tl::is_even, tl::is_positive); REQUIRE_FALSE(c(0)); REQUIRE_FALSE(c(1)); REQUIRE(c(2)); REQUIRE(c(4)); } -TEST_CASE("disjunction") { - auto d = tl::disjunction(tl::is_even, tl::is_positive); +TEST_CASE("either") { + auto d = tl::either(tl::is_even, tl::is_positive); REQUIRE(d(0)); REQUIRE(d(1)); REQUIRE(d(2)); @@ -149,3 +149,9 @@ TEST_CASE("negation") { REQUIRE_FALSE(n(4)); } +TEST_CASE("proj") { + auto p = tl::proj([](int x) { return x + 1; }, [](int x) { return x + 2; }); + REQUIRE(p(1) == 4); + REQUIRE(p(2) == 5); + REQUIRE(p(3) == 6); +} \ No newline at end of file