5
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

Elixirの対話モードでキーワードリストとマップの動作を確認してみた

Posted at

概要

Elixirの対話モードでキーワードリストとマップの動作を確認してみました。以下のページを参考にしました。

対話モードで実行

以下のコマンドを実行しました。

$ iex
Erlang/OTP 24 [erts-12.2.1] [source] [64-bit] [smp:4:4] [ds:4:4:10] [async-threads:1] [jit]

Interactive Elixir (1.12.2) - press Ctrl+C to exit (type h() ENTER for help)
iex(1)> list = [{:a, 1}, {:b, 2}]
[a: 1, b: 2]
iex(2)> list == [a: 1, b: 2]
true
iex(3)> list ++ [c: 3]
[a: 1, b: 2, c: 3]
iex(4)> [a: 0] ++ list
[a: 0, a: 1, b: 2]
iex(5)> new_list = [a: 0] ++ list
[a: 0, a: 1, b: 2]
iex(6)> new_list[:a]
0
iex(7)> query = from w in Weather,
...(7)> where: w.prcp > 0,
...(7)> where: w.temp < 20,
...(7)> select: w
** (CompileError) iex:7: undefined function from/2

iex(7)> if(false, do: :this, else: :that)
:that
iex(8)> if(false, [do: :this, else: :that])
:that
iex(9)> if(false, [{:do, :this}, {:else, :that}])
:that
iex(10)> [a: a] = [a: 1]
[a: 1]
iex(11)> a
1
iex(12)> [a: a] = [a: 1, b: 2]
** (MatchError) no match of right hand side value: [a: 1, b: 2]

iex(12)> [b: b, a: a] = [a: 1, b: 2]
** (MatchError) no match of right hand side value: [a: 1, b: 2]

iex(12)> map = %{:a => 1, 2 => :b}
%{2 => :b, :a => 1}
iex(13)> map[:a]
1
iex(14)> map[2]
:b
iex(15)> map[:c]
nil
iex(16)> %{} = %{:a => 1, 2 => :b}
%{2 => :b, :a => 1}
iex(17)> %{:a => a} = %{2 => :b, :a => 1}
%{2 => :b, :a => 1}
iex(18)> a
1
iex(19)> %{:c => c} = %{:a => 1, 2 => :b}
** (MatchError) no match of right hand side value: %{2 => :b, :a => 1}

iex(19)> n = 1
1
iex(20)> map = %{n => :one}
%{1 => :one}
iex(21)> map[n]
:one
iex(22)> %{^n => :one} = %{1 => :one, 2 => :two, 3 => :three}
%{1 => :one, 2 => :two, 3 => :three}
iex(23)> map = %{:a => 1, 2 => :b}
%{2 => :b, :a => 1}
iex(24)> Map.get(map, :a)
1
iex(25)> Map.put(map, :c, 3)
%{2 => :b, :a => 1, :c => 3}
iex(26)> Map.to_list(map)
[{2, :b}, {:a, 1}]
iex(27)> map = %{:a => 1, 2 => :b}
%{2 => :b, :a => 1}
iex(28)> %{map | 2 => :two}
%{2 => :two, :a => 1}
iex(29)> %{map | :c => 3}
** (KeyError) key :c not found in: %{2 => :b, :a => 1}
    (stdlib 3.17) :maps.update(:c, 3, %{2 => :b, :a => 1})
    (stdlib 3.17) erl_eval.erl:256: anonymous fn/2 in :erl_eval.expr/5
    (stdlib 3.17) lists.erl:1267: :lists.foldl/3
iex(29)> map = %{:a => 1, 2 => :b}
%{2 => :b, :a => 1}
iex(30)> Map.put(map, :c, 3)
%{2 => :b, :a => 1, :c => 3}
iex(31)> %{:a => 1, 2 => :b, :a => :one}
warning: key :a will be overridden in map
  iex:31

%{2 => :b, :a => :one}
iex(32)> %{:a => 1, :b => 2}
%{a: 1, b: 2}
iex(33)> map = %{:a => 1, 2 => :b}
%{2 => :b, :a => 1}
iex(34)> map.a
1
iex(35)> map.2
** (SyntaxError) iex:35:5: syntax error before: "2"

iex(35)> map[2]
:b
iex(36)> map.c
** (KeyError) key :c not found in: %{2 => :b, :a => 1}

iex(36)> users = [
...(36)> john: %{name: "John", age: 27, languages: ["Erlang", "Ruby", "Elixir"]},
...(36)> mary: %{name: "Mary", age: 29, languages: ["Elixir", "F#", "Clojure"]}
...(36)> ]
[
  john: %{age: 27, languages: ["Erlang", "Ruby", "Elixir"], name: "John"},
  mary: %{age: 29, languages: ["Elixir", "F#", "Clojure"], name: "Mary"}
]
iex(37)> users[:mary]
%{age: 29, languages: ["Elixir", "F#", "Clojure"], name: "Mary"}
iex(38)> users[:john].age
27
iex(39)> users = put_in(users[:john].age, 31)
[
  john: %{age: 31, languages: ["Erlang", "Ruby", "Elixir"], name: "John"},
  mary: %{age: 29, languages: ["Elixir", "F#", "Clojure"], name: "Mary"}
]
iex(40)> users[:john].age
31
iex(41)> users = update_in(users[:mary].languages, fn languages ->
...(41)> List.delete(languages, "Clojure")
...(41)> end)
[
  john: %{age: 31, languages: ["Erlang", "Ruby", "Elixir"], name: "John"},
  mary: %{age: 29, languages: ["Elixir", "F#"], name: "Mary"}
]
iex(42)> users[:mary].languages
["Elixir", "F#"]
iex(43)> 

まとめ

何かの役に立てばと。

5
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?