Transpose
Syntax
$collection.transpose()
$collection.transpose($fun)
Description
Transposes a collection of lists by combining elements at corresponding positions.
The function accepts a collection, which can contain any number of lists, and combines their elements at the corresponding positions. If a transposition functionfunis provided, this is used to combine the elements. Otherwise, tuples of the combined elements are returned.
If the two lists have different lengths, the shorter list is filled with null values.
Parameters
| Parameter name | Type | Type Description | Mandatory | Default |
|---|---|---|---|---|
| Collection | Enumeration | A collection that contains any number of lists. The lists are combined element by element. If there is only one list, it is processed unchanged. Empty collections produce an empty result. | yes | |
| fun | Function |
An optional function that is used to combine the corresponding elements from the two lists. The function receives as many parameters as there are lists in the collection. If not specified, tuple lists are created. |
no |
Tuple creation |
Return value
Type: List
A list of the combined elements. If no transposition function has been specified, the list contains tuples in the form of lists. Each tuple list contains the elements at the corresponding position from the input lists. If a function has been specified, the list contains the results of this function for each element tuple. The length of the result corresponds to the longest input list.
Examples
Simple transposition without function
list(
list("A", "B", "C"),
list(1, 2, 3)
).transpose()
Output: A list of tuple objects: [["A", 1], ["B", 2], ["C", 3]]
Transposition with combination function
list(
list("Hallo", "Guten", "Auf"),
list("Welt", "Tag", "Wiedersehen"),
list(null, "!", "?")
).transpose(first -> second -> third -> $first + " " + $second + $third)
Output: A list of combined strings: [Hallo Welt, Guten Tag!, Auf Wiedersehen?]
Transposition with different list lengths
list(
list(10, 20, 30, 40),
list(1, 2, 3)
).transpose(a -> b -> if($b != null, $a + $b, $a))
Output: A list with sums: [11, 22, 33, 40]. The fourth element remains unchanged, as the second list is shorter.