LoginSignup
8
7

More than 5 years have passed since last update.

Elixir の iex helper が捗る件

Last updated at Posted at 2013-12-10

起動と終了

iex は interactive shell です。ruby でいうところの irb、Haskell でいうところの ghci です。
$ iex で起動して、Ctrl+C 二回押しで終了します。

iex helper

iex では helper 関数があって、h とタイプすることで以下のようにリストが見れます。ちなみに実際はもっとカラフルに表示されます。
リスト中の c/2 とか cd/1 の /2 とかは /1 は引数の数を表しています(これ調べるの難しかったです、なんと検索すればいいやら)。

iex(1)> h

                                  IEx.Helpers                                       

Welcome to Interactive Elixir. You are currently seeing the documentation for 
the module IEx.Helpers which provides many helpers to make Elixir's shell more
joyful to work with.

This message was triggered by invoking the helper h(), usually referred to as
h/0 (since it expects 0 arguments).

There are many other helpers available:

• c/2       — compiles a file at the given path
• cd/1      — changes the current directory
• clear/0   — clears the screen
• flush/0   — flushes all messages sent to the shell
• h/0       — prints this help message
• h/1       — prints help for the given module, function or macro
• l/1       — loads the given module's beam code and purges the current version
• ls/0      — lists the contents of the current directory
• ls/1      — lists the contents of the specified directory
• m/0       — prints loaded modules
• pwd/0     — prints the current working directory
• r/1       — recompiles and reloads the given module's source file
• respawn/0 — respawns the current shell
• s/1       — prints spec information
• t/1       — prints type information
• v/0       — prints the history of commands evaluated in the session
• v/1       — retrieves the nth value from the history
• import_file/1             — evaluates the given file in the shell's context

Help for functions in this module can be consulted directly from the command
line, as an example, try:

┃ h(c/2)

You can also retrieve the documentation for any module or function. Try these:

┃ h(Enum)
┃ h(Enum.reverse/1)

To learn more about IEx as a whole, just type h(IEx).

iex(2)>

h コマンドでライブラリを調べて捗る

h コマンドには module, function, macro を渡すことができるので、使っている module の動作を調べるときに役立ちます。
例えば Stream について何も知らなかったので調べてみると。

iex(4)> h Stream

                                     Stream                                     

Module for creating and composing streams.

Streams are composable, lazy enumerables. Any enumerable that generates items
one by one during enumeration is called a stream. For example, Elixir's Range
is a stream:

┃ iex> range = 1..5
┃ 1..5
┃ iex> Enum.map range, &(&1 * 2)
┃ [2,4,6,8,10]

... 以下省略

ふむふむとなるわけですね。説明を読むと map 関数も使えるそうなので、さらに調べてみると。

iex(5)> h Stream.map

                             def map(enumerable, f)                             

Creates a stream that will apply the given function on enumeration.

Examples

┃ iex> stream = Stream.map([1, 2, 3], fn(x) -> x * 2 end)
┃ iex> Enum.to_list(stream)
┃ [2,4,6]

とサンプルコードまで出てきました。もちろん module, function などのドキュメントが書かれていなければ表示されないのですが、そういったものが充実していれば下調べするときに捗ります。
しかもこれは doctest を通っているので、動くことが保証されているコードです。Stream.map の実装に、h Stream.map で表示されたものが書かれていますね。

cd, ls, pwd コマンドで捗る

cd, ls, pwd はその名の通りディレクトリ操作関連コマンドです。iex 抜けて、cd で移動して、また iex 起動みたいな作業をしなくていいので捗りますね。

まとめ

簡単にですが、iex の helper についてちょろっと調べてみました。まだまだ調べていないコマンドもありますが、その他にも便利そうなコマンドがありますね。mとかsとかtとか。
@mururuさんの記事に他にも便利な機能が紹介されています。-> iexの便利機能

SEE ALSO

Programming Elixir

このエントリは Elixir Advent Calendar 2013 の9日目(投稿日は気にしない)でした。明日は。。。きっと誰かがやってくれる。

8
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
8
7