1 | import _curry1 from "./internal/_curry1.js";
|
---|
2 | /**
|
---|
3 | * Transposes the rows and columns of a 2D list.
|
---|
4 | * When passed a list of `n` lists of length `x`,
|
---|
5 | * returns a list of `x` lists of length `n`.
|
---|
6 | *
|
---|
7 | *
|
---|
8 | * @func
|
---|
9 | * @memberOf R
|
---|
10 | * @since v0.19.0
|
---|
11 | * @category List
|
---|
12 | * @sig [[a]] -> [[a]]
|
---|
13 | * @param {Array} list A 2D list
|
---|
14 | * @return {Array} A 2D list
|
---|
15 | * @example
|
---|
16 | *
|
---|
17 | * R.transpose([[1, 'a'], [2, 'b'], [3, 'c']]) //=> [[1, 2, 3], ['a', 'b', 'c']]
|
---|
18 | * R.transpose([[1, 2, 3], ['a', 'b', 'c']]) //=> [[1, 'a'], [2, 'b'], [3, 'c']]
|
---|
19 | *
|
---|
20 | * // If some of the rows are shorter than the following rows, their elements are skipped:
|
---|
21 | * R.transpose([[10, 11], [20], [], [30, 31, 32]]) //=> [[10, 20, 30], [11, 31], [32]]
|
---|
22 | * @symb R.transpose([[a], [b], [c]]) = [a, b, c]
|
---|
23 | * @symb R.transpose([[a, b], [c, d]]) = [[a, c], [b, d]]
|
---|
24 | * @symb R.transpose([[a, b], [c]]) = [[a, c], [b]]
|
---|
25 | */
|
---|
26 |
|
---|
27 | var transpose =
|
---|
28 | /*#__PURE__*/
|
---|
29 | _curry1(function transpose(outerlist) {
|
---|
30 | var i = 0;
|
---|
31 | var result = [];
|
---|
32 |
|
---|
33 | while (i < outerlist.length) {
|
---|
34 | var innerlist = outerlist[i];
|
---|
35 | var j = 0;
|
---|
36 |
|
---|
37 | while (j < innerlist.length) {
|
---|
38 | if (typeof result[j] === 'undefined') {
|
---|
39 | result[j] = [];
|
---|
40 | }
|
---|
41 |
|
---|
42 | result[j].push(innerlist[j]);
|
---|
43 | j += 1;
|
---|
44 | }
|
---|
45 |
|
---|
46 | i += 1;
|
---|
47 | }
|
---|
48 |
|
---|
49 | return result;
|
---|
50 | });
|
---|
51 |
|
---|
52 | export default transpose; |
---|