Use Patterns for Control Flow
Pattern matching goes well beyond assigning variables. Control flow—the branching logic of your application—is performed with pattern matching. Most programming languages rely on if statements for all (or close to all) of an application’s control flow. But Elixir offers more choices.
We’ll go over four control flow structures: if, case, with, and cond. They all have different purposes and situations where you’ll use them, but case statements are the most commonly used.
Let’s start with if statements.
If Statements
If statements are so common that it would be a bit strange for Elixir to not have them. Elixir’s if statements work largely like you’re used to. Use if to quickly check whether a value is truthy or falsy. ...
Get From Ruby to Elixir now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.