Last active
August 29, 2015 14:26
-
-
Save takuoka/2a3e97731ffae8897239 to your computer and use it in GitHub Desktop.
Elixirのデータ構造的なやつまとめ ref: http://qiita.com/taku_oka/items/083aa97f17a7e552d74b
This file contains hidden or 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
iex> [a: a] = [a: 1] | |
[a: 1] | |
iex> a | |
1 | |
iex> [a: a] = [a: 1, b: 2] | |
** (MatchError) no match of right hand side value: [a: 1, b: 2] | |
iex> [b: b, a: a] = [a: 1, b: 2] | |
** (MatchError) no match of right hand side value: [a: 1, b: 2] |
This file contains hidden or 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
iex> %{} = %{:a => 1, 2 => :b} | |
%{:a => 1, 2 => :b} | |
iex> %{:a => a} = %{:a => 1, 2 => :b} | |
%{:a => 1, 2 => :b} | |
iex> a | |
1 | |
iex> %{:c => c} = %{:a => 1, 2 => :b} | |
** (MatchError) no match of right hand side value: %{2 => :b, :a => 1} |
This file contains hidden or 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
iex> keyword = [] | |
[] | |
iex> map = %{} | |
%{} | |
iex> Dict.put(keyword, :a, 1) | |
[a: 1] | |
iex> Dict.put(map, :a, 1) | |
%{a: 1} |
This file contains hidden or 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
iex> Enum.reduce([1, 2, 3], 0, fn(x, acc) -> x + acc end) | |
6 | |
iex> Enum.map([1, 2, 3], fn(x) -> x * 2 end) | |
[2, 4, 6] |
This file contains hidden or 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
1..100_000 |> Enum.map(&(&1 * 3)) |> Enum.filter(odd?) |> Enum.sum |
This file contains hidden or 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 User do | |
defstruct name: "john", age: 27 | |
end |
This file contains hidden or 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
> john = %User{} #インスタンス生成 | |
%User{age: 27, name: "john"} | |
> john.name #アクセス | |
"john" | |
> meg = %{john | name: "meg"} #値の変更 | |
%User{age: 27, name: "meg"} | |
> %{meg | oops: :field} #マップと同じく存在しないキーには変更できない | |
** (ArgumentError) argument error |
This file contains hidden or 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
iex> %User{name: name} = john | |
%User{age: 27, name: "john"} | |
iex> name | |
"john" | |
iex> %User{} = %{} | |
** (MatchError) no match of right hand side value: %{} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment