I would be more comfortable with something that asserts the shape of the data it receives so I can catch bugs early:
Enum.map(
messages,
fn %{
id: message_id,
text: text,
inserted_at: created_at,
user: %{id: user_id, name: name}
} ->
%{
message_id: message_id,
text: text,
created_at: created_at,
user_id: user_id,
name: name
}
end
)
If even one message doesn’t comply with the expected structure you’ll get an appropriate error. Although to be fair, your code would also blow up but IMO in a slightly less intuitive fashion. I’d prefer mine because it also makes intent clearer.






















