Skip to content

Commit 2a08277

Browse files
committed
Fix v helper to work with new :iex_history format
1 parent df94927 commit 2a08277

File tree

1 file changed

+33
-3
lines changed

1 file changed

+33
-3
lines changed

lib/iex/lib/iex/helpers.ex

Lines changed: 33 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -225,12 +225,42 @@ defmodule IEx.Helpers do
225225
"""
226226
def v(n) when n < 0 do
227227
history = Process.get(:iex_history)
228-
Enum.fetch!(history, abs(n) - 1).result
228+
queue_nth_r(history, abs(n) - 1, :queue.len(history)).result
229229
end
230230

231231
def v(n) when n > 0 do
232-
history = Process.get(:iex_history) |> Enum.reverse
233-
Enum.fetch!(history, n - 1).result
232+
history = Process.get(:iex_history)
233+
queue_nth(history, n - 1, :queue.len(history)).result
234+
end
235+
236+
defp queue_nth(_, _, 0) do
237+
raise_bounds
238+
end
239+
240+
defp queue_nth(queue, 0, _) do
241+
{ :value, value } = :queue.peek(queue)
242+
value
243+
end
244+
245+
defp queue_nth(queue, n, len) when n > 0 do
246+
queue_nth(:queue.drop(queue), n-1, len-1)
247+
end
248+
249+
defp queue_nth_r(_, _, 0) do
250+
raise_bounds
251+
end
252+
253+
defp queue_nth_r(queue, 0, _) do
254+
{ :value, value } = :queue.peek_r(queue)
255+
value
256+
end
257+
258+
defp queue_nth_r(queue, n, len) when n > 0 do
259+
queue_nth(:queue.drop_r(queue), n-1, len-1)
260+
end
261+
262+
defp raise_bounds do
263+
raise "Out of bounds"
234264
end
235265

236266
@doc """

0 commit comments

Comments
 (0)