سؤال

How do you specify a function as a parameter to another function in Elixir? For instance, pass foo to bar so that bar can then call foo. What is the syntax both in the calling function and the function that receives it?

هل كانت مفيدة؟

المحلول

Use &Module.function/arity to pass it, and .(…) to call it.

For example:

def my_hof(f)
    f.([1, 2, 3], &(&1 * 2))
end

my_hof(&Enum.map/2)
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top