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(
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>