使用 Elixir pipeine 和 Enum 将数据放入列表中

Put the data in the list with Elixir pipeine and Enum

下午好。我有一个模块可以从 link.

获取域名
defmodule URIparser do
  defstruct domains: []
  def make_domain(uri) do
    case URI.parse(uri) do
      %URI{authority: nil} -> URI.parse("http://#{uri}")
      %URI{authority: _} -> URI.parse(uri)
    end
  end
end

之后我使用管道并获得我需要的域。

links = ["https://www.google.com/search?newwindow=1&sxsrf", "https://whosebug.com/questions/ask", "yahoo.com"]
Enum.each(links, fn(x) -> URIparser.make_domain(x) |> Map.take([:authority]) |> Map.values |> IO.inspect end)

这就是最后发生的事情:

["google.com"]
["whosebug.com"]
["yahoo.com"]
:ok

请告诉我们如何补充管道并将所有域放入一个列表中。其他解决方案也可用。

示例:

%{domains: ["google.com", "whosebug.com", "yahoo.com"]}

代替Enum.eachMap.take,使用Enum.mapMap.get

Enum.map(links, fn x ->
  x
  |> URIparser.make_domain()
  |> Map.get(:authority)
end)