From 954a8899928d3e5f3b4f297b63efd45de544bf40 Mon Sep 17 00:00:00 2001 From: jbranchaud Date: Sat, 18 Mar 2017 17:11:37 -0500 Subject: [PATCH] Add Referencing Values In IEx's History as an elixir til --- README.md | 3 +- elixir/referencing-values-in-iexs-history.md | 44 ++++++++++++++++++++ 2 files changed, 46 insertions(+), 1 deletion(-) create mode 100644 elixir/referencing-values-in-iexs-history.md diff --git a/README.md b/README.md index 1165d93..4995744 100644 --- a/README.md +++ b/README.md @@ -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) diff --git a/elixir/referencing-values-in-iexs-history.md b/elixir/referencing-values-in-iexs-history.md new file mode 100644 index 0000000..06c1db4 --- /dev/null +++ b/elixir/referencing-values-in-iexs-history.md @@ -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.