I’m getting the following error, for the code below it (Chain example from Programming Elixit 1.3):
I’ve double-checked the code and can’t find a discrepancy from the book. Does my newer version of Elixir or Erlang break it? (Elixir 1.5, Erlang 20)
iex(1)> Chain.run(10)
** (Protocol.UndefinedError) protocol String.Chars not implemented for {“10”}
(elixir) lib/string/chars.ex:3: String.Chars.impl_for!/1
(elixir) lib/string/chars.ex:22: String.Chars.to_string/1
chain.exs:20: Chain.create_processes/1
(stdlib) timer.erl:197: :timer.tc/3
chain.exs:25: Chain.run/1
defmodule Chain do
def counter(next_pid) do
receive do
n ->
send next_pid, n+1
end
end
def create_processes(n) do
last = Enum.reduce 1..n, self(),
fn (_, send_to) ->
spawn(Chain, :counter, [send_to])
end
send last, 0 # Start the count by sending zero to the last process.
receive do
final_answer when is_integer(final_answer) ->
"Result is #{{inspect(final_answer)}}"
end
end
def run(n) do
IO.puts inspect :timer.tc(Chain, :create_processes, [ n ])
end
end