Skip to content

Architecture

Vitaly Tomilov edited this page Sep 28, 2022 · 20 revisions

This library features a simple and open architecture.

Each operator here is just a function that takes some parameters, and returns another function that takes the source iterable, applies the transformation to it, and returns a new iterable. This means that technically, you can execute any operator independently, outside the pipeline:

const i1 = concat(3, 4)([1, 2]);
const i2 = count()(i1);

console.log([...i1]); //=> [1, 2, 3, 4]
console.log([...i2]); //=> [4]

This however doesn't mean that you should ever be using it this way, because function pipe adds two important things:

  • Strict type control in TypeScript, which you will lose, and have to resolve to any, which is quite bad.
  • It extends the final iterable with property first, to simplify use of one-value iterables, plus method catch, so you can chain error handlers.

A more reasonable way of taking advantage of this architecture is to construct pipelines dynamically:

function buildPipeline(data: Iterable<number>, unique?: boolean) {
    let i = pipe(
          data,
          filter(f => f > 100),
          defaultEmpty([1, 2, 3]) // default, if output is empty
    );
    
    if(unique) {
        i = pipe(i, distinct()); // make unique
    }

    // handle errors, and return:
    return i.catch(err => {
        console.log(err);
    });
}
Clone this wiki locally