paiza.ioでelixir その279

# 概要
paiza.ioでelixirやってみた。
etsやってみた。

# サンプルコード

```

set_table = :ets.new(:set_table, [:set])

:ets.insert(set_table, {:foo, 1})
:ets.insert(set_table, {:foo, 2})
:ets.lookup(set_table, :foo)
|> IO.inspect

bag_table = :ets.new(:bag_table, [:bag])

:ets.insert(bag_table, {:foo, 1})
:ets.insert(bag_table, {:foo, 2})
:ets.lookup(bag_table, :foo)
|> IO.inspect

set_table = :ets.new(:set_table, [:set])
:ets.insert(set_table, {:foo, 1})
:ets.insert(set_table, {:bar, 2})
:ets.insert(set_table, {:baz, 3})
:ets.insert(set_table, {:hoge, 4})
:ets.insert(set_table, {:fuga, 5})
k = :ets.first(set_table)  
|> IO.inspect
k = :ets.next(set_table, k)
|> IO.inspect
k = :ets.next(set_table, k)
|> IO.inspect
k = :ets.next(set_table, k) 
|> IO.inspect
k = :ets.next(set_table, k)
|> IO.inspect
k = :ets.next(set_table, k)
|> IO.inspect




ordered_set_table = :ets.new(:ordered_set_table, [:ordered_set])
:ets.insert(ordered_set_table, {:foo, 1})
:ets.insert(ordered_set_table, {:bar, 2})
:ets.insert(ordered_set_table, {:baz, 3})
:ets.insert(ordered_set_table, {:hoge, 4})
:ets.insert(ordered_set_table, {:fuga, 5})
k = :ets.first(ordered_set_table)
|> IO.inspect
k = :ets.next(ordered_set_table, k)
|> IO.inspect
k = :ets.next(ordered_set_table, k)
|> IO.inspect
k = :ets.next(ordered_set_table, k)
|> IO.inspect
k = :ets.next(ordered_set_table, k)
|> IO.inspect
k = :ets.next(ordered_set_table, k)
|> IO.inspect



bag_table = :ets.new(:bag_table, [:bag])
:ets.insert(bag_table, {:foo, 1})
:ets.insert(bag_table, {:foo, 1})
:ets.insert(bag_table, {:foo, 2})
:ets.lookup(bag_table, :foo)
|> IO.inspect


duplicate_bag_table = :ets.new(:duplicate_bag_table, [:duplicate_bag])
:ets.insert(duplicate_bag_table, {:foo, 1})
:ets.insert(duplicate_bag_table, {:foo, 1})
:ets.insert(duplicate_bag_table, {:foo, 2})
:ets.lookup(duplicate_bag_table, :foo)
|> IO.inspect

#table = :ets.new(:user_lookup, [:set, :protected])

:ets.new(:user_lookup, [:set, :protected, :named_table])
:ets.insert(:user_lookup, {"doomspork", "Sean", ["Elixir", "Ruby", "Java"]})
:ets.insert_new(:user_lookup, {"doomspork", "Sean", ["Elixir", "Ruby", "Java"]})
:ets.insert_new(:user_lookup, {"3100", "", ["Elixir", "Ruby", "JavaScript"]})
:ets.lookup(:user_lookup, "doomspork")
|> IO.inspect

:ets.match(:user_lookup, {:"$1", "Sean", :_})
|> IO.inspect

:ets.match(:user_lookup, {:"$99", :"$1", :"$3"})
|> IO.inspect

:ets.match_object(:user_lookup, {:"$1", :_, :"$3"})
|> IO.inspect

:ets.match_object(:user_lookup, {:_, "Sean", :_})
|> IO.inspect

:ets.match_object(:user_lookup, {:"$1", :_, :"$3"})
|> IO.inspect

:ets.select(:user_lookup, [{{:"$1", :_, :"$3"}, [], [:"$_"]}])
|> IO.inspect

#fun = :ets.fun2ms(fn {username, _, langs} when length(langs) > 2 -> username end)
#:ets.select(:user_lookup, fun)


:ets.delete(:user_lookup, "doomspork")


defmodule MyEts do
  def init, do: :ets.new(__MODULE__, [:set, :named_table])
  def get(key, default \\ nil) do
    case :ets.lookup(__MODULE__, key) do
      [] -> 
        default
      [{_, value} | _] -> 
        value
    end
  end
  def put(key, value), do: :ets.insert(__MODULE__, [{key, value}])
  def delete(key), do: :ets.delete(__MODULE__, key)
end


MyEts.init()

MyEts.put(:feeling, {"awesome", "0", "1"})

MyEts.get(:feeling)
|> IO.inspect

MyEts.delete(:feeling)



```

# 実行結果

```
[foo: 2]
[foo: 1, foo: 2]
:bar
:baz
:fuga
:hoge
:foo
:"$end_of_table"
:bar
:baz
:foo
:fuga
:hoge
:"$end_of_table"
[foo: 1, foo: 2]
[foo: 1, foo: 1, foo: 2]
[{"doomspork", "Sean", ["Elixir", "Ruby", "Java"]}]
[["doomspork"]]
[
  ["Sean", ["Elixir", "Ruby", "Java"], "doomspork"],
  ["", ["Elixir", "Ruby", "JavaScript"], "3100"]
]
[
  {"doomspork", "Sean", ["Elixir", "Ruby", "Java"]},
  {"3100", "", ["Elixir", "Ruby", "JavaScript"]}
]
[{"doomspork", "Sean", ["Elixir", "Ruby", "Java"]}]
[
  {"doomspork", "Sean", ["Elixir", "Ruby", "Java"]},
  {"3100", "", ["Elixir", "Ruby", "JavaScript"]}
]
[
  {"doomspork", "Sean", ["Elixir", "Ruby", "Java"]},
  {"3100", "", ["Elixir", "Ruby", "JavaScript"]}
]
{"awesome", "0", "1"}


```


# 成果物

https://paiza.io/projects/_roXSmBqLUYIYJOEIBZZVg


以上。