为什么调用 Elixir Kernel.apply/2 而不是调用匿名函数?
Why invoke Elixir Kernel.apply/2 instead of calling the anonymous function?
既然可以调用匿名函数,为什么还要用apply/2
?
不一样吗?
iex(40)> f = fn x -> x + 1 end
#Function<7.126501267/1 in :erl_eval.expr/5>
iex(41)> apply(f, [1])
2
iex(42)> f.(1)
2
虽然 Kernel.apply/3
would be a dynamic invocation when {m, f, a}
are not static, Kernel.apply/2
[最有可能] 的典型用例是为了简化将函数传递给其应用程序的过程。
(&Integer.digits/1) # or any other anonymous function
|> apply([42]) # easy piping to application
|> Enum.join() # more piping
#⇒ "42"
既然可以调用匿名函数,为什么还要用apply/2
?
不一样吗?
iex(40)> f = fn x -> x + 1 end
#Function<7.126501267/1 in :erl_eval.expr/5>
iex(41)> apply(f, [1])
2
iex(42)> f.(1)
2
虽然 Kernel.apply/3
would be a dynamic invocation when {m, f, a}
are not static, Kernel.apply/2
[最有可能] 的典型用例是为了简化将函数传递给其应用程序的过程。
(&Integer.digits/1) # or any other anonymous function
|> apply([42]) # easy piping to application
|> Enum.join() # more piping
#⇒ "42"