Skip to content

feat: add pred(callable) — first-class predicate pattern#15

Merged
sentomk merged 3 commits intomainfrom
copilot/add-predicate-pattern-feature
Apr 24, 2026
Merged

feat: add pred(callable) — first-class predicate pattern#15
sentomk merged 3 commits intomainfrom
copilot/add-predicate-pattern-feature

Conversation

Copy link
Copy Markdown
Contributor

Copilot AI commented Apr 23, 2026

Adds pred(callable) as a first-class pattern that lifts an arbitrary unary predicate into the match phase, avoiding the need for post-bind guards (pattern[guard]) when no binding is required.

Changes

  • include/ptn/pattern/pred.hpp — New pred_pattern<Callable> CRTP type + pred() factory. Non-binding (empty binding_args), consistent with wildcard, any/all, etc.
  • include/ptn/patternia.hpp — Includes pred.hpp; exports ptn::pred.
  • CMakeLists.txt — Adds pred.hpp to PTN_HEADERS.
  • tests/tests_pred_pattern.cpp — 10 tests: match/fallback, string subjects, composition with any/all, reuse, PTN_ON macro, fully-qualified usage.
  • tests/CMakeLists.txt — Wires in the new test TU.
  • docs/roadmap.md — Promotes pred(callable) from WIP → FINISHED (v0.9.3).

Usage

match(x) | on(
  pred([](int v) { return v % 2 == 0; }) >> "even",
  _ >> "odd"
);

// Composes natively with any()/all()
match(x) | on(
  all(pred([](int v) { return v > 0; }),
      pred([](int v) { return v % 2 == 0; })) >> "positive even",
  _ >> "other"
);

Copilot AI linked an issue Apr 23, 2026 that may be closed by this pull request
2 tasks
Agent-Logs-Url: https://github.com/sentomk/patternia/sessions/0dce48e7-ecdc-43e2-a5f7-a61a05983338

Co-authored-by: sentomk <115908952+sentomk@users.noreply.github.com>
Copilot AI changed the title [WIP] Add predicate pattern support for matching feat: add pred(callable) — first-class predicate pattern Apr 23, 2026
Copilot AI requested a review from sentomk April 23, 2026 02:42
@sentomk sentomk requested a review from Copilot April 24, 2026 10:18
@sentomk sentomk marked this pull request as ready for review April 24, 2026 10:20
Copy link
Copy Markdown

Copilot AI left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Pull request overview

Adds a new first-class pred(callable) pattern to Patternia, enabling match-time evaluation of unary predicates as non-binding patterns (usable in on(...) and composable with any/all), along with build/test/docs wiring.

Changes:

  • Introduces include/ptn/pattern/pred.hpp implementing pred_pattern<Callable> and the pred() factory (non-binding).
  • Exports ptn::pred via the umbrella header and registers the header in CMake.
  • Adds a dedicated GTest TU and wires it into the tests build; updates the roadmap status.

Reviewed changes

Copilot reviewed 6 out of 6 changed files in this pull request and generated 2 comments.

Show a summary per file
File Description
include/ptn/pattern/pred.hpp New predicate pattern implementation + binding contract specialization.
include/ptn/patternia.hpp Includes pred.hpp and exports ptn::pred in the public API.
CMakeLists.txt Adds the new public header to PTN_HEADERS.
tests/tests_pred_pattern.cpp New unit tests covering basic matching and composition scenarios.
tests/CMakeLists.txt Adds the new test translation unit to ptn_tests.
docs/roadmap.md Marks pred(callable) as finished and references a new release entry.

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

Comment on lines +49 to +52
constexpr bool match(Subject &&subj) const noexcept(noexcept(
static_cast<bool>(fn(std::forward<Subject>(subj))))) {
return static_cast<bool>(fn(std::forward<Subject>(subj)));
}
Copy link

Copilot AI Apr 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

pred_pattern::match calls the stored callable via fn(...), which prevents using certain valid “callable” forms such as pointers-to-member functions/data members. If the intent is to lift any unary predicate, consider using std::invoke(fn, subj) (and include <functional>) so member pointers and std::reference_wrapper work as expected.

Copilot uses AI. Check for mistakes.
Comment thread docs/roadmap.md

## FINISHED

Completed in [v0.9.3](changelog/v0.9.3.md):
Copy link

Copilot AI Apr 24, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This roadmap entry links to changelog/v0.9.3.md, but that file doesn’t exist in docs/changelog/ (latest present is v0.9.2.md). Either add the v0.9.3 changelog file in this PR or change the link/text to reference an existing release to avoid a broken docs link.

Suggested change
Completed in [v0.9.3](changelog/v0.9.3.md):
Completed in v0.9.3:

Copilot uses AI. Check for mistakes.
@sentomk sentomk merged commit 3bffd68 into main Apr 24, 2026
4 checks passed
@sentomk sentomk deleted the copilot/add-predicate-pattern-feature branch April 30, 2026 12:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[Issue]: [Feature] pred(callable) — predicate pattern

3 participants