Hi,
I got the following situation: I have multiple chats, wanted to get in my preload the newest message for each chat, but limit: 1 gives only one message, not one for each chat. So I thought about filtering my structure after the preload is done.
Here my code:
result
|> Enum.filter(fn(x) ->
Enum.filter(x.chat_users, fn(y) ->
case y == Enum.at(x.chat_users, 0) do
true ->
Enum.filter(y.chat_messages, fn(z) ->
case z == Enum.at(y.chat_messages, 0) do
true -> true
false -> false
end
end)
false -> false
end
end)
true
end)
In my preload I ordered the chat_messages and chat_users desc, so Enum.at(0) are the newest ones.
I know the inner filter functions have no effect on the outer scope. I know, looks really ugly and is not working.
In simple words I have my result with chats and it has chat_users and each chat_user has chat_messages and i need for each chat the newest user (Enum.at(0)) and the newest chat_message for this user.
How to accomplish this the elixir way? I don’t know how to use reduce inside my nested structure.
I think it is much easier to modify the result after the existing preload. Would take me 5 minutes in languages like C#, JS, Java… so should be also very easy for the elixir pros.