list - 有条件地附加到 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
推荐阅读
- node.js - 在 Azure 函数(Nodejs)中使用 Jest 进行测试时未定义 process.env 变量
- android - Xamarin-Android SDK 26
- api - Kitsu API 身份验证返回无效授权
- react-native - 如何将 componentDidMount 用于选项卡?
- node.js - Azure Function NodeJS:blobService.createAppendBlobFromLocalFile Promise 无法解析
- c# - 如何在 C# 中向 DataTable 添加数据
- asp.net-core - 如何从依赖注入类中接收 IHubContext?
- sql-server - 需要按 part_name 求和营业额,然后按段选择 MAX(营业额) - 需要先按一个标准求和,然后按另一个标准求和
- javascript - 如何获取值不等于null的对象的键和值?
- php - 即使在 PDO 准备语句和绑定参数之后也无法在数组中转义单引号