parts<T> static method

List<List<T>> parts<T>(
  1. List<T> colls,
  2. int parts
)

Part array

Implementation

static List<List<T>> parts<T>(List<T> colls, int parts) {
  if (colls.isEmpty) {
    return List.empty();
  }
  if (parts <= 0) {
    throw ArgumentError('the parts length must > 0');
  }
  if (colls.length <= parts) {
    return [colls];
  }
  List<List<T>> rets = [];
  List<T> items = [];

  var pnum = (colls.length / parts).floor();
  for (var coll in colls) {
    items.add(coll);
    if (items.length == pnum) {
      rets.add(items);
      if (rets.length < parts) {
        items = [];
      }
    }
  }
  return rets;
}