[erlang-questions] How to return first {Key, Value} from Map where Predicate is true

Joe Armstrong <>
Thu Jan 8 11:42:26 CET 2015


Reply in

http://joearms.github.io/2015/01/08/Some_Performance-Measurements-On-Maps.html

/Joe


On Thu, Jan 8, 2015 at 8:47 AM, Ulf Wiger <> wrote:
>
> On 08 Jan 2015, at 04:56, Harit Himanshu <>
> wrote:
>
> My attempt, looks like
>
> map_search_pred(Map, Pred)  ->
>   M = [{Key, Value} || {Key, Value} <- maps:to_list(Map), Pred(Key, Value)
> =:= true],
>   case length(M) of
>     0 -> {};
>     _ -> lists:nth(1, M)
>   end.
>
> ...
>
> Problem?
> The problem is efficiency.
> It uses list comprehension and runs for all the elements in the list. The
> better solution would be to return after the first match.
>
> As a beginner to language, I do not know a better idiomatic way to solve
> this, so looking for better ideas and suggestions
>
>
> You can use
>
> map_search_pred(Map, Pred) ->
>     case lists:dropwhile(fun(X) -> not Pred(X) end, maps:to_list(Map)) of
>         [Found | _] -> Found;
>         [] -> false
>     end.
>
> A few comments.
>
> - Using {} as a return value denoting “not found” is unusual. My version
> mimicks lists:keyfind/2, which returns the matching object or ‘false’.
>
> - Testing the length of the list using length/1 will force a count of all
> elements. In this case, you are only interested in checking if the list is
> non-empty, which is better done with a pattern-match. This also eliminates
> the need for calling lists:nth(1,M), which is also a more expensive option
> than hd(M).
>
> BR,
> Ulf W
>
> Ulf Wiger, Co-founder & Developer Advocate, Feuerlabs Inc.
> http://feuerlabs.com
>
>
>
>
> _______________________________________________
> erlang-questions mailing list
> 
> http://erlang.org/mailman/listinfo/erlang-questions
>


More information about the erlang-questions mailing list