@@ -1926,26 +1926,114 @@ defmodule Kernel do
19261926 end
19271927
19281928 @doc """
1929- Define setelem to set Tuple element according to Elixir conventions
1929+ Define set_elem to set Tuple element according to Elixir conventions
19301930 (i.e. it expects the tuple as first argument, zero-index based).
19311931
1932- It is implemented as a macro so it can be used in guards.
1933-
19341932 ## Example
19351933
19361934 iex> tuple = { :foo, :bar, 3 }
1937- ...> setelem (tuple, 0, :baz)
1935+ ...> set_elem (tuple, 0, :baz)
19381936 { :baz, :bar, 3 }
19391937
19401938 """
1941- defmacro setelem (tuple, index, value) when is_integer(index) do
1939+ defmacro set_elem (tuple, index, value) when is_integer(index) do
19421940 quote do: :erlang.setelement(unquote(index + 1), unquote(tuple), unquote(value))
19431941 end
19441942
1943+ defmacro set_elem(tuple, index, value) do
1944+ quote do: :erlang.setelement(unquote(index) + 1, unquote(tuple), unquote(value))
1945+ end
1946+
1947+ @doc false
19451948 defmacro setelem(tuple, index, value) do
1949+ IO.puts "setelem is deprecated, please use set_elem instead\n #{ Exception . format_stacktrace ( __CALLER__ . stacktrace ) } "
19461950 quote do: :erlang.setelement(unquote(index) + 1, unquote(tuple), unquote(value))
19471951 end
19481952
1953+ @doc """
1954+ Define insert_elem to insert element into a tuple according to
1955+ Elixir conventions (i.e. it expects the tuple as first argument,
1956+ zero-index based).
1957+
1958+ Please note that in versions of Erlang prior to R16B there is no BIF
1959+ for this operation and it is emulated by converting the tuple to a list
1960+ and back and is, therefore, inefficient.
1961+
1962+ ## Example
1963+
1964+ iex> tuple = { :bar, :baz }
1965+ ...> insert_elem(tuple, 0, :foo)
1966+ { :foo, :bar, :baz }
1967+ """
1968+ defmacro insert_elem(tuple, index, value) when is_integer(index) do
1969+ case :proplists.get_value(:insert_element,
1970+ :proplists.get_value(:exports, :erlang.module_info,[])) do
1971+ 3 ->
1972+ quote do: :erlang.insert_element(unquote(index + 1), unquote(tuple), unquote(value))
1973+ :undefined ->
1974+ do_insert_elem(tuple, index, value)
1975+ end
1976+ end
1977+ defmacro insert_elem(tuple, index, value) do
1978+ case :proplists.get_value(:insert_element,
1979+ :proplists.get_value(:exports, :erlang.module_info,[])) do
1980+ 3 ->
1981+ quote do: :erlang.insert_element(unquote(index) + 1, unquote(tuple), unquote(value))
1982+ :undefined ->
1983+ do_insert_elem(tuple, index, value)
1984+ end
1985+ end
1986+
1987+ defp do_insert_elem(tuple, index, value) do
1988+ quote do
1989+ {h, t} = :lists.split(unquote(index),
1990+ tuple_to_list(unquote(tuple)))
1991+ list_to_tuple(h ++ [unquote(value)|t])
1992+ end
1993+ end
1994+
1995+ @doc """
1996+ Define delete_elem to delete element from a tuple according to
1997+ Elixir conventions (i.e. it expects the tuple as first argument,
1998+ zero-index based).
1999+
2000+ Please note that in versions of Erlang prior to R16B there is no BIF
2001+ for this operation and it is emulated by converting the tuple to a list
2002+ and back and is, therefore, inefficient.
2003+
2004+ ## Example
2005+
2006+ iex> tuple = { :foo, :bar, :baz }
2007+ ...> delete_elem(tuple, 0)
2008+ { :bar, :baz }
2009+ """
2010+ defmacro delete_elem(tuple, index) when is_integer(index) do
2011+ case :proplists.get_value(:delete_element,
2012+ :proplists.get_value(:exports, :erlang.module_info,[])) do
2013+ 2 ->
2014+ quote do: :erlang.delete_element(unquote(index + 1), unquote(tuple))
2015+ :undefined ->
2016+ do_delete_elem(tuple, index)
2017+ end
2018+ end
2019+ defmacro delete_elem(tuple, index) do
2020+ case :proplists.get_value(:delete_element,
2021+ :proplists.get_value(:exports, :erlang.module_info,[])) do
2022+ 2 ->
2023+ quote do: :erlang.delete_element(unquote(index) + 1, unquote(tuple))
2024+ :undefined ->
2025+ do_delete_elem(tuple, index)
2026+ end
2027+ end
2028+
2029+ defp do_delete_elem(tuple, index) do
2030+ quote do
2031+ {h, [_|t]} = :lists.split(unquote(index),
2032+ tuple_to_list(unquote(tuple)))
2033+ list_to_tuple(h ++ t)
2034+ end
2035+ end
2036+
19492037 @doc """
19502038 Provides an integer division macro according to Erlang semantics.
19512039 Raises an error if one of the arguments is not an integer.
0 commit comments