如何在 Elixir 中展平字符串映射图?

How do I flatten a map of map of strings in Elixir?

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)

我正在尝试生成我需要抓取的网址映射。

上面的代码正在为我生成一个字符串映射图。我想将其展平为一个简单的字符串映射。

如何在 Elixir 中完成此操作?

使用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

不过,使用 comprehension 会使代码更简单。

for i <- 1..6, j <- 1..50, do: "https://thepiratebay.org/browse/#{i}00/#{j}/3"