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

New IterTools Functionality Discussion #50

Open
23 tasks
Smoren opened this issue Mar 5, 2024 · 2 comments
Open
23 tasks

New IterTools Functionality Discussion #50

Smoren opened this issue Mar 5, 2024 · 2 comments
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed

Comments

@Smoren Smoren added enhancement New feature or request help wanted Extra attention is needed good first issue Good for newcomers labels Mar 5, 2024
@zveroboy
Copy link

zveroboy commented Apr 5, 2024

@Smoren First of all, thank you for a wonderful library.

I guess it's pretty hard to implement a FP version of it where functions are curried and parameters are inversed using TS, right?

An example:

const takeWhile = _.curry(function* takeWhile(predicateFn, iterable) {
  for (const item of iterable) {
    if (predicateFn(item)) {
      yield item;
    } else {
      break;
    }
  }
});

const limitAsync = _.curry(async function* limitAsync(count, iterable) {
  if (count < 0) {
    throw new TypeError(`Limit must be ≥ 0. Got ${count}`);
  }

  let i = 0;
  for await (const datum of iterable) {
    if (i >= count) {
      return;
    }
    yield datum;
    ++i;
  }
});

// …
const asyncIterable = httpClient.fetchSomePaginatedDataIterable();

const fetch100ItemsOrLess = _.compose(
  toArrayAsync,
  limitAsync(100),
  flatMapAsync(response => response.data),
);

const result = await fetch100ItemsOrLess(asyncIterable);

@Smoren
Copy link
Owner Author

Smoren commented Apr 6, 2024

Hi @zveroboy! Thank you for your feedback!
I think you can use streams for such tasks.

import { AsyncStream } from 'itertools-ts';

const fetch100ItemsOrLess = async (response) => await AsyncStream.of(response)
  .flatMap(response => response.data)
  .limit(100)
  .toArray();

const result = await fetch100ItemsOrLess(asyncIterable);

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request good first issue Good for newcomers help wanted Extra attention is needed
Projects
None yet
Development

No branches or pull requests

2 participants