elixir - receiving :badarg on File.write -


i starting learn elixir, , first dynamic language, lost working functions without type declaration.

trying do:
def create_training_data(file_path, indices_path, result_path)     file_path     |> file.stream!     |> stream.with_index     |> filter_data_with_indices(indices_path)     |> create_output_file(result_path)   end    def filter_data_with_indices(raw_data, indices_path)     stream.filter raw_data, fn {_elem, index} ->       index_match?(index, indices_path)     end   end    defp index_match?(index, indices_path)     indices_path     |> file.stream!     |> enum.any? fn elem ->       (elem       |> string.replace(~r/\n/, "")       |> string.to_integer       |> (&(&1 == index)).())     end   end    defp create_output_file(data, path)     file.write(path, data)   end 

when call function:

create_training_data("./resources/data/usps.csv","./resources/indices/17.csv","./output.txt") 

it returns {:error, :badarg}. checked , error on create_output_file function.

if comment out function create_output_file, stream (kinda makes sense). problem maybe cannot give stream file.write? if problem, should do? did not find regarding on documentation.

edit

so, thing path file.write should ok, modified function this:

defp create_output_file(data, path)     io.puts("you trying write to: " <> path)     file.write(path, data) end 

now again when try run these parameters:

iex(3)> iabay.datahandling.create_training_data("/home/lhahn/data/usps.csv", "/home/lhahn/indices/17.csv", "/home/lhahn/output.txt") trying write to: /home/lhahn/output.txt {:error, :badarg} iex(4)> file.write("/home/lhahn/output.txt", "hello, world") :ok 

so, still got :badarg problem, maybe content passing not right?

first thing: feed tuples write. must first extract data them:

file_path |> file.stream! |> stream.with_index |> filter_data_with_indices(indices_path) |> stream.map(fn {x,y} -> x end)    # <------------------ here |> create_output_file(result_path) 

second thing:

it looks can't feed stream file.write/2 because expects iodata. if convert stream list before writing, goes well:

defp create_output_file(data, path)   data = enum.to_list(data)   :ok = file.write(path, data) end 

Comments

Popular posts from this blog

apache - PHP Soap issue while content length is larger -

asynchronous - Python asyncio task got bad yield -

javascript - Complete OpenIDConnect auth when requesting via Ajax -