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:
// Fibonacci numbers int fib(int n) { var<int> m; Match(n) { Case(1) return 1; Case(2) return 1; Case(2*m) return sqr(fib(m+1)) - sqr(fib(m-1)); Case(2*m+1) return sqr(fib(m+1)) + sqr(fib(m)); } EndMatch }
Add a Comment
Comments are closed.