Last active
November 28, 2016 07:26
-
-
Save lubien/021c0d11e534187da2b868e6429e04c8 to your computer and use it in GitHub Desktop.
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
defmodule Pinner do | |
def first do | |
case 10 do | |
# `x` sempre dá match porque sendo ele | |
# uma variável, representa-se que qualquer | |
# valor arbitrário pode ser aceito no caso | |
x -> | |
"x" | |
10 -> | |
"10" | |
end | |
end | |
# agora nós queremos checar se 11 é `x` | |
def second do | |
x = 11 | |
case 10 do | |
# mais uma vez o `x` sempre dá match | |
# uma vez que este `x` não é o mesmo do | |
# escopo parente, mas sim dizendo que | |
# estamos tentando dar match de 10 | |
# em um identificador `x` (que coincidentemente | |
# é o nome de uma variável no escopo parente) | |
x -> | |
"11" | |
10 -> | |
"10" | |
end | |
end | |
def third do | |
x = 11 | |
case 10 do | |
# usando o operador pin (^) nós dizemos | |
# que não estamos atribuindo 10 a uma | |
# variável `x` no caso abaixo, mas sim | |
# que queremos testar se o valor de 10 | |
# é o mesmo que o valor de `x` | |
^x -> | |
"11" | |
10 -> | |
"10" | |
end | |
end | |
end | |
IO.puts Pinner.first | |
IO.puts Pinner.second | |
IO.puts Pinner.third |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment