Giter VIP home page Giter VIP logo

Comments (4)

trxcllnt avatar trxcllnt commented on May 25, 2024

Does this not work?

function operator1(source) {
  return source.pipe(map(x => x * 3), filter(x => x % 2 == 0));
}

function operator2(source) {
  return map(x => x + 100)(source);
}

from([1,2,3]).pipe(operator1, operator2);

from ixjs.

buzzware avatar buzzware commented on May 25, 2024

@trxcllnt yes that does work.
I suppose I could either

  1. just use your operator1 style, and still use pipe for a single operator
  2. have the abstract operator function return either op() or [opA(),opB()] and use lodash flatten(castArray(operator())) the array before calling pipe(...operators).

I don't find your operator2 style intuitive. But if I was to adopt that, I would expect
pipe(map(x => x + 100))(source)
to work, but it has to be
pipe(source,map(x => x + 100))

If you're not going to have chaining operators on the source object, then being able to use standalone pipe() recursively within chaining pipe() would make a smooth experience.

For now, this achieves what I need :

const { from, pipe } = require('ix/asynciterable');
const { map,filter } = require('ix/asynciterable/operators');
const _ = require('lodash');

function joinOps(...ops) {
  return _.flatten(ops);
}

(async function() {
  try {

    function operator1() {
      return [
        map(x => x * 3),
        filter(x => x % 2 != 0)
      ];
    }

    function operator2() {
      return map(x => x + 100);
    }

    function logger(data) {
      console.log(data);
      return data;
    }

    try{
      let operators = [operator1,operator2];
      operators = _(operators).map(op => op()).flatten().value();
      let result = await from([1,2,3]).pipe(...operators).forEach(logger);
      console.log('done');
    }
    catch(err) {
      console.error('pipeline failed with error:', err);
    }

    console.log('finished');
  } catch(e) {
    console.log(e);
  } finally {
    process.exit();
  }
})();

from ixjs.

trxcllnt avatar trxcllnt commented on May 25, 2024

The style I described is how all the Ix operators are implemented; functions which return functions that take a source iterable and return a result iterable. pipe() is a specialization of left-fold over a list of functions with the signature (source: TSource) => TResult.

It sounds like you want a curried version of pipe, which is valid but straightforward to write yourself:

// could also be:
// const compose = (...ops) => (source) => source.pipe(...ops);
const compose = (...ops) => (source) => ops.reduce((source, op) => op(source), source);

function operator1() {
  return compose(map(x => x * 3), filter(x => x % 2 == 0));
}

function operator2() {
  return map(x => x + 100);
}

from([1,2,3]).pipe(operator1(), operator2());

from ixjs.

buzzware avatar buzzware commented on May 25, 2024

Thanks for that, I haven't had enough functional experience to write that quickly.
Yes, your compose() is what I was expecting from the standalone pipe().
This can't be a special need - could you add something like compose to the library?

from ixjs.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.