Hello, I’m trying to calculate a leaderboard of user just attended in a contest.
here is the structure of list I have. it is sorted descending so I’m pretty much sure that the first item has the most point and the next of has equal or less point.
[%{user_id:1, total_point:20}, %{user_id: 2, total_point:20}, %{user_id: 3, total_point: 16}]
The thing I want to do in this function is: add a attribute of rank to each map and return it.
And here is my code
result = user_list
|> Enum.with_index
|> Enum.map(fn({x, i}) ->
case i do
0 ->
x = Map.put(x, :rank, 1)
_ ->
case Enum.at(user_list, i-1).total_point > x.total_point do
true ->
x = Map.put(x, :rank, x.last.rank + 1)
false ->
x = Map.put(x, :rank, x.last.rank)
end
end
end)
the problem is if I want to access the current user rank attribute ( which I just dynamically added to the map ) with x.last or even with Enum.at(user_list, i-1).rank it say it does not exist which is true because it will be returned in my result variable when the job is done. so I can not access the last modified ( new map with rank attribute ). how can I have it and use it in this step.
and what I expect from my function to return is:
[%{rank: 1, user_id:1, total_point:20}, %{rank: 1, user_id: 2, total_point:20}, %{rank: 2, user_id: 3, total_point: 16}]
I would appreciate any help on this