0

I am using the highlandjs library to read files and add an end card to their contents before displaying them in the console:

const readFile = highland.wrapCallback(fs.readFile);
const addEndCard = x => x + '\nx---THE END---x\n';
files.map(readFile).parallel(3).map(addEndCard).each(console.log);

I would like to use highland.compose to wrap these into a single function call, I started with:

const readAllFiles = highland.compose(
  highland.map,
  addEndCard,
  readFile
);
readAllFiles(files).parallel(3).each(console.log);

I get the error:

TypeError: readAllFiles(...).parallel is not a function
    at Object.<anonymous> (/home/vamsi/Do/highland-fun/index.js:14:21)
    at Module._compile (module.js:398:26)
    at Object.Module._extensions..js (module.js:405:10)
    at Module.load (module.js:344:32)
    at Function.Module._load (module.js:301:12)
    at Function.Module.runMain (module.js:430:10)
    at startup (node.js:141:18)
    at node.js:980:3

It looks like the composed function is not returning a highland stream.

vamsiampolu
  • 6,328
  • 19
  • 82
  • 183

2 Answers2

0

readFile returns a stream but _.compose() composes functions not streams.

I believe what you're looking for is _.pipeline().

Gunar Gessner
  • 2,331
  • 23
  • 23
0

The reason your composed function is not returning a stream is that by passing map as you have done there you are using a curried function that is expecting two arguments. compose works by passing the result of applying each function in series from right to left, i.e, each function in the composition must be unary; when map receives a single argument it returns a function expecting the final argument rather than a result and it breaks the rest of the functions downstream. Something like this should work:

highland(files) // I presume files is an array
  .map(highland.compose(highland.map(addEndCard), readFile))
  .parallel(3)
  .each(console.log);

Also, the only way I could maintain your original API was like this:

const readAllFiles = highland.compose(
  highland.map(highland.map(addEndCard)),
  files => files.map(file => readFile(file))
);

readAllFiles(files).parallel(3).each(console.log);

Btw, are you using parallel over merge for a reason? If the order the files are processed in doesn't matter, use merge.

Stefano
  • 2,056
  • 2
  • 15
  • 13