elixirgenstage

Elixir flow: piping multiple arguments


I'm trying to write a Flow that looks like so:

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

The other functions in the module look like this:

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

But I keep getting this error:

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

Line 18 is the &cleanup/2 line. What am I doing wrong here? It feels like I'm not returning the right values somehow...


Solution

  • process/1 returns a tuple {id, sys_out}, while cleanup/2 is declared having an arity 2.

    Iterating a list of tuples implies the iterator must be of arity 1. You should decompose your tuple inside cleanup/1:

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

    and call it like:

    Flow.each(&cleanup/1)
    

    The same applies to the subsequent respond.