1
0
mirror of https://github.com/jbranchaud/til synced 2026-01-03 07:08:01 +00:00

Add Referencing Values In IEx's History as an elixir til

This commit is contained in:
jbranchaud
2017-03-18 17:11:37 -05:00
parent 7b86428161
commit 954a889992
2 changed files with 46 additions and 1 deletions

View File

@@ -7,7 +7,7 @@ variety of languages and technologies. These are things that don't really
warrant a full blog post. These are mostly things I learn by pairing with
smart people at [Hashrocket](http://hashrocket.com/).
_512 TILs and counting..._
_513 TILs and counting..._
---
@@ -104,6 +104,7 @@ _512 TILs and counting..._
- [Pattern Matching In Anonymous Functions](elixir/pattern-matching-in-anonymous-functions.md)
- [Quitting IEx](elixir/quitting-iex.md)
- [Range Into List Using Comprehensions](elixir/range-into-list-using-comprehensions.md)
- [Referencing Values In IEx's History](elixir/referencing-values-in-iexs-history.md)
- [Replace Duplicates In A Keyword List](elixir/replace-duplicates-in-a-keyword-list.md)
- [Requiring Keys For Structs](elixir/requiring-keys-for-structs.md)
- [Reversing A List](elixir/reversing-a-list.md)

View File

@@ -0,0 +1,44 @@
# Referencing Values In IEx's History
Each time we execute a statement in an `iex` session, the counter is
incremented. These numbers are references to the history of the session. We
can use these references to _refer_ to previously executed values using
`v/1`. This is particularly handy for multi-line statements or when we
forget to bind to the result of some function.
Consider the following `iex` session:
```elixir
iex(1)> :one
:one
iex(2)> 1 + 1
2
iex(3)> "three" |> String.to_atom()
:three
```
If we execute `v()` on its own, it is the same as `v(-1)` in that it will
give us the latest value in the history.
```elixir
iex(4)> v()
:three
```
Providing any positive number will refer to the references we see next to
each statement.
```elixir
iex(5)> v(1)
:one
```
Negative numbers, as we saw with `v(-1)`, will count backwards in the
history from where we are.
```elixir
iex(6)> v(-4)
2
```
See `h v` for more details.