@@ -225,12 +225,42 @@ defmodule IEx.Helpers do
225
225
"""
226
226
def v ( n ) when n < 0 do
227
227
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
229
229
end
230
230
231
231
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"
234
264
end
235
265
236
266
@ doc """
0 commit comments