Hi community, I hope all of you are good…
I’d like to know why this functions ins’t a top level function?
Is it because the only thing that do is call other private function?
defmodule Math do
def sin(x), do: do_sin(x)
defp do_sin(x), do: nil
end
More context:
I’m learning about AST, and I had a challenge with understand why this implementation below:
defmodule TopSecret do
def decode_secret_message_part(ast, acc) do
Macro.prewalk(ast, acc,
fn
{operation, _, [{name, _, args} | _]} = new_ast, acc) when operation in [:def, :defp] ->
{new_ast, [parse_name_to_str(name, args) | acc]}
other, acc -> {other, acc}
end)
end
end
Because this test always fail using that?!
test "ignores not top-level function definition" do
string = """
defmodule Math do
def sin(x), do: do_sin(x)
defp do_sin(x), do: nil
end
"""
ast = TopSecret.to_ast(string)
acc = []
{actual_ast, actual_acc} = TopSecret.decode_secret_message_part(ast, acc)
assert actual_ast == ast
assert actual_acc == acc
end
Link for my final solution:
But again, I don’t understand why I’ve success.