3 ms·
Hm that’s if expressions.
by slver 5y ago
Hm that’s if expressions.
- dnautics 5y agotechnically, they are case statements. Even the if is just sugar over case.
- weatherlight 5y agohttps://erlang.org/doc/reference_manual/expressions.html#pattern https://erlang.org/doc/reference_manual/expressions.html#pat... it's more like the = sign in math mathematics. 2x = 10 we can deduce x = 5 Here's an elixir example where a bunch of "if/else" blocks are refactored to use case statements, and then from case statements to pattern matching. https://nickjanetakis.com/blog/refactoring-elixir-code-if-cond-and-pattern-matching https://nickjanetakis.com/blog/refactoring-elixir-code-if-co...
- saba2008 5y agoAnd if statement is just syntax sugar over goto. After using mostly Erlang for several years, 'if' feels like ugly atavism.
- cannabis_sam 5y agoNo..? If-expressions is a specialized version that only works on booleans, pattern matching is a more general concept (that of course can be used to implement if-expressions, but so can pure lambda calculus)
- travisgriggs 5y agoI did not mean to imply that there are no if blocks in Erlang/Elixir. Just that I use them a whole lot less. Probably an order of magnitude less. And you still have to decide where your code goes at some level. The pattern of having two function variants (one which matches an empty list, and one the head/tail) is a different idiom to looping. So while for loops go away, you still traverse sequences. And more generally, a lot of the if(else) clauses I would normally write, now get solved with pattern matching. With control flow languages, the where the code goes and what the code does feel very intertwined, whereas with the pattern matching idioms, I find that where it goes, and what it does, are more orthogonally declared.