2016-08-06 9 views
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) 

私はクロールする必要があるURLのマップを生成しようとしています。エリクサーの文字列マップのマップをどのように平坦化するのですか?

上記のコードは、私のための文字列のマップのマップを生成しています。私はそれを単純な文字列のマップにフラット化したいと思います。

エリクサーでこれをどのように達成できますか?

答えて

2

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" 
関連する問題