4
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の対話モードでErlangライブラリの動作を確認してみた

Posted at

概要

Elixirの対話モードでErlangライブラリの動作を確認してみました。以下のページを参考にしました。

対話モードで実行

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

$ 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)> String.to_charlist("π")
[960]
iex(2)> :binary.bin_to_list("π")
[207, 128]
iex(3)> :io.format("Pi is approximately given by:~10.3f~n", [:math.pi])
Pi is approximately given by:     3.142
:ok
iex(4)> to_string :io_lib.format("Pi is approximately given by:~10.3f~n", [:math.pi])
"Pi is approximately given by:     3.142\n"
iex(5)> :io.format("円周率πは約~10.3f~n", [:math.pi])
åå¨çÏã¯ç´
              3.142
:ok
iex(6)> to_string :io_lib.format("円周率πは約:~10.3f~n", [:math.pi])
<<195, 165, 194, 134, 194, 134, 195, 165, 194, 145, 194, 168, 195, 167, 194,
  142, 194, 135, 195, 143, 194, 128, 195, 163, 194, 129, 194, 175, 195, 167,
  194, 180, 194, 132, 58, 32, 32, 32, 32, 32, 51, 46, 49, 52, 50, 10>>
iex(7)> Base.encode16(:crypto.hash(:sha256, "Elixir"))
"3315715A7A3AD57428298676C5AE465DADA38D951BDFAC9348A8A31E9C7401CB"
iex(8)> def application do
...(8)> [extra_applications: [:crypto]]
...(8)> end
** (ArgumentError) cannot invoke def/2 outside module
    (elixir 1.12.2) lib/kernel.ex:5963: Kernel.assert_module_scope/3
    (elixir 1.12.2) lib/kernel.ex:4699: Kernel.define/4
    (elixir 1.12.2) expanding macro: Kernel.def/2
    iex:8: (file)
iex(8)> digraph = :digraph.new()
{:digraph, #Reference<0.1816867024.1547042817.255620>,
 #Reference<0.1816867024.1547042817.255621>,
 #Reference<0.1816867024.1547042817.255622>, true}
iex(9)> coords = [{0.0, 0.0}, {3.0, 0.0}, {3.0, 4.0}]
[{0.0, 0.0}, {3.0, 0.0}, {3.0, 4.0}]
iex(10)> [v0, v1, v2] = (for c <- coords, do: :digraph.add_vertex(digraph, 
...(10)> c))
[{0.0, 0.0}, {3.0, 0.0}, {3.0, 4.0}]
iex(11)> :digraph.add_edge(digraph, v0, v1)
[:"$e" | 0]
iex(12)> :digraph.add_edge(digraph, v1, v2)
[:"$e" | 1]
iex(13)> :digraph.add_edge(digraph, v0, v2)
[:"$e" | 2]
iex(14)> :digraph.get_short_path(digraph, v0, v2)
[{0.0, 0.0}, {3.0, 4.0}]
iex(15)> table = :ets.new(:ets_test, [])
#Reference<0.1816867024.1547042817.255689>
iex(16)> :ets.insert(table, {"China", 1_390_080_000})
true
iex(17)> :ets.insert(table, {"USA", 325_890_000})
true
iex(18)> :ets.insert(table, {"Japan", 126_750_000})
true
iex(19)> :ets.i(table)
<1   > {<<"Japan">>,126750000}
<2   > {<<"USA">>,325890000}
<3   > {<<"China">>,1390080000}
EOT  (q)uit (p)Digits (k)ill /Regexp -->
EOT  (q)uit (p)Digits (k)ill /Regexp -->q
:ok
iex(20)> deg_to_rad = :math.pi() / 180
0.017453292519943295
iex(21)> sin = :math.sin(60 * deg_to_rad)
0.8660254037844386
iex(22)> sqrt_3 = :math.sqrt(3)
1.7320508075688772
iex(23)> sin == sqrt_3 / 2
true
iex(24)> :math.atan2(sqrt_3, 1) / deg_to_rad
59.99999999999999
iex(25)> x = :math.exp(10)
22026.465794806718
iex(26)> :math.log(x)
10.0
iex(27)> q = :queue.new
{[], []}
iex(28)> q = :queue.in("A", q)
{["A"], []}
iex(29)> q = :queue.in("B", q)
{["B"], ["A"]}
iex(30)> {value, q} = :queue.out(q)
{{:value, "A"}, {[], ["B"]}}
iex(31)> value
{:value, "A"}
iex(32)> {value, q} = :queue.out(q)
{{:value, "B"}, {[], []}}
iex(33)> value
{:value, "B"}
iex(34)> {value, q} = :queue.out(q)
{:empty, {[], []}}
iex(35)> value
:empty
iex(36)> :rand.uniform()
0.869295279706314
iex(37)> _ = :rand.seed(:exs1024, {123, 123534, 345345})
{%{
   jump: #Function<12.92093067/1 in :rand."-fun.exs1024_jump/1-">,
   max: 18446744073709551615,
   next: #Function<11.92093067/1 in :rand."-fun.exs1024_next/1-">,
   type: :exs1024
 },
 {[1777391367797874666, 1964529382746821925, 7996041688159811731,
   16797603918550466679, 13239206057622895956, 2190120427146910527,
   18292739386017762693, 7995684206500985125, 1619687243448614582,
   961993414031414042, 10239938031393579756, 12249841489256032092,
   1457887945073169212, 16031477380367994289, 12526413104181201380,
   16202025130717851397], []}}
iex(38)> :rand.uniform()
0.5820506340260994
iex(39)> :rand.uniform(6)
6
iex(40)> :zip.foldl(fn _, _, _, acc -> acc + 1 end, 0, :binary.bin_to_list("samples.zip"))
{:error, :enoent}
iex(41)> song = "
...(41)> The snow glows white on the mountain tonight
...(41)> Not a footprint to be seen
...(41)> A kingdom of isolation
...(41)> And it looks like I'm the queen"
"\nThe snow glows white on the mountain tonight\nNot a footprint to be seen\nA kingdom of isolation\nAnd it looks like I'm the queen"
iex(42)> compressed = :zlib.compress(song)<<120, 156, 29, 204, 49, 14, 195, 48, 12, 67, 209, 221, 167, 224, 214, 115, 116,
...(42)>   236, 210, 169, 23, 112, 16, 197, 22, 108, 139, 77, 172, 192, 215, 175, 208,
...(42)>   145, 15, 248, 76, 159, 42, 152, 198, 133, 210, 185, 38, 86, 85, 23, 208, 224,
...(42)>   ...>>
** (SyntaxError) iex:42:34: syntax error before: '<<'

iex(42)> byte_size(song)
127
iex(43)> byte_size(compressed)
** (CompileError) iex:43: undefined function compressed/0
    (stdlib 3.17) lists.erl:1358: :lists.mapfoldl/3
iex(43)> :zlib.uncompress(compressed)
** (CompileError) iex:43: undefined function compressed/0
    (stdlib 3.17) lists.erl:1358: :lists.mapfoldl/3
iex(43)> 

まとめ

何かの役に立てばと。

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