How to create a lazy-evaluated range from a file?

E_net4 has many friends

The File I/O API in Phobos is relatively easy to use, but right now I feel like it's not very well integrated with D's range interface.

I could create a range delimiting the full contents by reading the entire file into an array:

import std.file;
auto mydata = cast(ubyte[]) read("filename");
processData(mydata); // takes a range of ubytes

But this eager evaluation of the data might be undesired if I only want to retrieve a file's header, for example. The upTo parameter doesn't solve this issue if the file's format assumes a variable-length header or any other element we wish to retrieve. It could even be in the middle of the file, and read forces me to read all of the file up to that point.

But indeed, there are alternatives. readf, readln, byLine and most particularly byChunk let me retrieve pieces of data until I reach the end of the file, or just when I want to stop reading the file.

import std.stdio;
File file("filename");
auto chunkRange = file.byChunk(1000); // a range of ubyte[]s
processData(chunkRange); // oops! not expecting chunks!

But now I have introduced the complexity of dealing with fixed size chunks of data, rather than a continuous range of bytes.

So how can I create a simple input range of bytes from a file that is lazy evaluated, either by characters or by small chunks (to reduce the number of reads)? Can the range in the second example be seamlessly encapsulated in a way that the data can be processed like in the first example?

Vladimir Panteleev

You can use std.algorithm.joiner:

auto r = File("test.txt").byChunk(4096).joiner();

Note that byChunk reuses the same buffer for each chunk, so you may need to add .map!(chunk => chunk.idup) to lazily copy the chunks to the heap.

この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。

侵害の場合は、連絡してください[email protected]

編集
0

コメントを追加

0

関連記事

分類Dev

When and how many times can a clojure file be evaluated with leiningen?

分類Dev

Gatling: How can I load a range from a JSON feeder file?

分類Dev

How to create a .deb file from installed package?

分類Dev

How to create route from GPX file with Skobbler?

分類Dev

How to create a list with a range of years?

分類Dev

How to get a String from a Lazy.Builder?

分類Dev

How is an Or statement evaluated in VBA?

分類Dev

How is A *= B *= A *= B evaluated?

分類Dev

Trying to create a range from a set of values

分類Dev

Google Sheets: How do I create an array from a range, adding a column with a constant literal value in every row?

分類Dev

How do I create 3 or four classes of diameter from a range of diameters in R?

分類Dev

Python: How to create a csv string (no file) from a list of dictionaries?

分類Dev

How to create a File object from binary data in JavaScript

分類Dev

How to create automatic text file from a list in R?

分類Dev

how to create an audio clip from a video file using command line?

分類Dev

How do i get the last file from directory by the file name and then create the next file?

分類Dev

How to lazy load a js file in React (for a multilingual app)

分類Dev

How to create range slider in angular 5

分類Dev

How to create a no overlapping Validation for an integer range

分類Dev

How to create matrix that include range of numbers?

分類Dev

How is shift operator evaluated in C?

分類Dev

How to create file with no headers

分類Dev

How to create a Kconfig file

分類Dev

Create a list in a list from a file

分類Dev

How to use components from modules in others lazy load modules

分類Dev

How do I move from my html file to another html file to create an iOS hybrid app?

分類Dev

How do I move from my html file to another html file to create an iOS hybrid app?

分類Dev

Create DataFrame rows from date range unnesting field

分類Dev

How to create a mdb file with ODBC?

Related 関連記事

  1. 1

    When and how many times can a clojure file be evaluated with leiningen?

  2. 2

    Gatling: How can I load a range from a JSON feeder file?

  3. 3

    How to create a .deb file from installed package?

  4. 4

    How to create route from GPX file with Skobbler?

  5. 5

    How to create a list with a range of years?

  6. 6

    How to get a String from a Lazy.Builder?

  7. 7

    How is an Or statement evaluated in VBA?

  8. 8

    How is A *= B *= A *= B evaluated?

  9. 9

    Trying to create a range from a set of values

  10. 10

    Google Sheets: How do I create an array from a range, adding a column with a constant literal value in every row?

  11. 11

    How do I create 3 or four classes of diameter from a range of diameters in R?

  12. 12

    Python: How to create a csv string (no file) from a list of dictionaries?

  13. 13

    How to create a File object from binary data in JavaScript

  14. 14

    How to create automatic text file from a list in R?

  15. 15

    how to create an audio clip from a video file using command line?

  16. 16

    How do i get the last file from directory by the file name and then create the next file?

  17. 17

    How to lazy load a js file in React (for a multilingual app)

  18. 18

    How to create range slider in angular 5

  19. 19

    How to create a no overlapping Validation for an integer range

  20. 20

    How to create matrix that include range of numbers?

  21. 21

    How is shift operator evaluated in C?

  22. 22

    How to create file with no headers

  23. 23

    How to create a Kconfig file

  24. 24

    Create a list in a list from a file

  25. 25

    How to use components from modules in others lazy load modules

  26. 26

    How do I move from my html file to another html file to create an iOS hybrid app?

  27. 27

    How do I move from my html file to another html file to create an iOS hybrid app?

  28. 28

    Create DataFrame rows from date range unnesting field

  29. 29

    How to create a mdb file with ODBC?

ホットタグ

アーカイブ