Will love to hear your critique and comments
I have two lists of maps, location_list
and user_location_list
based off two structs: Location
and UserLocation
respectively.
Here, each list shows a partial listing of all the fields for brevity.
location_list = [%Location{location_id: 1, display_name: "Location One"}, %Location{location_id: 2, display_name: "Location Two"}]
user_location_list = [%UserLocation{location_id: 1, user_id: 1}, %UserLocation{location_id: 2, user_id:1}]
Basically after retrieving the location_id
values from user_location_list
based on a user_id
search, I then want to get the corresponding location records from location_list
with the fields location_id
and display_name
(so I can populate a list in Phoenix EEx template with relevant hyperlinks)
My code is as follows:
location = Enum.map(location_list, fn location -> location = Map.take(location, [:location_id, :display_name]) end)
This is to strip out the Struct so it can read [%{location_id: 1, display_name: "Location One"}...]
AND display only a subset of the fields in the Location struct
user_location = Enum.map(user_location_list, fn user_location -> user_location.location_id end)
So this produces a list [1, 2]
#this will contain the filtered locations based on values in user_location
dropdown_list = Enum.filter(location, fn (location) -> location.location_id in user_location end)
-
Please critique my approach so I can get better.
-
I really hope I can construct a pipeline here but I dont know how to. For starters I would be thinking like
dropdown_list = location_list
|> Enum.map(location_list, fn location -> location = Map.take(location, [:location_id, :display_name]) end)
|> Enum.filter(&l, fn (location) -> location.location_id in user_location end)
But it does not seem to work.
-
And I have heard so much about
Enum.reduce
. Is there any way it can be applied here to make the code better? -
Finally, any suggestions how you would have named the variables
location
anduser_location
(as they really are lists after all butlocation_list
is also a list itself)
Thanks very much.