Elixir Control-Flow Structures
Working with functional programming, we use function clauses to control the flow of the program. It’s the expected behavior of a functional programmer, but it doesn’t mean we can’t use some Elixir built-in control-flow structures, such as case, cond, if, and unless, to develop features quickly. We’ll see how each one can be useful and how it works.
Case: Control with Pattern Matching
case is useful when we want to check an expression with multiple pattern-matching clauses. It’s helpful for dealing with functions that may have an unexpected effect. To see how it works, we’ll change our script that calculates the abilities modifier for RPG players:
| user_input ... |
Get Learn Functional Programming with 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.