paiza.ioでelixir その63

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

# サンプルコード

```
send(self(), :test_message)
send(self(), :test_message1)
send(self(), :test_message2)
send(self(), :test_message3)
:erlang.process_info(self(), :message_queue_len)
|> IO.inspect
receive do msg -> IO.puts("Received message: #{inspect(msg)}") end
receive do msg -> IO.puts("Received message: #{inspect(msg)}") end
receive do msg -> IO.puts("Received message: #{inspect(msg)}") end
receive do msg -> IO.puts("Received message: #{inspect(msg)}") end
#receive do msg -> IO.puts("Received message: #{inspect(msg)}") end

defmodule Counter do
	use Agent
	def start_link(initial_value) do
		Agent.start_link(fn -> 
			initial_value 
		end, name: __MODULE__)
	end
	def value do
		Agent.get(__MODULE__, & &1)
	end
	def increment do
		Agent.update(__MODULE__, &(&1 + 1))
	end
end

Counter.start_link(0)
|> IO.inspect
Counter.value()
|> IO.inspect
Counter.increment()
|> IO.inspect
Counter.increment()
|> IO.inspect
Counter.value()
|> IO.inspect

{:ok, pid} = Agent.start_link(fn -> 
	42 
end)
|> IO.inspect
Agent.cast(pid, fn state -> 
	state + 1 
end)
|> IO.inspect
Agent.get(pid, fn state -> 
	state 
end)
|> IO.inspect

{:ok, pid} = Agent.start_link(fn -> 
	42 
end)
|> IO.inspect
Agent.cast(pid, Kernel, :+, [12])
|> IO.inspect
Agent.get(pid, fn state -> 
    state 
end)
|> IO.inspect

{:ok, pid} = Agent.start_link(fn -> 
	42 
end)
|> IO.inspect
Agent.get(pid, fn state -> 
	state 
end)
|> IO.inspect

{:ok, pid} = Agent.start_link(fn -> 
	42 
end)
|> IO.inspect
Agent.get_and_update(pid, fn state -> 
	{state, state + 1} 
end)
|> IO.inspect
Agent.get(pid, fn state -> 
	state 
end)
|> IO.inspect

{:ok, pid} = Agent.start(fn -> 
	42 
end)
|> IO.inspect
Agent.get(pid, fn state -> 
	state 
end)
|> IO.inspect

{:ok, pid} = Agent.start_link(fn -> 
	42 
end)
|> IO.inspect
Agent.get(pid, fn state -> 
	state 
end)
|> IO.inspect
{:error, {exception, _stacktrace}} = Agent.start(fn -> 
	raise "oops" 
end)
|> IO.inspect

{:ok, pid} = Agent.start_link(fn -> 
	42 
end)
|> IO.inspect
Agent.stop(pid)
|> IO.inspect

{:ok, pid} = Agent.start_link(fn -> 
	42 
end)
|> IO.inspect
Agent.update(pid, fn state -> 
	state + 1 
end)
|> IO.inspect
Agent.get(pid, fn state -> 
	state 
end)
|> IO.inspect

{:ok, pid} = Agent.start_link(fn -> 
	42 
end)
|> IO.inspect
Agent.update(pid, Kernel, :+, [12])
|> IO.inspect
Agent.get(pid, fn state -> 
	state 
end)
|> IO.inspect



```

# 実行結果

```
{:message_queue_len, 4}
Received message: :test_message
Received message: :test_message1
Received message: :test_message2
Received message: :test_message3
{:ok, #PID<0.97.0>}
0
:ok
:ok
2
{:ok, #PID<0.98.0>}
:ok
43
{:ok, #PID<0.99.0>}
:ok
54
{:ok, #PID<0.100.0>}
42
{:ok, #PID<0.101.0>}
42
43
{:ok, #PID<0.102.0>}
42
{:ok, #PID<0.103.0>}
42
{:error,
 {%RuntimeError{message: "oops"},
  [
    {:elixir_compiler_0, :"-__FILE__/1-fun-14-", 0,
     [file: 'Main.exs', line: 103]},
    {Agent.Server, :init, 1, [file: 'lib/agent/server.ex', line: 8]},
    {:gen_server, :init_it, 2, [file: 'gen_server.erl', line: 374]},
    {:gen_server, :init_it, 6, [file: 'gen_server.erl', line: 342]},
    {:proc_lib, :init_p_do_apply, 3, [file: 'proc_lib.erl', line: 249]}
  ]}}
{:ok, #PID<0.105.0>}
:ok
{:ok, #PID<0.107.0>}
:ok
43
{:ok, #PID<0.108.0>}
:ok
54
×
```

# 成果物


https://paiza.io/projects/20mClardXKeGMnyjqbI2_g?language=elixir


以上。