LoginSignup
4
1

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)> [3.14, :pie, "Apple"]
[3.14, :pie, "Apple"]
iex(2)> list = [3.14, :pie, "Apple"]
[3.14, :pie, "Apple"]
iex(3)> ["π" | list]
["π", 3.14, :pie, "Apple"]
iex(4)> list ++ ["Cherry"]
[3.14, :pie, "Apple", "Cherry"]
iex(5)> [1, 2] ++ [3, 4, 1]
[1, 2, 3, 4, 1]
iex(6)> ["foo", :bar, 42] -- [42, "bar"]
["foo", :bar]
iex(7)> [1,2,2,3,2,3] -- [1,2,3,2]
[2, 3]
iex(8)> [2] -- [2.0]
[2]
iex(9)> [2.0] -- [2.0]
[]
iex(10)> hd [3.14, :pie, "Apple"]
3.14
iex(11)> tl [3.14, :pie, "Apple"]
[:pie, "Apple"]
iex(12)> [head | tail] = [3.14, :pie, "Apple"]
[3.14, :pie, "Apple"]
iex(13)> head
3.14
iex(14)> tail
[:pie, "Apple"]
iex(15)> {3.14, :pie, "Apple"}
{3.14, :pie, "Apple"}
iex(16)> File.read("./file")
{:ok, ""}
iex(17)> File.read("./file2")
{:error, :enoent}
iex(18)> [foo: "bar", hello: "world"]
[foo: "bar", hello: "world"]
iex(19)> [{:foo, "bar"}, {:hello, "world"}]
[foo: "bar", hello: "world"]
iex(20)> map = %{:foo => "bar", "hello" => :world}
%{:foo => "bar", "hello" => :world}
iex(21)> map[:foo]
"bar"
iex(22)> map["hello"]
:world
iex(23)> key = "hello"
"hello"
iex(24)> %{key => "world"}
%{"hello" => "world"}
iex(25)> %{:foo => "bar", :foo => "hello world"}
warning: key :foo will be overridden in map
  iex:25

%{foo: "hello world"}
iex(26)> %{foo: "bar", hello: "world"}
%{foo: "bar", hello: "world"}
iex(27)> %{foo: "bar", hello: "world"} == %{:foo => "bar", :hello => "world"}
true
iex(28)> map = %{foo: "bar", hello: "world"}
%{foo: "bar", hello: "world"}
iex(29)> map.hello
"world"
iex(30)> map = %{foo: "bar", hello: "world"}
%{foo: "bar", hello: "world"}
iex(31)> %{map | foo: "baz"}
%{foo: "baz", hello: "world"}
iex(32)> map = %{hello: "world"}
%{hello: "world"}
iex(33)> %{map | foo: "baz"}
** (KeyError) key :foo not found in: %{hello: "world"}
    (stdlib 3.17) :maps.update(:foo, "baz", %{hello: "world"})
    (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(33)> Map.put(map, :foo, "baz")
%{foo: "baz", hello: "world"}
iex(34)> 

まとめ

何かの役に立てばと。

4
1
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
4
1