Erlang:“prepending”元组元素 [英] Erlang: "prepending" an element to a tuple

查看:145
本文介绍了Erlang:“prepending”元组元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  prepend(X,Tuple) - > 
list_to_tuple([X | tuple_to_list(Tuple)])。


解决方案

由于前置元素与将其插入位置1,您可以使用内置功能 erlang:insert_element / 3

 > erlang:insert_element(1,{a,b},z)。 
{z,a,b}

此功能已添加到Erlang / OTP R16A中。


Is it possible to write a faster equivalent to this function?

prepend(X, Tuple) ->
  list_to_tuple([X | tuple_to_list(Tuple)]).

解决方案

As prepending an element is the same as inserting it at position 1, you can use the built-in function erlang:insert_element/3:

> erlang:insert_element(1, {a, b}, z).
{z,a,b}

This function was added in Erlang/OTP R16A.

这篇关于Erlang:“prepending”元组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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