Dar*_*zer 1 list append 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
Run Code Online (Sandbox Code Playgroud)
但我确信这很尴尬。如何用惯用的方式重写这个?
您可以定义一个接受条件的辅助函数:
defp append_if(list, condition, item) do
if condition, do: list ++ [item], else: list
end
Run Code Online (Sandbox Code Playgroud)
然后像这样使用它:
[1,2,3]
|> append_if(true, 4)
|> append_if(false, 1000)
Run Code Online (Sandbox Code Playgroud)
生产:
[1, 2, 3, 4]
Run Code Online (Sandbox Code Playgroud)
您似乎正在使用该Supervisor.Spec模块,但该模块已被弃用。您可以用更现代的方式定义监督树,如下所示:
children =
[
Ssauction.Repo,
SsauctionWeb.Endpoint,
{Absinthe.Subscription, [SsauctionWeb.Endpoint]}
]
|> append_if(System.get_env("PERIODIC_CHECK") == "ON", Ssauction.PeriodicCheck)
Run Code Online (Sandbox Code Playgroud)
尽管您可能需要修改您的子主管才能实现该 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
Run Code Online (Sandbox Code Playgroud)