- Mastering Elixir
- André Albuquerque Daniel Caixinha
- 602字
- 2021-08-05 10:42:51
IEx
In the beginning of this chapter, we provided some instructions on how to start and stop an Elixir shell. You've seen it in use throughout this chapter. We'll now show you some other interesting things you can do inside IEx.
In this chapter, we've been giving you links to the official documentation of several Elixir modules. You can access this documentation right from IEx, using the h command. Let's say you want to know more about the Enum module:
iex> h Enum
Enum
#...,
The preceding code provides a set of algorithms that enumerate over enumerables according to the Enumerable protocol.
Note that, for brevity, the output of this command was cropped.
You can even pass a fully qualified function name, and get information about it. Let's say, for example, you don't remember the order of the arguments for the Enum.map/2 function:
iex> h Enum.map/2
def map(enumerable, fun)
@spec map(t(), (element() -> any())) :: list()
Returns a list where each item is the result of invoking fun on each
corresponding item of enumerable.
For maps, the function expects a key-value tuple.
## Examples
iex> Enum.map([1, 2, 3], fn(x) -> x * 2 end)
[2, 4, 6]
iex> Enum.map([a: 1, b: 2], fn({k, v}) -> {k, -v} end)
[a: -1, b: -2]
Another very interesting feature, which ships from Elixir 1.5 onwards, is the ability to set breakpoints from IEx. To showcase this, let's create a breakpoint on the StringHelper.palindrome?/1 function we've defined in the Functions and modules section, present in the "string_helper.ex" file:
iex> break! StringHelper.palindrome?/1
1
iex> StringHelper.palindrome?("abba")
Break reached: StringHelper.palindrome?/1 (string_helper.ex:2)
1: defmodule StringHelper do
2: def palindrome?(term) when is_bitstring(term) do
3: String.reverse(term) == term
4: end
pry(1)> term
"abba"
pry(2)> whereami
Location: string_helper.ex:2
1: defmodule StringHelper do
2: def palindrome?(term) when is_bitstring(term) do
3: String.reverse(term) == term
4: end
As you can see, you could access the argument passed to the function, term, and also use whereami to show the location where the breakpoint stopped the execution. To resume the execution, you use the continue command to go to the next breakpoint, or respawn to exit and start a new shell.
From Elixir 1.6 onward, you can also use pattern matching and guard clauses when setting a breakpoint. The breakpoint we defined earlier could also be set as break! StringHelper.palindrome?("abba"), which would make our breakpoint work when that function is called with "abba" as the argument.
To finish this section, we'll show you a handy feature of IEx: the ability to access values from the history. Sometimes, you call a certain function and realize afterward that you wanted to bind the result of that function call to a variable. If this happens, you can use the v/1 function. You pass it a number, which represents the position of the expression from which you want to retrieve the value (starting at 1). You can also pass a negative number, which makes the position relative to the current expression in the shell. You can call this function without providing an argument, which is the same as calling v(-1)–which means we're getting the value from the last expression. Let's see an example:
iex> 7 * 3
21
iex> a = v
21
iex> a * 2
42
iex> v(-3)
21
There are more things you can do with IEx, such as configuring it or connecting to remote shells. Please refer to the official documentation (at https://hexdocs.pm/iex/IEx.html) for further usage examples of IEx.
- 黑客攻防從入門到精通(實戰秘笈版)
- JavaScript百煉成仙
- 精通搜索分析
- 營銷數據科學:用R和Python進行預測分析的建模技術
- NativeScript for Angular Mobile Development
- 零基礎學Java(第4版)
- Windows Server 2016 Automation with PowerShell Cookbook(Second Edition)
- MongoDB權威指南(第3版)
- ANSYS Fluent 二次開發指南
- Python算法指南:程序員經典算法分析與實現
- Mastering ROS for Robotics Programming
- Windows Phone 7.5:Building Location-aware Applications
- 深入淺出React和Redux
- D3.js By Example
- 從零開始學C#