Elixir 流程:传递多个参数

Elixir flow: piping multiple arguments

我正在尝试编写一个 Flow,看起来像这样:

def perform(id) do
  id
  |> Flow.from_stage(max_demand: 10)
  |> Flow.map(&download(&1))
  |> Flow.map(&process(&1))
  |> Flow.each(&cleanup(&1, &2))
  |> Flow.each(&respond(&1, &2))
  |> Flow.run
end

模块中的其他函数如下所示:

def download(id) do
  Service.get_images(id)
  id
end

def process(id) do
  %Result{out: sys_out, status: _} = Porcelain.exec("osascript",
    ["#{File.cwd!}/lib/script/test", "#{id}", "#{Application.get_env(:app, :server_env)}"]
  )
  {id, sys_out}
end

def cleanup(id, files) do
  for file <- String.split(files, " ") do
    System.cmd("exiftool", ["#{File.cwd!}/#{file}"])
  end
  {id, files}
end

def respond(id, files) do
  files = String.split(files, " ")
  System.cmd("curl", [" -d 'dowhatever=#{files[0]}&else=#{files[1]}' -k #{Application.get_env(:app, :api)}what/#{id}/notify"])
  :ok
end

但是我一直收到这个错误:

** (FunctionClauseError) no function clause matching in Flow.each/2
...
(app) lib/app/processor.ex:18: App.Processor.perform/1

第 18 行是 &cleanup/2 行。我在这里做错了什么?感觉好像我没有以某种方式返回正确的值...

process/1 returns 元组 {id, sys_out},而 cleanup/2 被声明为具有元数 2.

迭代元组列表意味着迭代器必须为 1。你应该分解你的元组 inside cleanup/1:

- def cleanup(id, files) do
+ def cleanup({id, files}) do

并这样称呼它:

Flow.each(&cleanup/1)

后续respond.

同理