Exploring Elixir source code with IEx
2021-11-30Elixir's IEx.Helpers.open/1
allows you to find source code directly from IEx.
The open macro will open the given module or module.function/arity in your editor.
open
uses ELIXIR_EDITOR
by default,
But will fall back to EDITOR
if the former isn't available.
open is great for exploring Elixir source code.
For example, how is Enum.map/2
implemented for lists?
iex()> open Enum.map
And in my editor:
def map(enumerable, fun) when is_list(enumerable) do
:lists.map(fun, enumerable)
end
Okay, it makes sense for Elixir to reuse Erlang's implementation of map.
How does Erlang do it?:
iex()> open :lists.map
And in my editor:
map(F, [H|T]) ->
[F(H)|map(F, T)];
map(F, []) when is_function(F, 1) -> [].
So Erlang, and therefore Elixir,
uses the [head | tail] syntax to map functions over lists.
If you start your IEx session with Mix:
$ iex -S mix
open can find your project's source code,
And the source code of any downloaded dependencies.
I use the open macro to explore code that I don't understand.
Next time you want to find what a line of Elixir code does,
Try IEx.Helpers.open/1
Do other languages have similar tools for code exploration?
(Thanks to Adi Iyengar for introducing me to this tool.)
Adi Inyengar - Elixir: Open Files with Vim using IEx.Helpers.open/1