Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Elixir中有splat操作员吗?_Elixir - Fatal编程技术网

Elixir中有splat操作员吗?

Elixir中有splat操作员吗?,elixir,Elixir,Elixir中是否有splat运算符等效项,如*opts? 是否有方法将多个选项(而不是选项列表)作为参数传递给exec函数 没有splat操作符。Elixir(和Erlang)中的函数由它们的名称和arity(String.downcase/1,Enum.member?/2)定义,而变量函数则与此相反 Erlang的一位作者Joe Armstrong在他的书《编程Erlang:并发世界的软件》中提到了这一点: 1) 函数的arity是其名称的一部分, 2) 没有可变函数 如果您想使用参数列表(

Elixir中是否有splat运算符等效项,如*opts?
是否有方法将多个选项(而不是选项列表)作为参数传递给exec函数

没有splat操作符。Elixir(和Erlang)中的函数由它们的名称和arity(
String.downcase/1
Enum.member?/2
)定义,而变量函数则与此相反

Erlang的一位作者Joe Armstrong在他的书《编程Erlang:并发世界的软件》中提到了这一点:

1) 函数的arity是其名称的一部分,
2) 没有可变函数

如果您想使用参数列表(与您想要的相反)调用函数,可以使用

例如


正如Gazier所说,您不能在Elixir(或Erlang)中为函数指定变量arity。最简单的方法是传递一个列表来代替要改变数量的参数,然后使用模式匹配对其进行适当分解。根据上面的例子,它看起来是这样的:

defmodule Test do
  def add(a, b, c) do
    a + b + c 
  end
end

apply(Test, :add, [1, 2, 3])
注意:我没有测试这段代码,因为我不想安装瓷器,但它基本上应该是正确的

defmodule Test do
  def add(a, b, c) do
    a + b + c 
  end
end

apply(Test, :add, [1, 2, 3])
defmodule UnixCommands do
  alias Porcelain.Result
  def run(command,[opts]) do
    optlist = opts |> Enum.reduce(fn o-> "#{o} " end)
    %Result{out: output, status: _} = Porcelain.exec(command, optlist)
  end
end