Curious how to iterate with Enum.map and return multiple values from a set of data.
For example, lets say i want location, daysofweek, starttime, endtime.
My code for finding location looks like-
def get_data do
url = "https://data.sfgov.org/resource/jjew-r69b.json?"
response = HTTPoison.get!(url)
body = Poison.decode!(response.body, keys: :atoms)
body
|> Enum.map(& &1.location)
|> Enum.join(", ")
end
I think there might be a more elegant way to do a few values in one go and create a new struct? Perhaps Enum.map is the wrong way to go for my desired use case.
Looking like a for comprehension might be my best bet?
Well, if you use pattern matching within a list comprehension (for %{a: a, b: b} <- data, do: <something>) you will drop every item that does not fit, whereas Map.take/2 will leave you with more or less what you already have, just with less keys in the maps. Comprehensions also have a convenient :reduce option for whatever you might want to do later.
Again, I know nothing about the API or your requirements towards it, so I’m just thinking out loud about things to keep in mind
Also, it looks like you’re discarding the atoms of the API call return after using them to access the fields of the maps. Atoms are never garbage collected on the BEAM platform which Elixir runs on, so I’d recommend just using plain strings for both performance and security. The general rule of thumb here is to only generate atoms at compile time.
Yeah so if you want a list of strings where each string goes like “location, daysofweek, starttime, endtime” then you could try something like this:
get_values = fn map, keys -> for k <- keys, do: map[k] end
keys_to_get = ["location", "daysofweek", "starttime", "endtime"]
for item <- body, do: Enum.join(get_values.(item, keys_to_get), ", ")
That makes sense! I’m wondering now how to display this information in a sensible format on a LiveView page. Even with the comma separation after Enum.join it’s still a wall of text! I’ve tried a few different ideas with "\n" and the like but am a bit stumped.
Tables work well for tabular data . I’m not a web dev and don’t know anything about LV/Phoenix but I guess I’d look into some simple HTML+CSS, should go well enough.