1
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 episode:4 モジュールの文字列化

1
Last updated at Posted at 2019-05-15

環境

sh
$ lsb_release -d
No LSB modules are available.
Description:    Ubuntu 24.04.3 LTS
$ elixir -v
Erlang/OTP 27 [erts-15.2.7.4] [source] [64-bit] [smp:16:16] [ds:16:16:10] [async-threads:1] [jit:ns]

Elixir 1.17.3 (compiled with Erlang/OTP 26)

はじめに

Elixir episode:3 Enumerable までの fg プロジェクトを使う。

Inspect プロトコル

Fg.List の中身を簡単に確認したいなぁ。
毎回 Enum.to_list/1 は嫌だなぁ。

iex
iex(1)> list = Fg.List.new(0..99) 
%Fg.List{top: %Fg.Cell{fun: #Function<2.85842498/1 in Fg.Cell.new/2>}}
iex(2)> nested = Fg.List.cons(list, list)
%Fg.List{top: %Fg.Cell{fun: #Function<2.85842498/1 in Fg.Cell.new/2>}}
iex(3)> inspect(nested)
"%Fg.List{top: %Fg.Cell{fun: #Function<2.85842498/1 in Fg.Cell.new/2>}}"
iex(4)> Enum.to_list(nested)
[
  %Fg.List{top: %Fg.Cell{fun: #Function<2.85842498/1 in Fg.Cell.new/2>}},
  0,
  1,
  2,
  # 略
  ...
]

そんなときに Inspect.inspect/2 を実装する。

fg/lib/fg/list.ex
# 追加
defimpl Inspect, for: Fg.List do
  alias Fg.List, as: Me

  @left "#Fg.List<"
  @right ">"

  def inspect(%Me{} = me, _opts) do
    inspected = me |> Enum.to_list() |> Kernel.inspect()
    @left <> inspected <> @right
  end
end
iex
iex(1)> list = Fg.List.new(0..99)
#Fg.List<[0, 1, 2, #略 ...]>  # デフォで要素が50ヶ列挙される
iex(2)> Fg.List.cons(list, list)
#Fg.List<[#Fg.List<[0, 1, 2, #略 ...]>

なんかダラつくね。

以下の記事に丁寧な解説がありました、感謝。

inspect について調べてみた

Inspect.Algebra.surround_many/6 は非推奨で消えたらしいので、
Inspect.Algebra.container_doc/6 で読み替えましょう。

fg/lib/fg/list.ex
# 変更
defimpl Inspect, for: Fg.List do
  import Inspect.Algebra
  alias Fg.List, as: Me

  @left "#Fg.List<["
  @right "]>"

  def inspect(%Me{} = me, opts) do
    native = Enum.to_list(me)
    container_doc(@left, native, @right, opts, &Inspect.inspect/2)
  end
end
iex
iex(1)> list = Fg.List.new(0..99)
#Fg.List<[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19,
 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39,
 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, ...]>
iex(2)> nested = Fg.List.cons(list, list)
#Fg.List<[
  #Fg.List<[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18,
   19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37,
   38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, ...]>,
  0,
  1,
  2,
  # 略
  ...
]>
iex(3)> inspect(nested, limit: 3)
"#Fg.List<[#Fg.List<[0, 1, ...]>, 0, 1, ...]>"

なるほどぉ。オプションも効くようになったぞ、いいね。

String.Chars プロトコル

String.Chars.to_string/1 を実装し文字列/バイナリを返す。
リストの文字列化って何だ?

iex
iex(1)> abc = [97, 98, 99]
~c"abc"
iex(2)> nested = [abc | abc]
[~c"abc", 97, 98, 99]
iex(3)> to_string nested
"abcabc"

へぇ、平坦化しちゃうんだ。
じゃー flatten/1 がいるねぇ。

flatten 関数が理解できなかった その1

fg/lib/fg/list.ex
defmodule Fg.List do
  alias __MODULE__, as: Me
  alias Fg.Cell
  ...
  # 追加
  def flatten(%Me{} = me), do: _flatten(me, new())

  defp _flatten(%Me{top: %Cell{fun: nil}}, acc), do: acc
  defp _flatten(%Me{} = me, acc) do
    _flatten(head(me), _flatten(tail(me), acc))
  end
  defp _flatten(x, acc), do: cons(x, acc)
end
...
# 追加
defimpl String.Chars, for: Fg.List do
  alias Fg.List, as: Me

  def to_string(%Me{} = me) do
    me
    |> Me.flatten()
    |> Enum.to_list()
    |> Kernel.to_string()
  end
end
iex
iex(1)> abc = Fg.List.new [97, 98, 99]
#Fg.List<[97, 98, 99]>
iex(2)> nested = Fg.List.cons(abc, abc)
#Fg.List<[#Fg.List<[97, 98, 99]>, 97, 98, 99]>
iex(3)> to_string(nested)
"abcabc"

List.Chars プロトコル

List.Chars.to_charlist/1 で文字コード・リストを返す。
Erlang の文字列ね。

iex
iex(1)> abc = [97, 98, 99]
~c"abc"
iex(2)> nested = [abc | abc]
[~c"abc", 97, 98, 99]
iex(3)> to_charlist(nested)
[~c"abc", 97, 98, 99]
iex(4)> to_charlist [97,98,99,:d]
[97, 98, 99, :d]
iex(5)> to_charlist "あいうえお"
[12354, 12356, 12358, 12360, 12362]

リストの場合はなんもしないから、そのまま返せということ?

fg/lib/fg/list.ex
...
# 追加
defimpl List.Chars, for: Fg.List do
  alias Fg.List, as: Me
  alias Fg.Cell

  def to_charlist(%Me{} = me), do: _to_charlist(me, [])

  defp _to_charlist(%Me{top: %Cell{fun: nil}}, acc) do
    Enum.reverse(acc)
  end
  defp _to_charlist(me, acc) do
    {h, t} = {Me.head(me), Me.tail(me)}
    case h do
      %Me{} -> _to_charlist(t, [_to_charlist(h, []) | acc])
      _     -> _to_charlist(t, [h | acc])
    end
  end
end
iex
iex(1)> abc = Fg.List.new [97, 98, 99]
#Fg.List<[97, 98, 99]>
iex(2)> nested = Fg.List.cons(abc, abc)
#Fg.List<[#Fg.List<[97, 98, 99]>, 97, 98, 99]>
iex(3)> to_charlist nested
[~c"abc", 97, 98, 99]

ビヘイビアは継承だけど、プロトコルは疎結合なのがいいね。ノシ

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