【问题标题】:Elixir flow: piping multiple argumentsElixir flow:管道多个参数
【发布时间】:2017-10-18 13:40:56
【问题描述】:

我正在尝试写一个看起来像这样的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 行是 &amp;cleanup/2 行。我在这里做错了什么?感觉好像我没有以某种方式返回正确的值......

【问题讨论】:

    标签: elixir genstage


    【解决方案1】:

    process/1 返回一个元组{id, sys_out},而cleanup/2 被声明为具有元组2

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

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

    然后这样称呼它:

    Flow.each(&cleanup/1)
    

    同样适用于后续的respond

    【讨论】:

      猜你喜欢
      • 2016-07-25
      • 1970-01-01
      • 2020-12-01
      • 2019-05-15
      • 2019-08-09
      • 2017-10-12
      • 2014-02-07
      • 2023-03-06
      • 2021-03-08
      相关资源
      最近更新 更多