在长生不老药中访问中间结果

Accessing intermediate results in elixir

我有以下代码,我想有人想使用管道,但我不知道如何使用 - 我尝试了很多不同的方法,但似乎我缺少了解管道工作原理的关键。不管怎样:下面的代码做了我想要的:

项目has_manybudget_itens/work_items

def show(conn, %{"id" => id}) do
    project = Clients.get_project!(id)
    project = Map.put_new(project, :budget, total_budget(project.budget_items))
    project = Map.put_new(project, :budget_used, budget_used(project.work_items))
    project = Map.put_new(project, :budget_remaining, project.budget - project.budget_used)

    changeset = Clients.change_budget_item(%BudgetItem{project_id: project.id})
    render(conn, "show.html", project: project, changeset: changeset)
  end

有什么帮助吗?

管道运算符 |>/2,根据文档,

introduce the expression on the left-hand side as the first argument to the function call on the right-hand side.

也就是说,project 构建可能会像

project = Clients.get_project!(id)

total = total_budget(project.budget_items)
used = budget_used(project.work_items)

project =
  project
  |> Map.put_new(:budget, total)
  |> Map.put_new(:budget_used, used)
  |> Map.put_new(:budget_remaining, total - used)