【发布时间】:2017-05-10 14:37:02
【问题描述】:
假设我有一个函数,它有点冗长并且每次都使用相同的参数调用,这个函数也需要做很多设置才能在其回调中调用该模块的任何其他函数。
SomeMod.called_a_lot(‘xx’, fn(y) ->
SomeMod.needs_called_a_lot_to_be_called_first(‘do_stuff’)
end)
我想我可以这样包装它:
defp easier_to_call(func) do
SomeMod.called_a_lot(‘xx’, fn(y) -> func(y) end
end
然后像这样使用它:
easier_to_call(fn(y) ->
SomeMod.needs_called_a_lot_to_be_called_first(‘do_stuff’)
end)
实际上如何在 Elixir 中做到这一点?
【问题讨论】:
-
你的代码看起来不错;只需将
func(y)更改为func.(y),因为它是一个匿名函数。