Empty Map Pattern 即使对于非空 Map 也匹配

Empty Map Pattern matches even for non-empty Map

我试图解决的问题状态

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

我的尝试看起来像

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

当我运行它时,我看到输出为

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> 

我怎么这么确定? 我拉出第一个子句,所以它看起来像

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

然后再次运行

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>

根据map documentation

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

#{} = Expr

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

但是可以使用 erlang:map_size 代替:

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.