有条件地附加到 Elixir 中的列表?

Conditionally appending to a list in Elixir?

我在 Python 几个月后开始研究 Elixir 代码,我对 Elixir 的惯用记忆很模糊。

此代码有效:

# Define workers and child supervisors to be supervised
children = [
  # Start the Ecto repository
  supervisor(Ssauction.Repo, []),
  # Start the endpoint when the application starts
  supervisor(SsauctionWeb.Endpoint, []),

  supervisor(Absinthe.Subscription, [SsauctionWeb.Endpoint]),
]

children
= if System.get_env("PERIODIC_CHECK") == "ON" do
    Enum.concat(children, [worker(Ssauction.PeriodicCheck, [])])
  else
    children
  end

但我敢肯定这很尴尬。如何以地道的方式重写它?

您可以定义一个接受条件的辅助函数:

defp append_if(list, condition, item) do
  if condition, do: list ++ [item], else: list
end

然后像这样使用它:

[1,2,3]
|> append_if(true, 4)
|> append_if(false, 1000)

产生:

[1, 2, 3, 4]

您似乎在使用 Supervisor.Spec 模块,该模块已弃用。您可以像这样以更现代的方式定义您的监督树:

children =
  [
    Ssauction.Repo,
    SsauctionWeb.Endpoint,
    {Absinthe.Subscription, [SsauctionWeb.Endpoint]}
  ]
  |> append_if(System.get_env("PERIODIC_CHECK") == "ON", Ssauction.PeriodicCheck)

虽然您可能需要修改您的子主管以实现 Supervisor 行为。


如果您要构建大型列表,通常会在列表前添加,然后对结果进行一次反转,以避免每次遍历整个列表的应用:

defp prepend_if(list, condition, item) do
  if condition, do: [item | list], else: list
end

def build_list do
  []
  |> prepend_if(true, 1)
  |> prepend_if(true, 2)
  |> prepend_if(true, 3)
  |> prepend_if(false, nil)
  |> prepend_if(false, 5000)
  |> Enum.reverse()
end

你也可以;

[
  supervisor(Ssauction.Repo, []),
  supervisor(SsauctionWeb.Endpoint, []),
  supervisor(Absinthe.Subscription, [SsauctionWeb.Endpoint]),
]
|> Kernel.++(if System.get_env("PERIODIC_CHECK") == "ON" do
   [worker(Ssauction.PeriodicCheck, [])]
  else
   []
  end
)