编写一个函数来找出所有不带歧义的函数名,也就是 那些只在一个模块里出现过的函数名(erlang)

  erlang程序设计第八章练习题第二题:

  code:all_loaded()命令会返回一个由{Mod,File}对构成的列表,内含所有Erlang系统

载入的模块。使用内置函数Mod:module_info()了解这些模块。编写一些函数来找出哪个模块
导出的函数最多,以及哪个函数名最常见。编写一个函数来找出所有不带歧义的函数名,也就是
那些只在一个模块里出现过的函数名。

  这里主要有三个问题:

  1、哪个模块导出的函数最多

  2、哪个函数名最常见

  3、哪些函数只在一个模块出现

  module_fun:find_most、find_pop、find_alone分别实现了上述问题.

  

  1 -module (module_fun).
  2 -export([find_most/0, find_pop/0, find_alone/0]).
  3 
  4 %%查找加载的模块
  5 find_modules() ->
  6   [Module_Name || {Module_Name, _} <- code:all_loaded()].
  7 
  8 %%查找export最多的模块
  9 find_most() ->
 10   find_most(find_modules())
 11   .
 12 find_most([]) -> {};
 13 find_most([H | T]) ->  find_most(T, {H,length(find_export(H))}).
 14 find_most([H | T], {_, Sum} = M) ->
 15   Sum1 = length(find_export(H)),
 16   if
 17     Sum1 > Sum ->  find_most(T, {H, Sum1});
 18     true -> find_most(T, M)
 19   end
 20 ;
 21 find_most([], M) -> M.
 22 
 23 %%查找哪个函数最常见
 24 find_pop() ->
 25   Map = find_pop_map(find_all_export()),
 26   find_pop_max(Map)
 27 .
 28 
 29 find_pop_map([]) -> #{};
 30 find_pop_map([{Name, _} | T]) ->
 31   Map = find_pop_map(T),
 32   maps:put(Name,case maps:is_key(Name, Map) of
 33                   true -> maps:get(Name, Map);
 34                   false -> 0
 35                 end + 1, Map)
 36 .
 37 
 38 find_pop_max(Map) ->
 39   find_pop_max(maps:keys(Map), Map).
 40 
 41 find_pop_max([], _) -> {none, -1};
 42 find_pop_max([H | T], Map) ->
 43   Count = maps:get(H, Map),
 44   Max = {_, Count1} = find_pop_max(T, Map),
 45   if
 46     Count > Count1 -> {H, Count};
 47     true -> Max
 48   end.
 49 
 50 
 51 %%查找只在一个模块里出现的函数
 52 find_alone() ->
 53   Module_Names = find_modules(),
 54   L = [ {F, Module_Name} || Module_Name <- Module_Names, F <- find_fun_name(Module_Name)],
 55   find_alone(L, L)
 56 .
 57 
 58 find_alone_ex(_, []) -> false;
 59 find_alone_ex({Name, M} = P, [{Name1, M1} | T] ) ->
 60   if
 61     M =:= M1 -> find_alone_ex(P, T);
 62     Name =:= Name1 -> true;
 63     true -> find_alone_ex(P, T)
 64   end
 65 .
 66 
 67 find_alone([], _) -> #{};
 68 find_alone([{Name, M} = H | T], L) ->
 69   Map = find_alone(T, L),
 70   case find_alone_ex(H, L) of
 71       true -> Map;
 72       false ->
 73         Array = case maps:is_key(M, Map) of
 74            true -> maps:get(M, Map);
 75            false -> []
 76         end,
 77         maps:put(M, [Name | Array], Map)
 78   end.
 79 
 80 
 81 
 82 find_all_export() ->
 83   Module_Names = find_modules(),
 84   [ F || Module_Name <- Module_Names, F <- find_export(Module_Name)].
 85 
 86 
 87 %%模块export的函数
 88 find_export(Module_Name) ->
 89   [{Fun_Name, Fun_Int} ||
 90     {exports, Fun_Arr} <- apply(Module_Name, module_info, [])
 91     , {Fun_Name, Fun_Int} <- Fun_Arr
 92     , Fun_Name =/= module_info
 93   ].
 94 
 95 find_fun_name(Module_Name) ->
 96   filter_fun(find_export(Module_Name)).
 97 
 98 filter_fun([]) ->
 99   [];
100 filter_fun([{Name, _} | T]) ->
101   [Name | filter_fun(lists:filter(fun({Name1, _}) -> Name =/= Name1 end, T))]
102 .

 

 

posted @ 2019-05-08 11:16  风的低吟  阅读(269)  评论(0编辑  收藏  举报