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