1

I have a map (errors of Ecto.Changeset) like that:

%{ field_a: ["error 1", "error 2"], field_b: ["something here"] } 

I want it to be a

[ "field_a error 1", "field_a error 2", "field_b something here" ] 

I tried to implement it with

Enum.map(map, fn({ key, errors }) -> # another anonymous function which generates strings end) 

but got a compilation error since elixir doesn't support nested anonymous functions right now.

How can I implement this convertation?

1 Answer 1

3

Although you can use Enum.flat_map, I believe a for would be clearer here:

errors = %{ field_a: ["error 1", "error 2"], field_b: ["something here"] } formatted = for {field, messages} <- errors, message <- messages, do: "#{field} #{message}" IO.inspect formatted 

Output:

["field_a error 1", "field_a error 2", "field_b something here"] 

but got a compilation error since elixir doesn't support nested anonymous functions right now.

That's for anonymous recursive functions; Elixir supports nested anonymous functions just fine.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.