I’m working to port some clj to cljs and needed a cljs version of line-seq. Having spent most of my cljs time in the browser, using NodeJS is a bit of a new world. There’s almost certainly a package to read files line by line, but I decided to roll my own because:
It’d be good experience reading through NodeJS docs
I’d have to wrap some cljs anyway
I’m a programmer :)
I saw this post on streams and decided to convert it to cljs.
First of, we load in our NodeJS libs:
As we read the file stream, we want to transform it from generic chunks of data to lines of text. We define a transform fn that we’ll use on our stream:
We also want to make sure we flush the last bit of data we read:
Lastly, we set up our file stream and pipe it through our transformer:
I generally prefer using channels instead of passing around callbacks, so I actually used this version with core.async:
This worked, but I really want the laziness of line-seq without the asynchronous code. In my next post, I’ll implement line-seq to work just the way we want.