4
votes

I'm working through Dave Thomas' Programming Elixir and I'm attempting some examples from the list chapter.

When I'm learning a language I prefer to stay as basic as I can by running <language executable> <script file>. In this case I'm running elixir reduce.exs

contents of reduce.exs:

require IEx;

defmodule MyList do
  def reduce([], memo, _), do: memo
  def reduce([head | tail], memo, func) do
    IEx.pry
    reduce(tail, func.(head, memo), func)
  end
end

ExUnit.start()
defmodule MyListTest do
  use ExUnit.Case

  def test do
    assert 10 == MyList.reduce([1,2,3,4], 0, &(&1 + &2))
  end
end

IO.puts(MyListTest.test())

When run the console outputs:

Cannot pry #PID<0.70.0> at reduce.exs:9. Is an IEx shell running?

I assume I am completely misunderstanding some core concepts, but I'm not entirely sure what they are.

My expectation is that the program would just drop into an iex session when it hits the execution of IEx.pry. Given iex is in the elixir core libraries, I thought the require IEx would be enough to use pry.

Do I need to use IEx.pry/3? Do I need to run a separate instance of iex and somehow connect the two nodes together?

Just evaluating the code by running iex reduce.exs runs the file, but it does not show the test output.

Feel free to correct any and every silly assumption I've made.

1

1 Answers

4
votes

You are getting this error because an IEx needs to be running. Using the command iex reduce.exs will allow you to enter into the code and where IEx.pry is placed in your source file.

To continue execution from there type respawn in the shell. It will ask you if you want to allow the pry for every recursion, but eventually will print out the result of the test.

The reason you don't see the output of the test before hand, is that IEx.pry stalls execution, thereby your test function hasn't returned and the IO.puts call hasn't completed.