说我有一个函数,稍微冗长,每次调用相同的参数,这个函数也需要很多做一些设置之前,可以调用该模块的任何其他功能它的回调。Elixir - 通过函数参数
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)
怎样才能实际上做到这一点的灵药?
你的代码看起来不错;只要将'func(y)'改为'func。(y)',因为它是一个匿名函数。 – Dogbert