An empty card template is suitable even for non-empty cards

The problem I'm trying to solve is

Write a map_search_pred (Map, Pred) function that returns the first {Key, Value} element in the map for which Pred (Key, Value) is true.

My attempt looks like

map_search_pred(#{}, _) -> {}; map_search_pred(Map, Pred) -> [H|_] = [{Key, Value} || {Key, Value} <- maps:to_list(Map), Pred(Key, Value) =:= true], H. 

When I run this, I see the output as

 1> lib_misc:map_search_pred(#{1 => 1, 2 => 3}, fun(X, Y) -> X =:= Y end). {} 2> lib_misc:map_search_pred(#{1 => 1, 2 => 3}, fun(X, Y) -> X =:= Y end). {} 3> maps:size(#{}). 0 4> 

How am I sure? I pulled out the first sentence to make it look like

 map_search_pred(Map, Pred) -> [H|_] = [{Key, Value} || {Key, Value} <- maps:to_list(Map), Pred(Key, Value) =:= true], H. 

and run again

 1> lib_misc:map_search_pred(#{1 => 1, 2 => 3}, fun(X, Y) -> X =:= Y end). {1,1} 2> lib_misc:map_search_pred(#{}, fun(X, Y) -> X =:= Y end). ** exception error: no match of right hand side value [] in function lib_misc:map_search_pred/2 (/Users/harith/code/IdeaProjects/others/erlang/programmingErlang/src/lib_misc.erl, line 42) 3> 
+5
source share
1 answer

According to the documentation card :

Matching an expression with an empty map literal will match its type, but no variables will be bound:

# {} = Expr

This expression will match if the Expr expression is of type map, otherwise it will fail with the exception of badmatch.

However, erlang: map_size can be used instead:

 map_search_pred(Map, _) when map_size(Map) == 0 -> {}; map_search_pred(Map, Pred) -> [H|_] = [{Key, Value} || {Key, Value} <- maps:to_list(Map), Pred(Key, Value) =:= true], H. 
+7
source

Source: https://habr.com/ru/post/1210608/


All Articles