Can we have this convenience as a function in the File module: read(file, start, length)

150 views
Skip to first unread message

Charles Okwuagwu

unread,
May 9, 2016, 1:36:15 PM5/9/16
to elixir-lang-core
Can we have this convenience as a function in the File module?

read(file, start, length)




The suggested solution is not immideiately obvious

{:ok, file} = :file.open(source_file, [:read, :binary]) 
:file.position(file, 5) # start_position = 5
:file.read(file, 10) # bytes to read = 10


eksperimental

unread,
May 12, 2016, 8:25:40 AM5/12/16
to elixir-l...@googlegroups.com
Hi Charles,
thank you for your proposal.

I think it could be an interesting addition. But rather than just a
convenience function, make it behave more like its IO
conterpart: IO.read/2

Where you can pass the byte length, or :line.
Because sometimes is not about the bytes, but about the information
held in a certain number of lines in the file.

I'm thinking something like

File.read(file_path, :line, 1)

# or to read the first five lines
File.read(file_path, :line, 1..5)

I would like to know how the rest of the list feels about it.

José Valim

unread,
May 12, 2016, 8:36:05 AM5/12/16
to elixir-l...@googlegroups.com
We need to consider that performance wise they are quite different. We can move the cursor when reading lines, we can't move the cursor when reading bytes. We would actually need to load the file into Elixir. It would probably be more interesting to add a File.position instead of hiding the underlying behaviour.


José Valim
Skype: jv.ptec
Founder and Director of R&D


--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/20160512192534.35e62698.eksperimental%40autistici.org.
For more options, visit https://groups.google.com/d/optout.

Charles Okwuagwu

unread,
May 12, 2016, 8:45:28 AM5/12/16
to elixir-lang-core, jose....@plataformatec.com.br
def read(file, start, length) do
  {ok, f} = :file.open(file, [:binary])
  {ok, data} = :file.pread(f, start, length)
  :file.close(f)
  data
end
We are not reading the whole file, or are we? :file.pread/3

José Valim

unread,
May 12, 2016, 9:08:17 AM5/12/16
to Charles Okwuagwu, elixir-lang-core
Not in your example but if you want to do it per line, then yes. 
--

eksperimental

unread,
May 12, 2016, 9:38:43 AM5/12/16
to elixir-l...@googlegroups.com
I was thinking of delegating stuff to IO.read when dealing with lines

On Thu, 12 May 2016 15:08:15 +0200
José Valim <jose....@plataformatec.com.br> wrote:

> Not in your example but if you want to do it per line, then yes.
>
> On Thursday, May 12, 2016, Charles Okwuagwu <okwua...@gmail.com>
> wrote:
>
> > def read(file, start, length) do
> > {ok, f} = :file.open(file, [:binary])
> > * {ok**, data} = :file.pread(f, start,** length)*
> > :file.close(f)
> > dataend
> >
> > We are not reading the whole file, or are we? *:file.pread/3*
> >
> >
> > On Thursday, May 12, 2016 at 1:36:05 PM UTC+1, José Valim wrote:
> >>
> >> We need to consider that performance wise they are quite
> >> different. We can move the cursor when reading lines, we can't
> >> move the cursor when reading bytes. We would actually need to load
> >> the file into Elixir. It would probably be more interesting to add
> >> a File.position instead of hiding the underlying behaviour.
> >>
> >>
> >> *José Valim*

Charles Okwuagwu

unread,
May 12, 2016, 9:58:38 AM5/12/16
to elixir-lang-core, eksper...@autistici.org
I think we are best served with two functions:

read_bytes(file, start, bytes)

read_lines(file, start, lines)

You an then optimize as required for each specific case

Charles Okwuagwu

unread,
May 22, 2016, 9:20:41 PM5/22/16
to elixir-lang-core, eksper...@autistici.org

Please is this still being considered?

Thanks

José Valim

unread,
May 23, 2016, 4:19:49 AM5/23/16
to elixir-l...@googlegroups.com
It would probably be more interesting to add  a File.position instead of hiding the underlying behaviour. 

My position on the topic is the same. Your solution to the problem is 3 lines of code. It doesn't seem to be a common use case either, so I don't see the reasoning in adding it to Elixir. If the issue is with discovery, I would rather add File.position, as mentioned above, instead of providing convenience functions without adding the primitive operations.



José Valim
Skype: jv.ptec
Founder and Director of R&D
Reply all
Reply to author
Forward
0 new messages