erlang各种有用的函数包括一些有用nif封装,还有一些性能测试case。
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符

61 行
1.5 KiB

2 年前
2 年前
  1. -module(utMapsFold).
  2. -export([fold/1, diff/2, diffKeys/2]).
  3. fold(Map) ->
  4. fold(maps:iterator(Map), []).
  5. fold(Iterator, AccList) ->
  6. case maps:next(Iterator) of
  7. {Key, Value, NextIter} ->
  8. fold(NextIter, [{Key, Value} | AccList]);
  9. none ->
  10. AccList
  11. end.
  12. diff(Map1, Map2) ->
  13. case map_size(Map1) =< map_size(Map2) of
  14. true ->
  15. diff_1(maps:next(maps:iterator(Map1)), Map1, Map2);
  16. _ ->
  17. diff_2(maps:next(maps:iterator(Map2)), Map1)
  18. end.
  19. diff_1(none, Map1, _Map2) ->
  20. Map1;
  21. diff_1({Key, _Value, NextIter}, Map1, Map2) ->
  22. case maps:is_key(Key, Map2) of
  23. true ->
  24. diff_1(maps:next(NextIter), maps:remove(Key, Map1), Map2);
  25. _ ->
  26. diff_1(maps:next(NextIter), Map1, Map2)
  27. end.
  28. diff_2(none, Map1) ->
  29. Map1;
  30. diff_2({Key, _Value, NextIter}, Map1) ->
  31. diff_2(maps:next(NextIter), maps:remove(Key, Map1)).
  32. diffKeys(Map1, Map2) ->
  33. case map_size(Map1) =< map_size(Map2) of
  34. true ->
  35. diffKeys_1(maps:next(maps:iterator(Map1)), Map2, []);
  36. _ ->
  37. diffKeys_2(maps:next(maps:iterator(Map2)), Map1)
  38. end.
  39. diffKeys_1(none, _Map2, Acc) ->
  40. Acc;
  41. diffKeys_1({Key, _Value, NextIter}, Map2, Acc) ->
  42. case maps:is_key(Key, Map2) of
  43. true ->
  44. diffKeys_1(maps:next(NextIter), Map2, Acc);
  45. _ ->
  46. diffKeys_1(maps:next(NextIter), Map2, [Key | Acc])
  47. end.
  48. diffKeys_2(none, Map1) ->
  49. maps:to_list(Map1);
  50. diffKeys_2({Key, _Value, NextIter}, Map1) ->
  51. diffKeys_2(maps:next(NextIter), maps:remove(Key, Map1)).