Git Product home page Git Product logo

Comments (8)

brainkim avatar brainkim commented on May 29, 2024

I am almost down for this. However:

  1. There are multiple libraries that do array-like iterator/async iterator functions.

Could we use these libraries instead?

  1. As a general rule, I want to avoid exporting functions under the @channel namespace which do not use the channel class. Functions like filterChannel could be defined purely with an async generator:
async function *filterAsyncIter<T>(iter: AsyncIterable<T>, pred: (value: T) => any): AsyncIterable<T> {
  for await (const value of iter) {
    if (pred(value)) {
      yield value;
    }
  }
}

Is there a reason we would use channels for these methods?

These should probably be functions and not instance level methods so the API doesn't differ from async iterators.

Yes, my thoughts exactly! 🥰

from repeater.

elderapo avatar elderapo commented on May 29, 2024

I believe these should return channels or utilize some API that allows chaining.

const numbersChannel = new Channel<number>(() => {});

const oddNumbersChannel = filterChannel(numbersChannel, item => item % 2 !== 0);
const oddNumbersGreatherThan50Channel = filterChannel(numbersChannel, item => item > 50);

const newChannel = something(numbersChannel) <-- not idea about the name
    .use(filterChannel, item => item % 2)
    .use(filterChannel, item => item > 50)
    .toChannel();

or

const numbersChannel = new Channel<number>(() => {});

const newChannel = something(numbersChannel)
    .filter(item => item % 2)
    .map(item => `number:${item}`)
    .toChannel();

Also, I think while at util functions we should add ones for converting async generators to channels and vice versa.

from repeater.

brainkim avatar brainkim commented on May 29, 2024

Also, I think while at util functions we should add ones for converting async generators to channels and vice versa.

If we adhere to the “no differences between async generators and channels” rule, we should never have to convert async generators to channels.

Even if you want to create a chainable API, I don’t think there’s a need to have a toChannel call at the end. All you really have to do is make the something function return an async iterable somehow and then use this class interchangeably with channels. This could be a fun project idea!

from repeater.

elderapo avatar elderapo commented on May 29, 2024

Actually, now that I think about this “no differences between async generators and channels” rule it does not make much sense in a sense of extending possibilities of async generators.

The whole point of this package is to make work with "async iterable like" easier no? Channels are essentially an implementation of async iterators. As long as they extend the API of async iterators all the libraries utilizing async iterators should work just fine with channels.

There is zero need for converting channels to async iterators because channels are essentially async iterators. However async iterators are not channels so that's why we should create some asyncGeneratorToChannel utility function.

from repeater.

brainkim avatar brainkim commented on May 29, 2024

Actually, now that I think about this “no differences between async generators and channels” rule it does not make much sense in a sense of extending possibilities of async generators.

Yeah this library isn’t about extending async generators, just making it easy to convert callback-based APIs to async iterator-based APIs. Once you have channels, they are completely indistinguishable from async generator objects.

As long as they extend the API of async iterators all the libraries utilizing async iterators should work just fine with channels.

Yes, exactly.

However async iterators are not channels so that's why we should create some asyncGeneratorToChannel utility function.

This is easy to write with the Channel API:

function convertIterToChan(iter) {
  return new Channel(async (push, stop) => {
    for await (const value of Channel.race([iter, stop])) {
      await push(value);
    }

    stop();
    return stop;
  });
}

But again, my question is, if you already have an async iterator/able, what is the point of upgrading it to a channel? What additional value is derived if you assume that channels are indistinguishable from async generators? Insofar as channels are indistinguishable from async generator objects, you could write an equivalent convertIterToChan with an async generator too!

async function *convertIterToGen(iter) {
  for await (const value of iter) {
    yield value;
  }
}

Or even shorter:

async function *convertIterToGen(iter) {
  yield *iter;
}

The only difference between convertIterToGen and convertIterToChan is that the constructor of the returned iterator is a native generator object in the first case, and this library’s channel class in the second.

from repeater.

elderapo avatar elderapo commented on May 29, 2024

Async generators do not have utilities for manipulating them ex.: filtering, mapping, combining/merging, etc. I believe filtering/mapping can be easily done but more complicated scenarios of merging are much easier with channels.

from repeater.

brainkim avatar brainkim commented on May 29, 2024

Async generators do not have utilities for manipulating them ex.: filtering, mapping, combining/merging, etc. I believe filtering/mapping can be easily done but more complicated scenarios of merging are much easier with channels.

Channels will never have utilities for filtering/mapping unless async generators gain these methods as well. Additionally, the channel combinator functions make a conscious decision to only take AsyncIterable values as parameters; they never require their arguments to be channels. Typically, everything you need for filtering, mapping, etc. is already exposed via the AsyncIterable interface. In your above example, I’m not sure why or how intermediate channels would be used to implement map/filter, and I guarantee those things can be done with async generators (and you can create a fluent chainable API with a little bit of extra work).

I think there is definitely a need for a library which does what you’re saying, and it would be even better if it were both typed with TypeScript and polymorphically sync/async according to whether the arguments passed to it are Iterable or AsyncIterable. I encourage you to experiment with this stuff and let me know what you discover!

from repeater.

brainkim avatar brainkim commented on May 29, 2024

Reopening this cuz I plan on moving the combinator static method to a separate module and adding combinators.

from repeater.

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.