3 ms·
All these complicated examples and no mention of functional pattern matching or pattern matching at all. IMHO These are by far the two most important ways to g
by mikkom 10y ago
All these complicated examples and no mention of functional pattern matching or pattern matching at all.
IMHO These are by far the two most important ways to get rid of complex code embedded with ifs.
One very good example of this is erlangs factorial method.
factorial(0) -> 1;
factorial(N) -> N * factorial(N-1).
There are many other nice examples at the erlang documentation:
https://www.erlang.org/course/sequential-programming#funcsyntax https://www.erlang.org/course/sequential-programming#funcsyn...
- majewsky 10y agoTrue functional-style pattern matching is only slowly coming into imperative languages right now, most prominently in Rust.
- chriswarbo 10y agoUnfortunately, pattern matching isn't available in many languages (or at least, including a new dependency for the sake of removing `if`s may not be worth it). On the other hand, the article recommends using sub-type polymorphism, which is also unavailable in many languages.