<div dir="ltr">Huge thanks @joe for detailed analysis on that. That made me go through some of the documentation and read more. I believe find_first is definitely a better way.<div><br></div><div>When I said performance, I really meant traversing the complete list is un-necessary, early return is best way do do once {Key, Value} is found</div><div><br></div><div>Thanks again</div><div>+ Harit Himanshu</div></div><div class="gmail_extra"><br><div class="gmail_quote">On Thu, Jan 8, 2015 at 2:42 AM, Joe Armstrong <span dir="ltr"><<a href="mailto:erlang@gmail.com" target="_blank">erlang@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">Reply in<br>
<br>
<a href="http://joearms.github.io/2015/01/08/Some_Performance-Measurements-On-Maps.html" target="_blank">http://joearms.github.io/2015/01/08/Some_Performance-Measurements-On-Maps.html</a><br>
<span class="HOEnZb"><font color="#888888"><br>
/Joe<br>
</font></span><div class="HOEnZb"><div class="h5"><br>
<br>
On Thu, Jan 8, 2015 at 8:47 AM, Ulf Wiger <<a href="mailto:ulf@feuerlabs.com">ulf@feuerlabs.com</a>> wrote:<br>
><br>
> On 08 Jan 2015, at 04:56, Harit Himanshu <<a href="mailto:harit.subscriptions@gmail.com">harit.subscriptions@gmail.com</a>><br>
> wrote:<br>
><br>
> My attempt, looks like<br>
><br>
> map_search_pred(Map, Pred)  -><br>
>   M = [{Key, Value} || {Key, Value} <- maps:to_list(Map), Pred(Key, Value)<br>
> =:= true],<br>
>   case length(M) of<br>
>     0 -> {};<br>
>     _ -> lists:nth(1, M)<br>
>   end.<br>
><br>
> ...<br>
><br>
> Problem?<br>
> The problem is efficiency.<br>
> It uses list comprehension and runs for all the elements in the list. The<br>
> better solution would be to return after the first match.<br>
><br>
> As a beginner to language, I do not know a better idiomatic way to solve<br>
> this, so looking for better ideas and suggestions<br>
><br>
><br>
> You can use<br>
><br>
> map_search_pred(Map, Pred) -><br>
>     case lists:dropwhile(fun(X) -> not Pred(X) end, maps:to_list(Map)) of<br>
>         [Found | _] -> Found;<br>
>         [] -> false<br>
>     end.<br>
><br>
> A few comments.<br>
><br>
> - Using {} as a return value denoting “not found” is unusual. My version<br>
> mimicks lists:keyfind/2, which returns the matching object or ‘false’.<br>
><br>
> - Testing the length of the list using length/1 will force a count of all<br>
> elements. In this case, you are only interested in checking if the list is<br>
> non-empty, which is better done with a pattern-match. This also eliminates<br>
> the need for calling lists:nth(1,M), which is also a more expensive option<br>
> than hd(M).<br>
><br>
> BR,<br>
> Ulf W<br>
><br>
> Ulf Wiger, Co-founder & Developer Advocate, Feuerlabs Inc.<br>
> <a href="http://feuerlabs.com" target="_blank">http://feuerlabs.com</a><br>
><br>
><br>
><br>
><br>
</div></div><div class="HOEnZb"><div class="h5">> _______________________________________________<br>
> erlang-questions mailing list<br>
> <a href="mailto:erlang-questions@erlang.org">erlang-questions@erlang.org</a><br>
> <a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/listinfo/erlang-questions</a><br>
><br>
</div></div></blockquote></div><br></div>