-
-
Notifications
You must be signed in to change notification settings - Fork 401
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
f98d44f
commit b9bd559
Showing
5 changed files
with
56 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
# Multiple clause functions | ||
|
||
```elixir | ||
defmodule Year do | ||
@spec leap_year?(non_neg_integer) :: boolean | ||
def leap_year?(year) when rem(year, 400) == 0, do: true | ||
def leap_year?(year) when rem(year, 100) == 0, do: false | ||
def leap_year?(year) when rem(year, 4) == 0, do: true | ||
def leap_year?(_), do: false | ||
end | ||
``` | ||
|
||
In Elixir, functions can have multiple clauses. | ||
Which one will be executed depends on parameter matching and guards. | ||
When a function with multiple clauses is invoked, the parameters are compared to the definitions in the order in which they were defined, and only the first one matching will be invoked. | ||
|
||
While in the [operators approach][operators-approach], it was possible to reorder expressions as long as the suitable boolean operators were used, in this approach, there is only one correct order of definitions. | ||
|
||
In our case, the three guards in the function clauses are as follows: | ||
|
||
```elixir | ||
when rem(year, 400) == 0 | ||
when rem(year, 100) == 0 | ||
when rem(year, 4) == 0 | ||
``` | ||
|
||
But because of the order they are evaluated in, they are equivalent to: | ||
|
||
```elixir | ||
when rem(year, 400) == 0 | ||
when rem(year, 100) == 0 and not rem(year, 400) == 0 | ||
when rem(year, 4) == 0 and not rem(year, 100) == 0 and not rem(year, 400) = 0 | ||
``` | ||
|
||
The final clause, `def leap_year?(_), do: false`, returns false if previous clauses are not a match. | ||
|
||
## Guards | ||
|
||
The [guards][guards] are part of the pattern-matching mechanism. | ||
They allow for more complex checks of values. | ||
However, because of when they are executed to allow the compiler to perform necessary optimization, | ||
only a minimal subset of operations are permitted. | ||
`Kernel.rem/2` is on this limited list, and `Integer.mod/2` is not. | ||
This is why, in this approach, only the first one will work, and the latter will not. | ||
|
||
In this approach, the boolean operators matter too. Only the strict ones, `not`, `and`, `or` are allowed. | ||
The relaxed `!`, `&&`, `||` will fail to compile. | ||
|
||
[operators-approach]: https://exercism.org/tracks/elixir/exercises/leap/approaches/operators | ||
[guards]: https://hexdocs.pm/elixir/main/patterns-and-guards.html#guards |
2 changes: 1 addition & 1 deletion
2
...ctice/leap/.approaches/guards/snippet.txt → ...ce/leap/.approaches/functions/snippet.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
def leap_year?(year) when rem(year, 400) == 0, do: true | ||
def leap_year?(year) when rem(year, 100) == 0, do: false | ||
def leap_year?(year) when rem(year, 4) == 0, do: true | ||
def leap_year?(_), do: false | ||
def leap_year?(_), do: false |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters