Skip to content

Commit

Permalink
Fixes example. Closes #40
Browse files Browse the repository at this point in the history
  • Loading branch information
wintermeyer committed Jan 25, 2024
1 parent a805346 commit a8087f2
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 7 deletions.
1 change: 1 addition & 0 deletions modules/ROOT/pages/acknowledgments.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ A lot of different people made it possible to create this book.
## Bug Hunters

- https://github.com/digitalcohesion[@digitalcohesion] 2023-09-29
- https://github.com/j0urneyK[@j0urneyK] 2024-01-24
14 changes: 7 additions & 7 deletions modules/ROOT/pages/elixir/operators/match-operator.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -221,19 +221,17 @@ Pattern matching with keyword lists is often used in function heads. Consider a
[source,elixir]
----
defmodule User do
def greet(name, opts \\ []) do
greet(name, opts)
end
def greet(name, opts \\ []) <1>
defp greet(name, [role: "admin"]) do
def greet(name, [role: "admin"]) do
"Welcome, #{name}. You have admin privileges."
end
defp greet(name, [role: "moderator"]) do
def greet(name, [role: "moderator"]) do
"Welcome, #{name}. You can moderate content."
end
defp greet(name, _) do
def greet(name, []) do
"Welcome, #{name}."
end
end
Expand All @@ -244,7 +242,9 @@ IO.puts User.greet("Bob", role: "admin") # Outputs: "Welcome, Bob. You have admi
IO.puts User.greet("Carol", role: "moderator") # Outputs: "Welcome, Carol. You can moderate content."
----
In this example, we define different greetings based on user roles. When calling the `greet` function, we can optionally provide a `role`. We have created private functions (`defp`) for each specific role we want to handle ("admin", "moderator"), and a fallback function for the general case.
<1> We define a `greet/2` function header with a default value for the second argument. The default value is an empty list `[]`.

In this example, we define different greetings based on user roles. When calling the `greet` function, we can optionally provide a `role`.
indexterm:[Pattern Matching, Keyword Lists, Roles]


Expand Down

0 comments on commit a8087f2

Please sign in to comment.