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

split into chunks #20

Open
vincevargadev opened this issue May 21, 2024 · 0 comments
Open

split into chunks #20

vincevargadev opened this issue May 21, 2024 · 0 comments

Comments

@vincevargadev
Copy link
Member

extension SplitInto<T> on Iterable<T> {
  Iterable<Iterable<T>> splitInto(int n) sync* {
    final it = _sizes(length, n).iterator;
    for (int i = 0, a = 0, size = 0; i < n; i++, a += size) {
      size = (it..moveNext()).current;
      yield skip(a).take(size);
    }
  }
}

Iterable<int> _sizes(int length, int n) sync* {
  final largeCount = (length % n);
  final size = length ~/ n;
  for (var i = 0; i < n; i++) {
    yield i < largeCount ? size + 1 : size;
  }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant