作为参数的功能 - 这里发生了什么?

问题描述 投票:0回答:1

我正在浏览一些Elixir koans,我对这些代码行中发生的事情感到有点困惑。

  def times_five_and_then(number, fun), do: fun.(number * 5)
  def square(number), do: number * number

  koan "You can pass functions around as arguments. Place an '&' before the name and state the arity" do
    assert times_five_and_then(2, &square/1) == ___
  end

对于___,正确的答案是100.但我不知道如何或为什么,任何人都可以清楚地说明发生了什么?

elixir
1个回答
2
投票

&function capture运营商。 &square/1的意思是“捕获名为square的函数,它具有arity 1(简单英语中的arity 1:接受单个参数)”。

times_five_and_then(2, &square/1)

这意味着“用数字times_five_and_then调用2函数并捕获功能square(具有arity 1)”。

存储在变量中的函数称为匿名函数(因为它没有名称,与使用def关键字定义的函数不同)。可以使用&捕获运算符将命名函数捕获为匿名函数,这是您的示例中发生的情况。

times_five_and_then/2称其参数为numberfunfun参数是包含匿名函数的变量。该函数可以是任何函数,但在您的示例中,它恰好是square/1函数的捕获。调用匿名函数的语法是variable_name.(args)。这就是fun.(number * 5)在这种情况下发生的事情。存储在fun中的函数正在使用参数number * 5调用。

让我们看看times_five_and_then/2的实现:

def times_five_and_then(number, fun), do: fun.(number * 5)

这需要fun参数,并使用number参数调用它包含的函数,乘以5。发生了什么:

  1. 定义了一个名为square/1的函数,它将其参数平方。
  2. square函数被捕获为匿名函数,并作为第二个参数传递给times_five_and_then/2函数。
  3. times_five_and_then/2函数将捕获的函数绑定到fun变量。
  4. times_five_and_then/2函数将其第一个参数(称为number)乘以5。
  5. times_five_and_then/2函数调用存储在fun中的匿名函数(这是square/2函数的捕获),具有上述乘法的结果。
  6. 返回在fun中执行函数的结果。

或者换句话说,2乘以5,然后传递给square/2函数的捕获,该函数将其平方,得到100

© www.soinside.com 2019 - 2024. All rights reserved.