transpose<T> function

List<List<T>> transpose<T>(
  1. List<List<T>> matrix
)

Transposes the specified matrix of lists, truncating to the shortest row.

Implementation

List<List<T>> transpose<T>(List<List<T>> matrix) {
  final n = matrix.length;
  if (n == 0) return <List<T>>[];
  var m = matrix[0].length;
  for (var j = 1; j < n; ++j) {
    final len = matrix[j].length;
    if (len < m) m = len;
  }
  if (m == 0) return <List<T>>[];
  return List<List<T>>.generate(
    m,
    (i) => List<T>.generate(n, (j) => matrix[j][i]),
  );
}