0
category_urls = [ "https://thepiratebay.org/browse/100/0/3", "https://thepiratebay.org/browse/200/0/3", "https://thepiratebay.org/browse/300/0/3", "https://thepiratebay.org/browse/400/0/3", "https://thepiratebay.org/browse/500/0/3", "https://thepiratebay.org/browse/600/0/3" ] result = category_urls |> Enum.map(fn category_url -> 1..50 |> Enum.map(fn i -> String.replace(category_url, "/0/", "/#{i}/") end) end) 

I'm trying to generate a map of urls I need to crawl.

The code above is generating a map of map of strings for me. I'd like to just flatten it to a simple map of strings.

How can I accomplish this in Elixir?

1 Answer 1

2

Use Enum.flat_map/2.

category_urls = [ "https://thepiratebay.org/browse/100/0/3", "https://thepiratebay.org/browse/200/0/3", "https://thepiratebay.org/browse/300/0/3", "https://thepiratebay.org/browse/400/0/3", "https://thepiratebay.org/browse/500/0/3", "https://thepiratebay.org/browse/600/0/3" ] result = category_urls |> Enum.flat_map(fn category_url -> 1..50 |> Enum.map(fn i -> String.replace(category_url, "/0/", "/#{i}/") end) end) end 

Though, use comprehension would make the code much simpler.

for i <- 1..6, j <- 1..50, do: "https://thepiratebay.org/browse/#{i}00/#{j}/3" 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.