Jump to content

Read a specific line from a file: Difference between revisions

Added Elixir implementation
(Added Elixir implementation)
Line 548:
line 7: foreach (char[] line; f.byLine()) {</pre>
 
=={{header|Elixir}}==
The idea is to stream the file, filter the elements and get the remaining element. If does not exist, nil will be throw and we will pattern match with print_line(_). If the value exists, it will match print_line({value, _line_number}) and the value of the line will be printed.
<lang Elixir>
defmodule LineReader do
def get_line(filename, line) do
File.stream!(filename)
|> Stream.with_index
|> Stream.filter(fn {_value, index} -> index == line-1 end)
|> Enum.at(0)
|> print_line
end
defp print_line({value, _line_number}), do: String.trim(value)
defp print_line(_), do: {:error, "Invalid Line"}
end
</lang>
=={{header|Erlang}}==
Using function into_list/1 from [[Read_a_file_line_by_line]].
Anonymous user
Cookies help us deliver our services. By using our services, you agree to our use of cookies.