Replying to a private mail from Brian:
kirillkh wrote:Note that the fifth version is only perfect because the author knew that he didn't need to keep the line around. Doing something even moderately more complicated would almost certainly require an allocation in the main loop- not that this is that big of a problem (Ocaml allocation is lightning fast).
This should be a FAQ.
Since we're talking of 10+ lines of code and only one case among many possible (you might also want to do something fairly similar, but not quite the same, as iterating over all words or characters in a file, doing something else than counting, etc.), I would rather see it implemented in a library as combinator. What I have in mind is a function that goes over a file and invokes some user code on each block of bytes/characters/lines/words/... The points of customization would be:
* how to detect the start and end of block
* routine to pass the blocks to
Then, on top of this combinator, build block-specific ones: for byte, char, line, word blocks. Also make it possible to customize buffering behavior.
Being new to OCaml, I'm interested in comments - is what I suggest a good idea? If yes, why hasn't anyone implemented it yet? One could argue that it's trivial and can be implemented in each use case anew, but among 5 different solutions posted so far, each has its own problems, besides the supposedly ideal 5-th -- I'd take this as an indication that, although simple, it's not really trivial to write this thing.
The problem with this library is when to stop. A real simple "fold over the lines of a file" interface might be nice, but there'll always be pressure to add just one more feature, deal with just one more slightly more complex case- at the end of which you get a badly specified and badly implemented parser combinator library.
Brian