Hi, I am looking for a transpose function in Elixir. For example, I have such an array a, and after calling the function, the result should be b:
a
b
a = [[1, 2], [3, 4], [5, 6]] b = transpose(a) b => [[1, 3, 5], [2, 4, 6]]
There is no one at Elixir right now, but you can create your own:
def transpose([]), do: [] def transpose([[]|_]), do: [] def transpose(a) do [Enum.map(a, &hd/1) | transpose(Enum.map(a, &tl/1))] end
There (still) is not in Elixir, but you can use:
def transpose(rows) do rows |> List.zip |> Enum.map(&Tuple.to_list/1) end
Here's a slightly different solution:
def transpose(m) where length(m) <2, do: m def transpose(m) do for i <- 0..length(m)-1 do Enum.reduce(m,[], fn x,acc -> acc ++ [Enum.at(x,i)] end) end end
where mis your matrix.
m
Source: https://habr.com/ru/post/1540911/More articles:Different deadlines for individual runs - c ++Add custom cordova plugin to IBM Worklight 6.1 - pluginsHow to access environment variables on a JSP page - javaReaches values ββbetween two jsp with jstl - javaAutomatic parameterized constructor selection when deserializing JsonCovertor - jsonGit commit was stopped before completion - gitgetting environment variable value in java? - javaTrying to use std :: add_const to turn T & into const T & - c ++access to inputs created in renderUI in Shiny - dynamicFlowering three. js - blur? - three.jsAll Articles