elixir 模塊

模塊定義  defmodule函數

函數定義  defio

私有函數  defp  --至關於其餘語言 privatemodule

iex(29)> defmodule Math do
...(29)> def sum(a, b) do
...(29)> a + b
...(29)> end
...(29)> end

iex(30)> Math.sum(1, 2)
3語法

 

捕捉語法 定義函數的快捷方式word

iex(31)> fun = &(&1+1)
#Function<6.90072148/1 in :erl_eval.expr/5>
iex(32)> fun.(2)
3語言

 

等價於join

iex(33)> a = fn x -> x + 1 end
#Function<6.90072148/1 in :erl_eval.expr/5>
iex(34)> a.(2)
3參數

 

 

默認參數eval

iex(38)> defmodule Concat do
...(38)> def join(a, b, sep \\ " ") do
...(38)> a <> sep <> b
...(38)> end
...(38)> end

 

iex(39)> IO.puts Concat.join("hello", "word")hello word:okiex(40)> IO.puts Concat.join("hello", "word", " ")hello word:ok

相關文章
相關標籤/搜索