split method

Iterable<Iterable<T>> split(
  1. int length
)

Iterable is divided into length elements each, creating a list of lists.

Iterablelength個の要素ごとに分割し、リストのリストを作成します。

final array = [1, 2, 3, 4, 5, 6, 7, 8];
final splitted = array.split(2); // [[1, 2], [3, 4], [5, 6], [7, 8]]

Implementation

Iterable<Iterable<T>> split(int length) {
  length = length.limit(0, this.length);
  List<T>? tmp;
  final res = <Iterable<T>>[];
  if (length == 0) {
    return res;
  }
  int i = 0;
  for (final item in this) {
    if (i % length == 0) {
      if (tmp != null) {
        res.add(tmp);
      }
      tmp = [];
    }
    tmp?.add(item);
    i++;
  }
  if (tmp != null) {
    res.add(tmp);
  }
  return res;
}