Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Array like helper functions (combinator module) #26

Open
elderapo opened this issue Aug 12, 2019 · 8 comments
Open

Array like helper functions (combinator module) #26

elderapo opened this issue Aug 12, 2019 · 8 comments
Labels
enhancement New feature or request

Comments

@elderapo
Copy link

I think it would be handy to have array-like helper functions in the core or utils/helpers package.

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

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

const stringOddNumbersChannel = mapChannel(oddNumbersChannel, item => item.toString());

Can't think of about any other from the top of my head. These should probably be functions and not instance level methods so the API doesn't differ from async iterators.

@brainkim brainkim added enhancement New feature or request good first issue Good for newcomers labels Aug 12, 2019
@brainkim
Copy link
Member

brainkim commented Aug 12, 2019

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! 🥰

@brainkim brainkim removed the good first issue Good for newcomers label Aug 12, 2019
@elderapo
Copy link
Author

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.

@brainkim
Copy link
Member

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!

@elderapo
Copy link
Author

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.

@brainkim
Copy link
Member

brainkim commented Aug 12, 2019

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.

@elderapo
Copy link
Author

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.

@brainkim
Copy link
Member

brainkim commented Aug 14, 2019

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!

@brainkim brainkim added the wontfix This will not be worked on label Aug 14, 2019
@brainkim brainkim reopened this Aug 25, 2020
@brainkim brainkim changed the title Array like helper functions Array like helper functions (combinator module) Aug 25, 2020
@brainkim
Copy link
Member

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

@brainkim brainkim removed the wontfix This will not be worked on label Aug 26, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants