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

Björn-Egil Dahlberg wallentin.dahlberg@REDACTED
Thu Jan 8 15:24:01 CET 2015


Thank you Jesper! =)

2015-01-08 14:00 GMT+01:00 Jesper Louis Andersen <
jesper.louis.andersen@REDACTED>:

> 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.
>
> * Make it right
> * Make it beautiful
> * Make it fast
>
> Of course, having a defined function for this:
>
> -spec maps:find(fun ((B) -> boolean()), map(A, B)) -> {ok, {A, B}} |
> not_found.
>
> Could be useful in the long run. But this is an iterative process that
> moves ahead one release every year :)
>
>
> On Thu Jan 08 2015 at 11:42:37 AM Joe Armstrong <erlang@REDACTED> wrote:
>
>> 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 <ulf@REDACTED> wrote:
>> >
>> > On 08 Jan 2015, at 04:56, Harit Himanshu <harit.subscriptions@REDACTED
>> >
>> > 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
>> > erlang-questions@REDACTED
>> > http://erlang.org/mailman/listinfo/erlang-questions
>> >
>> _______________________________________________
>> erlang-questions mailing list
>> erlang-questions@REDACTED
>> http://erlang.org/mailman/listinfo/erlang-questions
>>
>
> _______________________________________________
> 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/20150108/905c183e/attachment.htm>


More information about the erlang-questions mailing list