Skip to content

Instantly share code, notes, and snippets.

@pppillai
Created May 13, 2020 17:37
Show Gist options
  • Save pppillai/ae6d472c7790417dc1facacab2cc409e to your computer and use it in GitHub Desktop.
Save pppillai/ae6d472c7790417dc1facacab2cc409e to your computer and use it in GitHub Desktop.
-module('chapter-5').
-export([map_search_pred/2]).
% write a funciton map_search(Map, Pred) that returns the first element {Key, Value}
% in the map for which Pred(Key, Value) is true.
% 16> A = fun(X, Y) -> case X > Y of true -> true; _ -> false end end.
% #Fun<erl_eval.13.126501267>
% 17> A(1, 2).
% false
% 18> 'chapter-5':map_search_pred(#{1=>2, 4=>3}, A).
% {4,3}
% 19>
map_search_pred(Map, Pred) ->
map_search(maps:to_list(Map), Pred).
map_search([], _Pred) ->
[];
map_search([H|T], Pred) ->
{Key, Value} = H,
case Pred(Key, Value) of
true -> {Key, Value};
_ -> map_search(T, Pred)
end.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment