# Alternative to Streams?

**URL:** <https://discuss.ocaml.org/t/alternative-to-streams/1020>\
**Category:** Learning\
**Created:** [October 27, 2017, 2:33pm UTC](https://discuss.ocaml.org/t/alternative-to-streams/1020 "2017-10-27T14:33:54Z")\
**Posts on this page:** 8\
**Page:** 1

<div class="post-metadata">

**Author:** ![perry](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/perry/32/518_2.png) [@perry](https://discuss.ocaml.org/u/perry)\
**Post date:** [October 27, 2017, 2:33pm UTC](https://discuss.ocaml.org/t/alternative-to-streams/1020/1 "2017-10-27T14:33:54Z")

</div>

This:

[https://caml.inria.fr/mantis/view.php?id=6711](https://caml.inria.fr/mantis/view.php?id=6711)

…leads me to wonder what the future of the Streams interface might be. I’m currently using it in a project so that I can have input to a lexical analyzer come from either a string (for unit testing) or a file with the same API. Should I be building my own similar gadget instead? (I’m not that in love with the interface anyway, I seem to prefer things that return options rather than throwing exceptions…)

---

<div class="post-metadata">

**Author:** ![Drup](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/drup/32/35_2.png) [@Drup](https://discuss.ocaml.org/u/Drup)\
**Post date:** [October 27, 2017, 2:48pm UTC](https://discuss.ocaml.org/t/alternative-to-streams/1020/2 "2017-10-27T14:48:26Z")

</div>

My personal vote goes to [sequence](https://github.com/c-cube/sequence/) and [gen](https://github.com/c-cube/gen/).

In a few OCaml version, we might be able to use [https://github.com/ocaml/ocaml/pull/1002](https://github.com/ocaml/ocaml/pull/1002) to replace those.

---

<div class="post-metadata">

**Author:** ![dbuenzli](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/dbuenzli/32/18_2.png) [@dbuenzli](https://discuss.ocaml.org/u/dbuenzli)\
**Post date:** [October 27, 2017, 2:55pm UTC](https://discuss.ocaml.org/t/alternative-to-streams/1020/3 "2017-10-27T14:55:58Z")

</div>

> [@Drup](#):
>
> In a few OCaml version, we might be able to use [https://github.com/ocaml/ocaml/pull/1002](https://github.com/ocaml/ocaml/pull/1002) to replace those.

But it’s certainly not what you want you want to use if you are streaming from files (and the actual reason why I’m against its inclusion in the stdlib).

---

<div class="post-metadata">

**Author:** ![perry](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/perry/32/518_2.png) [@perry](https://discuss.ocaml.org/u/perry)\
**Post date:** [October 27, 2017, 2:59pm UTC](https://discuss.ocaml.org/t/alternative-to-streams/1020/4 "2017-10-27T14:59:20Z")

</div>

@dbuenzli Somewhat confused by that last comment?

@Drup These both seem a bit more general than what I want, but would you recommend them anyway?

---

<div class="post-metadata">

**Author:** ![dbuenzli](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/dbuenzli/32/18_2.png) [@dbuenzli](https://discuss.ocaml.org/u/dbuenzli)\
**Post date:** [October 27, 2017, 3:10pm UTC](https://discuss.ocaml.org/t/alternative-to-streams/1020/5 "2017-10-27T15:10:48Z")

</div>

> [@perry](#):
>
> @dbuenzli Somewhat confused by that last comment?

When you are streaming from/to files you need to be able to handle errors and resource disposal, these thing won’t do it and simply plugging file reading functions in there will easily result in pain like too many open fds etc. (you would be doing the equivalent of Haskell’s lazy IO, google for that you should be able to find plenty of discussion about its shortcomings).

One way to side step the issue with these iterators is to first read the whole file in memory and start from that, but then you are no longer streaming (and the error handling story e.g. for decoders is still spoty).

There was more discussion in the first [PR](https://github.com/ocaml/ocaml/pull/635) but somehow it got subsequently ignored.

---

<div class="post-metadata">

**Author:** ![perry](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/perry/32/518_2.png) [@perry](https://discuss.ocaml.org/u/perry)\
**Post date:** [October 27, 2017, 3:51pm UTC](https://discuss.ocaml.org/t/alternative-to-streams/1020/6 "2017-10-27T15:51:51Z")

</div>

Okay, so you’re saying that these won’t work for me particularly well. Do you have suggestions on what will? I could also try to build something primitive myself, it wouldn’t be too hard, but I would have hoped for a more generic solution.

(Oh, and on file descriptor leaks, I presume from that there isn’t any way in OCaml to attach a finalizer to a data structure to (for example) free a fd if no one is using it any more…?)

---

<div class="post-metadata">

**Author:** ![Drup](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/drup/32/35_2.png) [@Drup](https://discuss.ocaml.org/u/Drup)\
**Post date:** [October 27, 2017, 4:12pm UTC](https://discuss.ocaml.org/t/alternative-to-streams/1020/7 "2017-10-27T16:12:52Z")

</div>

While @dbuenzli’s concerns about resources are generally justified, using `gen` for lexing/parsing is, in practice, perfectly fine and you can find several people doing exactly that. The usage pattern for parsing a file is extremely simple (open the file, lex, parse, close). Sedlex, in particular, works particularly well with gen.

My advice would be not to overthink the matter, unless you want to spend more time thinking about iterators than what your application does. 🙂

If you have more complex usage patterns, then yes, lazyness comes back to bite you in the ass, but that’s not always the case.

---

<div class="post-metadata">

**Author:** ![dbuenzli](https://sea2.discourse-cdn.com/flex020/user_avatar/discuss.ocaml.org/dbuenzli/32/18_2.png) [@dbuenzli](https://discuss.ocaml.org/u/dbuenzli)\
**Post date:** [October 27, 2017, 4:38pm UTC](https://discuss.ocaml.org/t/alternative-to-streams/1020/8 "2017-10-27T16:38:03Z")

</div>

> [@Drup](#):
>
> While @dbuenzli’s concerns about resources are generally justified, using gen for lexing/parsing is, in practice, perfectly fine and you can find several people doing exactly that.

Indeed if I remember correctly `Gen` are [simple generators](http://okmij.org/ftp/continuations/PPYield/) and in those the producer drives the generation which allows to control the resources more precisely ([here’s](https://gist.github.com/dbuenzli/74be3c9fe51e017790b5) a few implementation of them from first principles).
