h 是 Elixir shell 中最有用的工具了. 多亏了这门语言对文档的支持一级棒, 我们可以用这个辅助函数查看任意代码的文档. 我们看一下使用的例子:
iex> h EnumEnumProvides a set of algorithms that enumerate over enumerables according to theEnumerable protocol.┃ iex>Enum.map([1,2,3],fn(x) -> x *2end)┃ [2,4,6]Some particular types, like maps, yield a specific format on enumeration.Forexample, the argument is always a {key, value} tuple for maps:┃ iex> map = %{a: 1, b: 2}┃ iex>Enum.map(map,fn {k, v} -> {k, v *2} end)┃ [a: 2, b: 4]Note that the functions in the Enum module are eager: they always start theenumeration of the given enumerable.TheStream module allows lazy enumerationof enumerables and provides infinite streams.Since the majority of the functions inEnum enumerate the whole enumerable andreturn a list as result, infinite streams need to be carefully used with suchfunctions, as they can potentially run forever.For example:┃ Enum.eachStream.cycle([1,2,3]), &IO.puts(&1)
甚至可以在 shell 中与补全功能组合在一起使用. 假想一下这是我们第一次使用 Map:
iex> h MapMapA set of functions for working with maps.Maps are key-value stores where keys can be any value and are compared usingthe match operator (===).Maps can be created with the %{} special form definedin the Kernel.SpecialForms module.iex>Map.delete/2 drop/2 equal?/2fetch!/2 fetch/2 from_struct/1get/2 get/3 get_and_update!/3get_and_update/3 get_lazy/3 has_key?/2keys/1 merge/2 merge/3new/0 new/1 new/2pop/2 pop/3 pop_lazy/3put/3 put_new/3 put_new_lazy/3split/2 take/2 to_list/1update!/3 update/4 values/1iex> h Map.merge/2defmerge(map1, map2)Merges two maps into one.All keys in map2 will be added to map1, overriding any existing one.If you have a struct and you would like to merge a set of keys into the struct,donotuse this function, as it would merge all keys on the right side into thestruct, even if the key is not part of the struct.Instead,useKernel.struct/2.Examples┃ iex>Map.merge(%{a: 1, b: 2}, %{a: 3, d: 4})┃ %{a: 3, b: 2, d: 4}
如我们所见, 不仅可以看到模块中有什么函数还能看到每个单独函数的文档, 而且它们大多都有使用示例.
i
让我们用刚才学到的 h 来 了解一下 i 辅助函数:
iex> h idefi(term)Prints information about the given data type.iex> i MapTermMapData typeAtomModule bytecode/usr/local/Cellar/elixir/1.3.3/bin/../lib/elixir/ebin/Elixir.Map.beamSource/private/tmp/elixir-20160918-33925-1ki46ng/elixir-1.3.3/lib/elixir/lib/map.exVersion [9651177287794427227743899018880159024]Compile time no value foundCompile options [:debug_info]DescriptionUseh(Map) to access its documentation.CallMap.module_info() to access metadata.Raw representation :"Elixir.Map"Reference modulesModule,Atom
如果想要重新编译一个具体的模块我们可以使用 r 辅助函数. 比如改变了一点代码想要运行新加的函数, 可以保存变更后使用 r 来重新编译:
iex> r MyProjectwarning: redefining module MyProject (current version loaded from _build/dev/lib/my_project/ebin/Elixir.MyProject.beam) lib/my_project.ex:1{:reloaded,MyProject, [MyProject]}
t
t 辅助函数告诉我们在模块中都有那些定义的类型:
iex> t Map@type key() ::any()@type value() ::any()
现在我们知道了在 Map 的实现中定义了 key 和 value 类型. 对应的在 Map 的源文件中:
defmoduleMapdo# ... @type key :: any @type value :: any# ...
这个简单的例子, 说明在每个实现中 key 和 value 可以是任意类型的, 知道这些是有帮助的.