[erlang-questions] list comprehensions speed

Sean Cribbs sean@REDACTED
Tue Feb 25 15:28:58 CET 2014


I would do neither of those, there's a BIF just for this purpose:

lists:keyfind("name3", 1, A)

One could also use the slower, but perhaps easier to understand proplists
(lists:keyfind/3 is more general):

proplists:get_value("name3", A)

If you are not reading the keys for those pairs from the outside world (a
file, the network, etc), I'd use atoms instead of strings (just remove the
quotes).


On Tue, Feb 25, 2014 at 8:08 AM, Oleg <lego12239@REDACTED> wrote:

>   Hello.
>
> I'm erlang newbie. Don't beat me, please :-), if this question is obvious.
>
> I have a list of key-value pairs:
>
> A=[{"name1", 1}, {"name2", 77}, {"name3", 33}, {"name4", 234}].
>
> What is faster:
>
> [ Value || {Name, Value} <- A, string:equal(Name, "name3")].
>
> Or:
>
> get_value(Key, []) ->
>   [].
> get_value(Key, [H|T]) ->
>   {Name, Value} = H,
>   case string:equal(Name, Key) of
>     true ->
>       [Value];
>     false ->
>       get_value(Key, T)
>   end.
>
> start() ->
>   get_value("name3", A).
> _______________________________________________
> erlang-questions mailing list
> erlang-questions@REDACTED
> http://erlang.org/mailman/listinfo/erlang-questions
>



-- 
Sean Cribbs <sean@REDACTED>
Software Engineer
Basho Technologies, Inc.
http://basho.com/
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20140225/ade9ba41/attachment.htm>


More information about the erlang-questions mailing list