1
votes

I'm new to Erlang. I have a Map with which I have to create registered processes of all the keys and then further processing. I'm registering the processes in partone module:

-module(partone). 
-import(parttwo,[start/2]).
start() ->
    {ok,List}=file:consult("file.txt"),
    MyMap=maps:from_list(List),
    maps:fold(
        fun(Key,Value,ok) -> 
            print_Map([Key],[Value])
        end,ok,MyMap),
    maps:fold(
        fun(K,V,ok) -> 
            register(K,spawn(calling, startFun,[K,V]))
        end,ok,MyMap).

print_Map(Key,Value)->
    io:fwrite("~n~w : ~w",[Key,Value]).

parttwo.erl:
-module(parttwo).
-export([startFun/2]).
-import(partone,[startFun/0]).

 startFun(Key,Value) ->
    io:fwrite("~w~n KEY::",[Key]).

I could get the map contents in the output which is by print_Map. But then I'm getting the following error: {"init terminating in do_boot",{function_clause,[{exchange,'-start/0-fun-1-',[jill,[bob,joe,bob],true],[{file,"d:/exchange.erl"},{line,40}]},{lists,foldl,3,[{file,"lists.erl"},{line,1263}]},{init,start_em,1,[{file,"init.erl"},{line,1085}]},{init,do_boot,3,[{file,"init.erl"},{line,793}]}]}} init terminating in do_boot ({function_clause,[{exchange,-start/0-fun-1-,[jill,[],true],[{},{}]},{lists,foldl,3,[{},{_}]},{init,start_em,1,[{},{}]},{init,do_boot,3,[{},{}]}]})

Crash dump is being written to: erl_crash.dump...done

1

1 Answers

2
votes

The problem you are having is caused by using ok as the 3rd parameter to maps:fold().

In your first call to maps:fold(), because io:fwrite() returns ok, your fun returns ok each time it is called, which means erlang makes the call:

Fold_Fun(hello, 10, ok)

which matches your fun clause:

                          |
                          V
Fold_Fun = fun(Key,Value,ok) -> ...

However, in your second maps:fold() call, because register() returns a pid, your fun returns a pid each time it is called, which means erlang makes the call:

Fold_Fun(hello, 10, SomePid)

which does not match the fun clause:

                        a pid
                          |
                          V
Fold_Fun = fun(Key,Value,ok)

If you want to ignore the 3rd parameter of maps:fold() then you can specify a don't care variable, e.g. _Acc. In erlang, a variable will match anything, while the atom ok will only match ok.

====

spawn(calling, startFun,[K,V])

You have not posted anything about a "calling" module.

-module(my).
-compile(export_all).

start() ->
    {ok, Tuples} = file:consult("data.txt"),
    MyMap = maps:from_list(Tuples),
    io:format("~p~n", [MyMap]),
    Keys = maps:keys(MyMap),
    lists:foreach(
      fun(Key) -> 
            #{Key := Val} = MyMap,
            register(Key, spawn(my, show, [Key, Val]))
      end,
      Keys
    ).

show(Key, Val) ->
    io:format("~w : ~w~n", [Key, Val]).

data.txt:

~/erlang_programs$ cat data.txt
{hello, 10}.
{world, 20}.

In the shell:

9> c(my).
my.erl:2: Warning: export_all flag enabled - all functions will be exported
{ok,my}

10> my:start().
#{hello => 10,world => 20}
hello : 10
world : 20
ok