[erlang-questions] list comprehensions speed

Erik Søe Sørensen eriksoe@REDACTED
Tue Feb 25 15:40:31 CET 2014


Others have given good answers.
For completeness, I thought I'd mention this:
Your example
[ Value || {Name, Value} <- A, string:equal(Name, "name3")].
could be simplified to:
[ Value || {"name3", Value} <- A].

Note that both result in a list.

Huh. Question to the list: I realize that I don't know how string:equal()
differs from using '=:='... The docs don't tell explicitly. Is there a
difference?
Den 25/02/2014 15.13 skrev "Oleg" <lego12239@REDACTED>:

>   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
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20140225/6b1ffcc2/attachment.htm>


More information about the erlang-questions mailing list