Skip to content

Instantly share code, notes, and snippets.

@sukechannnn
Last active July 7, 2017 07:23
Show Gist options
  • Save sukechannnn/3cf55be7bda8309f424471e33616dea0 to your computer and use it in GitHub Desktop.
Save sukechannnn/3cf55be7bda8309f424471e33616dea0 to your computer and use it in GitHub Desktop.

簡単なパターンマッチ

a = 1
a + 3

1 = a
2 = a

ちょっと複雑なパターンマッチ

list = [1, 2, 3]
[a, b, c] = list
a
b
c
list = [1, 2, [3, 4, 5]]
[a, b, c] = list
list = [1, 2, 3]
[a, 2, b] = list

アンダースコア

[1, _, _] = [1, 2, 3]
[1, _, _] = [1, "cat", "dog"]

束縛し直しができるが、束縛し直すのを防ぐこともできる

a = 1
a = 2
a^ = 1
a = 2

ファイル開いたりとか便利

{ :ok, file } = File.read("./example.txt")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment