Is there a transpose function in Elixir?

前端 未结 3 920
鱼传尺愫
鱼传尺愫 2020-12-19 01:03

Hi I look for a transpose function in Elixir. For example I have this kind of array a and after calling a function the result should be b:

3条回答
  •  一整个雨季
    2020-12-19 01:36

    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 m is your matrix.

提交回复
热议问题