[erlang-patches] Add lists:init/1 - got tired of implementing it

Anton Fedorov <>
Thu Apr 25 18:05:30 CEST 2013


Hmm... if add new functions into lists, think about this one too:

%% --------------------------------------------
%% @spec lists_first(Pred,List) -> {Element, List}
%%
%% @doc Pick first element from List, for which Pred return true.
%% @end ---------------------------------------
lists_first(_Pred,[]) ->
    undefined;
lists_first(Pred,[Element|Rest]) ->
    case Pred(Element) of
        true -> Element;
        _ -> lists_first(Pred, Rest)
    end.

%% --------------------------------------------
%% @spec lists_nth(Pred,N,List) -> {Element, List}
%%
%% @doc Pick Nth element from List, for which Pred return true.
%% @end ---------------------------------------
lists_nth(_Pred,_N,[]) ->
    undefined;
lists_nth(Pred,N,[Element|Rest]) ->
    case Pred(Element) of
        true -> case N of
                    0 -> Element;
                    _ -> lists_nth(Pred, N-1, Rest)
        end;
        _ -> lists_nth(Pred, N, Rest)
    end.

also have tired of copying in several projects.

В письме от Чтв, 25 Апр 2013, 22:54 Fred Hebert пишет:
> Sounds to me like it would have been better implemented as something like:
>
> init([_]) -> [];
> init([H|T]) -> [H|init(T)].
>
> As this would avoid re-building the list twice, once on each reversal.
>
> Regards,
> Fred.
>
>
> On Thu, Apr 25, 2013 at 11:12 AM, Hans Svensson <> wrote:
>
>> Hi,
>>
>> Yesterday I implemented, for the umpteenth time, the init-function. (I
>> guess being taught Haskell at one point could be blamed partly for the
>> coding style requiring this function...) It is a trivial function that I
>> think should be part of the standard lists module. This patch adds the
>> function, tests in lists_SUITE, and documentation of the function.
>>
>> The implementation is trivial: reverse(tl(reverse(List))).
>>
>> If I've missed some religious reason for not having this function feel
>> free to drop the patch ;-)
>>
>> Cheers,
>> Hans
>>
>> git fetch
>> git://github.com/hanssv/otp.**git<http://github.com/hanssv/otp.git>add_init_to_lists
>>
>> https://github.com/hanssv/otp/**compare/erlang:maint...add_**init_to_lists<https://github.com/hanssv/otp/compare/erlang:maint...add_init_to_lists>
>> https://github.com/hanssv/otp/**compare/erlang:maint...add_**
>> init_to_lists.patch<https://github.com/hanssv/otp/compare/erlang:maint...add_init_to_lists.patch>
>> ______________________________**_________________
>> erlang-patches mailing list
>> 
>> http://erlang.org/mailman/**listinfo/erlang-patches<http://erlang.org/mailman/listinfo/erlang-patches>
>>
> _______________________________________________
> erlang-patches mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-patches
>


-- 
Regards,
Anton Fedorov
Call2ru service
E-Mail: 
Jabber: 
Skype: datacompboy
ICQ: 272-35-262
Mobile: +7-913-925-7974 [SMS 24h, Call 05:00-19:00 MSKT (GMT+3)]



More information about the erlang-patches mailing list