@@ -1926,26 +1926,114 @@ defmodule Kernel do
1926
1926
end
1927
1927
1928
1928
@doc """
1929
- Define setelem to set Tuple element according to Elixir conventions
1929
+ Define set_elem to set Tuple element according to Elixir conventions
1930
1930
(i.e. it expects the tuple as first argument, zero-index based).
1931
1931
1932
- It is implemented as a macro so it can be used in guards.
1933
-
1934
1932
## Example
1935
1933
1936
1934
iex> tuple = { :foo, :bar, 3 }
1937
- ...> setelem (tuple, 0, :baz)
1935
+ ...> set_elem (tuple, 0, :baz)
1938
1936
{ :baz, :bar, 3 }
1939
1937
1940
1938
"""
1941
- defmacro setelem (tuple, index, value) when is_integer(index) do
1939
+ defmacro set_elem (tuple, index, value) when is_integer(index) do
1942
1940
quote do: :erlang.setelement(unquote(index + 1), unquote(tuple), unquote(value))
1943
1941
end
1944
1942
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
1945
1948
defmacro setelem(tuple, index, value) do
1949
+ IO.puts "setelem is deprecated, please use set_elem instead\n #{ Exception . format_stacktrace ( __CALLER__ . stacktrace ) } "
1946
1950
quote do: :erlang.setelement(unquote(index) + 1, unquote(tuple), unquote(value))
1947
1951
end
1948
1952
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
+
1949
2037
@doc """
1950
2038
Provides an integer division macro according to Erlang semantics.
1951
2039
Raises an error if one of the arguments is not an integer.
0 commit comments