I am starting to learn Elixir, and this is also my first dynamic language, so I really lost work with functions without type declaration.
What I'm trying to do:
def create_training_data(file_path, indices_path, result_path) do 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) do Stream.filter raw_data, fn {_elem, index} -> index_match?(index, indices_path) end end defp index_match?(index, indices_path) do 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) do File.write(path, data) end
When I call the function:
create_training_data("./resources/data/usps.csv","./resources/indices/17.csv","./output.txt")
It returns {: error ,: badarg}. I already checked and the error is in the create_output_file function.
If I comment on the create_output_file function, then I will return to the stream (it makes sense). Maybe the problem may be that I can not stream to the .write file? If this is a problem, what should I do? I did not find anything regarding this in the documentation.
Edit
So the thing is that the path to File.write should be fine, I changed the function this way:
defp create_output_file(data, path) do IO.puts("You are trying to write to: " <> path) File.write(path, data) end
Now again, when I try to run these options:
iex(3)> IaBay.DataHandling.create_training_data("/home/lhahn/data/usps.csv", "/home/lhahn/indices/17.csv", "/home/lhahn/output.txt") You are trying to write to: /home/lhahn/output.txt {:error, :badarg} iex(4)> File.write("/home/lhahn/output.txt", "Hello, World") :ok
So, I still have a problem: badarg, maybe the content that I am transmitting is wrong?