Search code examples
elixirelixir-iexex

Elixir piping output to an anonymous function


I'm currently learning elixir and i'm trying to print functions and their arity

print = fn ({function , arity}) ->
        IO.puts "#{function}/#{arity}" 
        end

Enum.__info__(:functions) |> Enum.each(print.())

this returns

** (BadArityError) #Function<0.60149952 in file:learn.exs> with arity 1 called with no arguments
    learn.exs:5: (file)
    (elixir) lib/code.ex:767: Code.require_file/2

Solution

  • Your issue is how you pass print to Enum.each. The variable print is already bound to a function. When you do print.() you're calling that function with no arguments and passing the result to Enum.each. What you want instead is to pass the print function itself as an argument to Enum.each. So:

    Enum.__info__(:functions) |> Enum.each(print)