The current (Release 17.x) implementation of maps are deliberately kept simple. This has some severe performance problems, but it maximizes implementation flexibility. I think it is a good approach to the problem.<div><br></div><div>* Make it right</div><div>* Make it beautiful</div><div>* Make it fast</div><div><br></div><div>Of course, having a defined function for this:</div><div><br></div><div>-spec maps:find(fun ((B) -> boolean()), map(A, B)) -> {ok, {A, B}} | not_found.<br><br>Could be useful in the long run. But this is an iterative process that moves ahead one release every year :)<br><br><div class="gmail_quote">On Thu Jan 08 2015 at 11:42:37 AM Joe Armstrong <<a href="mailto:erlang@gmail.com">erlang@gmail.com</a>> 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/<u></u>01/08/Some_Performance-<u></u>Measurements-On-Maps.html</a><br>
<br>
/Joe<br>
<br>
<br>
On Thu, Jan 8, 2015 at 8:47 AM, Ulf Wiger <<a href="mailto:ulf@feuerlabs.com" target="_blank">ulf@feuerlabs.com</a>> wrote:<br>
><br>
> On 08 Jan 2015, at 04:56, Harit Himanshu <<a href="mailto:harit.subscriptions@gmail.com" target="_blank">harit.subscriptions@gmail.com</a><u></u>><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>
> ______________________________<u></u>_________________<br>
> erlang-questions mailing list<br>
> <a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a><br>
> <a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/<u></u>listinfo/erlang-questions</a><br>
><br>
______________________________<u></u>_________________<br>
erlang-questions mailing list<br>
<a href="mailto:erlang-questions@erlang.org" target="_blank">erlang-questions@erlang.org</a><br>
<a href="http://erlang.org/mailman/listinfo/erlang-questions" target="_blank">http://erlang.org/mailman/<u></u>listinfo/erlang-questions</a><br>
</blockquote></div></div>