Skip to content

Instantly share code, notes, and snippets.

@takuoka
Last active August 29, 2015 14:26
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save takuoka/2a3e97731ffae8897239 to your computer and use it in GitHub Desktop.
Save takuoka/2a3e97731ffae8897239 to your computer and use it in GitHub Desktop.
Elixirのデータ構造的なやつまとめ ref: http://qiita.com/taku_oka/items/083aa97f17a7e552d74b
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]
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}
iex> keyword = []
[]
iex> map = %{}
%{}
iex> Dict.put(keyword, :a, 1)
[a: 1]
iex> Dict.put(map, :a, 1)
%{a: 1}
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]
1..100_000 |> Enum.map(&(&1 * 3)) |> Enum.filter(odd?) |> Enum.sum
defmodule User do
defstruct name: "john", age: 27
end
> 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
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