[erlang-questions] how to make the comprehension work
Roelof Wobben
r.wobben@REDACTED
Mon Aug 17 21:26:14 CEST 2015
Thanks,
I already found that that I have to change [ { } ] to { }
Roelof
Op 17-8-2015 om 20:21 schreef Roger Lipscombe:
> code:all_loaded/0, according to the documentation
> (http://www.erlang.org/doc/man/code.html#all_loaded-0) returns
> [{Module, Loaded}]. This is the notation for a list of tuples. Let's
> see:
>
> 1> code:all_loaded().
> [{io,"/usr/local/erlang-R16B03-1/lib/stdlib-1.19.4/ebin/io.beam"},
> {erl_distribution,"/usr/local/erlang-R16B03-1/lib/kernel-2.16.4/ebin/erl_distribution.beam"},
> {edlin,"/usr/local/erlang-R16B03-1/lib/stdlib-1.19.4/ebin/edlin.beam"},
> {zlib,preloaded},
> {...}|...]
>
> You are attempting to use a list comprehension to extract just the
> module names from that (what's that got to do with
> most_used_function_name?), as follows:
>
> 2> [Mod || [{Mod, _}] <- code:all_loaded()].
> []
>
> Your problem is that the '<-' operator iterates over each *element* of
> the list, and you're attempting to match another list. If your
> generator doesn't match, it'll return nothing. For example:
>
> 3> L = [{a, 1}, {a, 2}, {b, 3}, {b, 4}, {c, 5}].
> [{a,1},{a,2},{b,3},{b,4},{c,5}]
>
> 4> [X || {b, X} <- L].
> [3,4]
>
> This, weirdly, is not spelled out in the documentation at
> http://erlang.org/doc/programming_examples/list_comprehensions.html,
> which refers to the '<-' operator as "is taken from" (I generally call
> it "comes from").
>
> With that knowledge, you can play in the shell:
>
> 5> [X || X <- code:all_loaded()].
> [{io,"/usr/local/erlang-R16B03-1/lib/stdlib-1.19.4/ebin/io.beam"},
> {erl_distribution,"/usr/local/erlang-R16B03-1/lib/kernel-2.16.4/ebin/erl_distribution.beam"},
> {...}|...]
>
> Same as before; let's try matching on this:
>
> 6> [Mod || {Mod, _} <- code:all_loaded()].
> [io,erl_distribution,edlin,zlib,error_handler,io_lib,
> prim_eval,filename,erts_internal,unicode,orddict,gb_sets,
> inet_db,inet,ordsets,group,gen,erl_scan,kernel,erl_eval,
> prim_file,ets,lists,sets,inet_udp,io_lib_pretty,code,
> ram_file,dict|...]
>
> ...and we can even add a filter:
>
> 7> [Mod || {Mod, Loaded} <- code:all_loaded(), Loaded =:= preloaded].
> [zlib,prim_eval,erts_internal,prim_file,prim_zip,prim_inet,
> erlang,otp_ring0,init,erl_prim_loader]
>
> ...which is equivalent to:
>
> 8> [Mod || {Mod, preloaded} <- code:all_loaded()].
> [zlib,prim_eval,erts_internal,prim_file,prim_zip,prim_inet,
> erlang,otp_ring0,init,erl_prim_loader]
>
> On 17 August 2015 at 18:57, Roelof Wobben <r.wobben@REDACTED> wrote:
>> Op 17-8-2015 om 19:42 schreef Lukas Winkler:
>>> On 17 August 2015 at 19:32, Garrett Smith <g@REDACTED> wrote:
>>>> On Mon, Aug 17, 2015 at 12:22 PM, Roelof Wobben <r.wobben@REDACTED> wrote:
>>>>> Op 17-8-2015 om 19:17 schreef Lukas Winkler:
>>>>>> On 17 August 2015 at 19:14, Roelof Wobben <r.wobben@REDACTED> wrote:
>>>>>>> Hello,
>>>>>>>
>>>>>>> I try to extract the modules names from the output of code:loaded_all.
>>>>>>>
>>>>>>> So I tried this :
>>>>>>>
>>>>>>> -module(module).
>>>>>>>
>>>>>>> -export([most_used_function_name/0]).
>>>>>>>
>>>>>>> most_used_function_name() ->
>>>>>>> process_module_data(code:all_loaded()).
>>>>>>>
>>>>>>> process_module_data(Loaded_modules) ->
>>>>>>> Module_list = get_modules_name(Loaded_modules).
>>>>>>>
>>>>>>> get_modules_name(Loaded_Modules_list) ->
>>>>>>> [ModuleList || [{ModuleList, _}] <- Loaded_Modules_List ].
>>>>>>
>>>>>> Friendly reminder that we are not your compiler. Count the ]....
>>>>>>
>>>>>> -winlu
>>>>>>
>>>>> I know that. If I count the [ I see 2 and ] I see also 2.
>>>> He he - no, it's not that.
>>> True, I jumped to the first error that my eyes saw, which is not what
>>> the compile error was. Still if you ever manage to solve the unbound
>>> error you already have a clue where the next problem might be.
>>>
>>> -winlu
>> I solved that one. Next problem is why the outcome is a empty list.
>> So time to use some io:format ,
>>
>> Roelof
>>
>>
>>
>> ---
>> Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware.
>> https://www.avast.com/antivirus
>>
>> _______________________________________________
>> erlang-questions mailing list
>> erlang-questions@REDACTED
>> http://erlang.org/mailman/listinfo/erlang-questions
---
Dit e-mailbericht is gecontroleerd op virussen met Avast antivirussoftware.
https://www.avast.com/antivirus
More information about the erlang-questions
mailing list