Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Partial application #3

Closed
3 tasks
yvt opened this issue Mar 17, 2023 · 2 comments
Closed
3 tasks

Partial application #3

yvt opened this issue Mar 17, 2023 · 2 comments
Labels
enhancement New feature or request tracking issue

Comments

@yvt
Copy link
Owner

yvt commented Mar 17, 2023

A new macro that acts like a curried function (e.g., matcher!($pat)($expr) == try_match!($expr, $pat)) could be useful in some situations.

Issues

  • What should it be called? What about syntax? A few candidates:
    • matcher!($pat)
    • try_matcher!($pat)
    • try_match!(_, $pat), try_match!(, $pat), try_match!($pat)
  • Should it return Option<MatchedVars> or Result<MatchedVars, Original>?
  • Should the if guard and the success expression (=> $expr) capture outer variables by move?

Examples

Iterator::filter_map

Assumes the closure returns Option<MatchedVars>

let array = [Var1(42), Var2, Var1(10)];
let filtered: Vec<_> = array
    .iter()
//  .filter_map(|x| try_match!(x, &Var1(_0) if _0 > 20).ok())
    .filter_map(matcher!(&Var1(_0) if _0 > 20))
    .collect();
assert_eq!(filtered, [42]);

Assumes the closure returns Result<MatchedVars, Original>

let array = [Var1(42), Var2, Var1(10)];
let filtered: Vec<_> = array
    .iter()
//  .filter_map(|x| try_match!(x, &Var1(_0) if _0 > 20).ok())
    .map(matcher!(&Var1(_0) if _0 > 20))
    .filter_map(Result::ok)
    .collect();
assert_eq!(filtered, [42]);

Applicable use cases in the wild: Tamschi/quote-miner, yvt/Stella2

Iterator::map + Fallible Iterator::collect

Assumes the closure returns Result<MatchedVars, Original>

let array = [Var1(42), Var2, Var1(10)];
let filtered: Result<Vec<_>, _> = array
    .iter()
    .map(matcher!(&Var1(_0) if _0 > 20))
    .collect();
// `Var2` is the first value that doesn't match
assert_eq!(filtered, Err(&Var2));
@yvt yvt added the enhancement New feature or request label Mar 17, 2023
@yvt
Copy link
Owner Author

yvt commented May 3, 2023

Unstably implemented in commit 4950789, using the signature try_match!(, $pat) (inspired by GHC tuple sections)

@yvt yvt changed the title Add matcher! Partial application May 4, 2023
@yvt
Copy link
Owner Author

yvt commented May 4, 2023

Change of plan: Insta-stabilized the feature in commit 7e3beed. Since it's not an ecosystem kind of crate, we can just increase the major version anytime without causing an ecosystem split.

@yvt yvt closed this as completed May 4, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request tracking issue
Projects
None yet
Development

No branches or pull requests

1 participant