Skip to content

Commit

Permalink
Change predicate names
Browse files Browse the repository at this point in the history
  • Loading branch information
TartanLlama committed Dec 9, 2024
1 parent 0378f58 commit 85c6bb3
Show file tree
Hide file tree
Showing 2 changed files with 36 additions and 24 deletions.
22 changes: 14 additions & 8 deletions include/tl/functional/predicates.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<decltype(f)>(f), p = std::forward<decltype(p)>(p)](auto&&... args) {
return std::invoke(f, std::invoke(p, std::forward<decltype(args)>(args)...));
};
};

inline constexpr auto eq = [](auto&& x) {
return [x = std::forward<decltype(x)>(x)](auto&& y) {
return x == y;
};
};
inline constexpr auto not_equal_to = [](auto&& x) {
inline constexpr auto neq = [](auto&& x) {
return [x = std::forward<decltype(x)>(x)](auto&& y) {
return x != y;
};
};
inline constexpr auto less_than = [](auto&& x) {
inline constexpr auto lt = [](auto&& x) {
return [x = std::forward<decltype(x)>(x)](auto&& y) {
return y < x;
};
};
inline constexpr auto greater_than = [](auto&& x) {
inline constexpr auto gt = [](auto&& x) {
return [x = std::forward<decltype(x)>(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<decltype(x)>(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<decltype(x)>(x)](auto&& y) {
return y >= x;
};
Expand Down Expand Up @@ -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<decltype(predicates)>(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<decltype(predicates)>(predicates)...)](auto&& x) {
return std::apply([&x](auto&&... predicates) {
return (predicates(x) || ...);
Expand Down
38 changes: 22 additions & 16 deletions tests/predicates.cpp
Original file line number Diff line number Diff line change
@@ -1,41 +1,41 @@
#include <catch2/catch.hpp>
#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));
Expand Down Expand Up @@ -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));
Expand All @@ -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);
}

0 comments on commit 85c6bb3

Please sign in to comment.