Mach7: Pattern Matching for C++

Here is a new library to perform pattern matching:

Mach7: Pattern Matching for C++

by Yuriy Solodkyy, Gabriel Dos Reis and Bjarne Stroustrup

From the article:

Pattern matching is an abstraction mechanism that can greatly simplify source code. Commonly, pattern matching is built into a language to provide better syntax, faster code, correctness guarantees and improved diagnostics. Mach7 is a library solution to pattern matching in C++ that maintains many of these features. All the patterns in Mach7 are user-definable, can be stored in variables, passed among functions, and allow the use of open class hierarchies…

Example:

  1. // Fibonacci numbers
  2. int fib(int n)
  3. {
  4.     var<int> m;
  5.  
  6.     Match(n)
  7.     {
  8.       Case(1)     return 1;
  9.       Case(2)     return 1;
  10.       Case(2*m)   return sqr(fib(m+1)) - sqr(fib(m-1));
  11.       Case(2*m+1) return sqr(fib(m+1)) + sqr(fib(m));
  12.     }
  13.     EndMatch
  14. }

Add a Comment

Comments are closed.

Comments (1)

0 0

grizzlysmit said on Oct 10, 2017 10:04 PM:

I like this Idea a way we can extend the types switch copes with, without messing with switch and potentially breaking code, it would be nice to have something other than break to end a case (breakcase ??), so break can still break the containing loop, also a synonym for case would be good same case but takes only one statement and doesn't need a break or what ever (breakcase??)