[erlang-questions] How can I make a list of this
Richard A. O'Keefe
ok@REDACTED
Thu Aug 20 07:04:51 CEST 2015
On 19/08/2015, at 11:10 pm, Roelof Wobben <r.wobben@REDACTED> wrote:
>
> Still my question stands. How do I get a nice list of the ourcome of the exports of the functions ot more then 1 module.
> I now have a list of list of tuples where every list is the outcome of 1 module.
Step 1.
Module:module_info() ->
[ {exports, List_Of_Exports}
, {imports, List_Of_Imports}
, {attributes, List_Of_Attributes}
, {compile, Information_About_Compiler_Source_And_Options}
...
].
A little familiarity with the standard libraries suggests
that if f(Args) returns a list of {Key,Value} pairs then
f(Args,Key) might return just the Value you are interested
in. Play with it in the shell and discover that it is true.
Step 2.
Module:module_info(exports) ->
a list of {Name,Arity} pairs, just the exports we wanted.
Step 3.
You already know about list comprehension.
You may not realise that you can have more than one
generator (Pattern <- List) in a list comprehension
and that these operate as nested loops.
all_exports(Modules) ->
[ {Module,Name,Arity}
|| Module <- Modules,
{Name,Arity} <- Module:module_info(exports)
].
You could try this in the shell:
Modules = [orddict, proplists],
Triples = [ {Module,Name,Arity}
|| Module <- Modules,
{Name,Arity} <- Module:module_info(exports) ].
Step 4.
If you want the list sorted on module, then name within module,
then arity within name within module, you can just use
lists:sort(Triples).
Now suppose we want that listed sorted by Name, then Arity,
then Module. You have to write your own comparison function,
but that's not hard.
Compare = fun ({M1,F,N}, {M2,F,N}) -> M1 =< M2
; ({_,F,N1}, {_,F,N2}) -> N1 =< N2
; ({_,F1,_}, {_,F2,_}) -> F1 =< F2
end,
lists:sort(Compare, Triples).
(try-it-in-the-shell code tested)
More information about the erlang-questions
mailing list