Elixir中有转置功能吗? [英] Is there a transpose function in Elixir?

查看:83
本文介绍了Elixir中有转置功能吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在Elixir中寻找转置函数。
例如,我有这种数组 a ,调用函数后,结果应为 b

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:

a = [[1, 2], [3, 4], [5, 6]]
b = transpose(a)
b => [[1, 3, 5], [2, 4, 6]]


推荐答案

Elixir中目前没有,但您可以使用以下命令创建自己的文件:

There isn't one in Elixir currently, but you could create your own with:

def transpose([]), do: []
def transpose([[]|_]), do: []
def transpose(a) do
  [Enum.map(a, &hd/1) | transpose(Enum.map(a, &tl/1))]
end

这篇关于Elixir中有转置功能吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆