[erlang-questions] [erlang-bugs] erlang:delete_element/2 missing

ok@REDACTED ok@REDACTED
Thu Feb 7 21:51:39 CET 2013


> I guess I'll do something like this.
>
> 4> Tupple = {one,two,three}.
> 5> lists:foldl(fun(C,Acc) when C/=2 ->
> erlang:append_element(Acc,erlang:element(C,Tupple)); (_,Acc) -> Acc
> end,{},[1,2,3]).
> {one,three}

Yike.  Hello O(N**2)!


delete_element(Index, Tuple)
 when is_tuple(Tuple),
      is_integer(Index),
      tuple_size(Tuple) >= Index, Index >= 1
 -> L = tuple_to_list(Tuple),
    {F,[_|B]} = lists:split(Index - 1, L),
    list_to_tuple(F ++ B).

At least that's O(N).  Even a BIF can't beat that
(although it can improve the constant factor).







More information about the erlang-questions mailing list