We can't find the internet
Attempting to reconnect
Something went wrong!
Hang in there while we get back on track
The amazing v/1 helper function
jrowah
I may be late on this one but today I learned about the v/1, a helper function that retrieves the value from a previous expression by its line number during an iex session.
Here’s how it works:
iex(1)> 2 + 2
4
iex(2)> "hello"
"hello"
iex(3)> v(1)
4
iex(4)> v(2)
"hello"
You can also use v/0 (without an argument) to get the value of the most recent expression:
iex(1)> 3 * 7
21
iex(2)> v()
21
iex(3)> v() + 10
31
And there’s a negative index variant - v(-1) gets the previous result, v(-2) gets the one before that, etc:
iex(1)> 100
100
iex(2)> 200
200
iex(3)> v(-1)
200
iex(4)> v(-2)
100
This is super useful when you’re experimenting in the shell and want to reuse a result without retyping the expression or assigning everything to variables. It’s especially handy when you’ve just computed something complex and want to pipe it into another function or inspect it further.
copied to clipboard