I’m not sure if the solution of @LostKobrakai is correct. What happens if one of smoothie would have :error
instead of :ok
?
Therefore I would use following result library which can handle this error state and the code could look like:
def get_smoothies_recipe() do
smoothies =
get_smoothies_url()
|> get_smoothies_html_body()
|> Enum.map(fn body ->
[get_smoothie_name(body), get_smoothie_ingredients(body), get_smoothie_directions(body)]
|> Result.fold()
|> Result.map(fn [name, ingredients, directions] -> %{name: name, ingredients: ingredients, directions: directions}
end)
end)
|> Result.fold()
end