Order loaded modules by amount of functions
Clash Royale CLAN TAG#URR8PPP
.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;
up vote
3
down vote
favorite
I am trying to solve some exercises from the "Programming Erlang" book.
One of them is "Write a function to determine which module exports the most functions"
This is my solution:
-module(module_info).
-export([modules_ordered_by_amount_of_functions/0]).
modules_ordered_by_amount_of_functions() ->
Modules = loaded_modules_with_functions(),
SortedModules = lists:sort(fun compare_functions_length/2, Modules),
lists:map(fun extract_amount_of_functions/1, SortedModules).
loaded_modules_with_functions() ->
LoadedModules = code:all_loaded(),
LoadedModuleNames = lists:map(fun extract_module_name/1, LoadedModules),
lists:map(fun extract_functions_from_module/1, LoadedModuleNames).
extract_module_name(ModuleName, _) -> ModuleName.
extract_functions_from_module(ModuleName) ->
[_, exports, Functions | _] = ModuleName:module_info(),
ModuleName, Functions.
compare_functions_length (_, FunctionsA, _, FunctionsB) ->
length(FunctionsA) >= length(FunctionsB).
extract_amount_of_functions (ModuleName, Functions) ->
ModuleName, length(Functions).
It doesn't really feel elegant to me.
reflection erlang
add a comment |Â
up vote
3
down vote
favorite
I am trying to solve some exercises from the "Programming Erlang" book.
One of them is "Write a function to determine which module exports the most functions"
This is my solution:
-module(module_info).
-export([modules_ordered_by_amount_of_functions/0]).
modules_ordered_by_amount_of_functions() ->
Modules = loaded_modules_with_functions(),
SortedModules = lists:sort(fun compare_functions_length/2, Modules),
lists:map(fun extract_amount_of_functions/1, SortedModules).
loaded_modules_with_functions() ->
LoadedModules = code:all_loaded(),
LoadedModuleNames = lists:map(fun extract_module_name/1, LoadedModules),
lists:map(fun extract_functions_from_module/1, LoadedModuleNames).
extract_module_name(ModuleName, _) -> ModuleName.
extract_functions_from_module(ModuleName) ->
[_, exports, Functions | _] = ModuleName:module_info(),
ModuleName, Functions.
compare_functions_length (_, FunctionsA, _, FunctionsB) ->
length(FunctionsA) >= length(FunctionsB).
extract_amount_of_functions (ModuleName, Functions) ->
ModuleName, length(Functions).
It doesn't really feel elegant to me.
reflection erlang
add a comment |Â
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I am trying to solve some exercises from the "Programming Erlang" book.
One of them is "Write a function to determine which module exports the most functions"
This is my solution:
-module(module_info).
-export([modules_ordered_by_amount_of_functions/0]).
modules_ordered_by_amount_of_functions() ->
Modules = loaded_modules_with_functions(),
SortedModules = lists:sort(fun compare_functions_length/2, Modules),
lists:map(fun extract_amount_of_functions/1, SortedModules).
loaded_modules_with_functions() ->
LoadedModules = code:all_loaded(),
LoadedModuleNames = lists:map(fun extract_module_name/1, LoadedModules),
lists:map(fun extract_functions_from_module/1, LoadedModuleNames).
extract_module_name(ModuleName, _) -> ModuleName.
extract_functions_from_module(ModuleName) ->
[_, exports, Functions | _] = ModuleName:module_info(),
ModuleName, Functions.
compare_functions_length (_, FunctionsA, _, FunctionsB) ->
length(FunctionsA) >= length(FunctionsB).
extract_amount_of_functions (ModuleName, Functions) ->
ModuleName, length(Functions).
It doesn't really feel elegant to me.
reflection erlang
I am trying to solve some exercises from the "Programming Erlang" book.
One of them is "Write a function to determine which module exports the most functions"
This is my solution:
-module(module_info).
-export([modules_ordered_by_amount_of_functions/0]).
modules_ordered_by_amount_of_functions() ->
Modules = loaded_modules_with_functions(),
SortedModules = lists:sort(fun compare_functions_length/2, Modules),
lists:map(fun extract_amount_of_functions/1, SortedModules).
loaded_modules_with_functions() ->
LoadedModules = code:all_loaded(),
LoadedModuleNames = lists:map(fun extract_module_name/1, LoadedModules),
lists:map(fun extract_functions_from_module/1, LoadedModuleNames).
extract_module_name(ModuleName, _) -> ModuleName.
extract_functions_from_module(ModuleName) ->
[_, exports, Functions | _] = ModuleName:module_info(),
ModuleName, Functions.
compare_functions_length (_, FunctionsA, _, FunctionsB) ->
length(FunctionsA) >= length(FunctionsB).
extract_amount_of_functions (ModuleName, Functions) ->
ModuleName, length(Functions).
It doesn't really feel elegant to me.
reflection erlang
edited Jun 11 at 19:29
200_success
123k14143399
123k14143399
asked Jun 4 at 20:01
Altoyr
1163
1163
add a comment |Â
add a comment |Â
1 Answer
1
active
oldest
votes
up vote
2
down vote
Breaking the problem into tiny, useful functions is a very good way to write Erlang code. So your code is easy to read and gives us a great start in understanding and analyzing it.
The one thing I notice is that, in loaded_modules_with_functions
, you map a function over a list and then map a function on the resulting list. This creates an intermediate list which will just be thrown away (GCed.) I'd combine the two operations so there's only one pass through the list. Since the result of this function is the resulting list, I'd use a list comprehension:
loaded_modules_with_functions() ->
[ extract_functions_from_module(ModName) || ModName, _ <- code:all_loaded() ].
In extract_functions_from_module
, you're making an assumption that the export list is always the second element. Maybe this is the case. Or maybe in your OTP release this is always the case. The next release, however, may break your code if module_info
adds more information. You can use the proplists
module (in the standard library) to generalize the look-up:
extract_functions_from_module(ModuleName) ->
ModuleName, proplists:get_value(exports, ModuleName:module_info(), ).
Finally, I'd put a list comprehension at the end of modules_ordered_by_amount_of_functions
and inline extract_amount_of_functions
.
add a comment |Â
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
2
down vote
Breaking the problem into tiny, useful functions is a very good way to write Erlang code. So your code is easy to read and gives us a great start in understanding and analyzing it.
The one thing I notice is that, in loaded_modules_with_functions
, you map a function over a list and then map a function on the resulting list. This creates an intermediate list which will just be thrown away (GCed.) I'd combine the two operations so there's only one pass through the list. Since the result of this function is the resulting list, I'd use a list comprehension:
loaded_modules_with_functions() ->
[ extract_functions_from_module(ModName) || ModName, _ <- code:all_loaded() ].
In extract_functions_from_module
, you're making an assumption that the export list is always the second element. Maybe this is the case. Or maybe in your OTP release this is always the case. The next release, however, may break your code if module_info
adds more information. You can use the proplists
module (in the standard library) to generalize the look-up:
extract_functions_from_module(ModuleName) ->
ModuleName, proplists:get_value(exports, ModuleName:module_info(), ).
Finally, I'd put a list comprehension at the end of modules_ordered_by_amount_of_functions
and inline extract_amount_of_functions
.
add a comment |Â
up vote
2
down vote
Breaking the problem into tiny, useful functions is a very good way to write Erlang code. So your code is easy to read and gives us a great start in understanding and analyzing it.
The one thing I notice is that, in loaded_modules_with_functions
, you map a function over a list and then map a function on the resulting list. This creates an intermediate list which will just be thrown away (GCed.) I'd combine the two operations so there's only one pass through the list. Since the result of this function is the resulting list, I'd use a list comprehension:
loaded_modules_with_functions() ->
[ extract_functions_from_module(ModName) || ModName, _ <- code:all_loaded() ].
In extract_functions_from_module
, you're making an assumption that the export list is always the second element. Maybe this is the case. Or maybe in your OTP release this is always the case. The next release, however, may break your code if module_info
adds more information. You can use the proplists
module (in the standard library) to generalize the look-up:
extract_functions_from_module(ModuleName) ->
ModuleName, proplists:get_value(exports, ModuleName:module_info(), ).
Finally, I'd put a list comprehension at the end of modules_ordered_by_amount_of_functions
and inline extract_amount_of_functions
.
add a comment |Â
up vote
2
down vote
up vote
2
down vote
Breaking the problem into tiny, useful functions is a very good way to write Erlang code. So your code is easy to read and gives us a great start in understanding and analyzing it.
The one thing I notice is that, in loaded_modules_with_functions
, you map a function over a list and then map a function on the resulting list. This creates an intermediate list which will just be thrown away (GCed.) I'd combine the two operations so there's only one pass through the list. Since the result of this function is the resulting list, I'd use a list comprehension:
loaded_modules_with_functions() ->
[ extract_functions_from_module(ModName) || ModName, _ <- code:all_loaded() ].
In extract_functions_from_module
, you're making an assumption that the export list is always the second element. Maybe this is the case. Or maybe in your OTP release this is always the case. The next release, however, may break your code if module_info
adds more information. You can use the proplists
module (in the standard library) to generalize the look-up:
extract_functions_from_module(ModuleName) ->
ModuleName, proplists:get_value(exports, ModuleName:module_info(), ).
Finally, I'd put a list comprehension at the end of modules_ordered_by_amount_of_functions
and inline extract_amount_of_functions
.
Breaking the problem into tiny, useful functions is a very good way to write Erlang code. So your code is easy to read and gives us a great start in understanding and analyzing it.
The one thing I notice is that, in loaded_modules_with_functions
, you map a function over a list and then map a function on the resulting list. This creates an intermediate list which will just be thrown away (GCed.) I'd combine the two operations so there's only one pass through the list. Since the result of this function is the resulting list, I'd use a list comprehension:
loaded_modules_with_functions() ->
[ extract_functions_from_module(ModName) || ModName, _ <- code:all_loaded() ].
In extract_functions_from_module
, you're making an assumption that the export list is always the second element. Maybe this is the case. Or maybe in your OTP release this is always the case. The next release, however, may break your code if module_info
adds more information. You can use the proplists
module (in the standard library) to generalize the look-up:
extract_functions_from_module(ModuleName) ->
ModuleName, proplists:get_value(exports, ModuleName:module_info(), ).
Finally, I'd put a list comprehension at the end of modules_ordered_by_amount_of_functions
and inline extract_amount_of_functions
.
answered Jun 11 at 17:07
RichN
38819
38819
add a comment |Â
add a comment |Â
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f195839%2forder-loaded-modules-by-amount-of-functions%23new-answer', 'question_page');
);
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Sign up or log in
StackExchange.ready(function ()
StackExchange.helpers.onClickDraftSave('#login-link');
);
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password