Suppose I have a map like this
A = #{a=>1,b=>2,c=>3}.
I want to create a function which converts A to a list of tuples of key-value pairs.
list = [{a,1},{b,2},{c,3}]
maps:to_list/1 does exactly this:
1> maps:to_list(#{a=>1,b=>2,c=>3}).
[{a,1},{b,2},{c,3}]
You can use maps:fold/3 for loop map items. Let's say you need just convert a map, then you can use something like:
1> A = #{a=>1,b=>2,c=>3}.
2> maps:fold(
fun(K, V, Acc) ->
[{K, V} | Acc]
end,
[], A).
[{c,3},{b,2},{a,1}]
For case if need to do the same for nested maps, this example can be modify like:
1> A = #{a => 1, b => 2, c => 3, d => #{a => 1, b => #{a => 1}}},
2> Nested =
fun F(K, V = #{}, Acc) -> [{K, maps:fold(F, [], V)} | Acc];
F(K, V, Acc) -> [{K, V} | Acc]
end,
3> maps:fold(Nested, [], A).
[{d,[{b,[{a,1}]},{a,1}]},{c,3},{b,2},{a,1}]
Related
I am newer to erlang, I have a map like #{"a/.+":"v1", "b/c/.+": "v2"}
I want to get value by input key like "a/d" to match "a/.+" and get "v1".
It easy to pattern when key is exactly and input key is regex, how can I implement it.
For this case, you don't have to use map. The idea is to use regex to find which key match with the input, and then return the mapped value.
The un-optimized version is using re:run/2 without compiling the pattern.
L1 = [{"a/.+", "v1"}, {"b/c/.+", "v2"}],
LResult1 = lists:foldl(fun({K, V}, ListIn) ->
Match = re:run("a/d", K),
case Match of
nomatch -> ListIn;
_ -> ListIn ++ [{K, V}]
end
end,
[],
L1
),
case LResult1 of
[] ->
no_matching_rule;
_ ->
Hd1 = hd(LResult1),
{K1, V1} = Hd1,
V1
end.
The optimized version can be achieved by compiling the regex pattern once before using them.
{ok, Mp1} = re:compile("a/.+"),
{ok, Mp2} = re:compile("b/c/.+"),
L2 = [{Mp1, "v1"}, {Mp2, "v2"}],
LResult2 = lists:foldl(fun({K, V}, ListIn) ->
Match = re:run("a/d", K),
case Match of
nomatch -> ListIn;
_ -> ListIn ++ [{K, V}]
end
end,
[],
L2
),
case LResult2 of
[] ->
no_matching_rule;
_ ->
Hd2 = hd(LResult2),
{K2, V2} = Hd2,
V2
end.
Pasting any of above codes in Erlang shell will yield "v1".
Above code assumes that one input can have multiple matched pattern, but only the first one will be printed as output.
If you want to find all the keys in the map that match your string, you will need to iterate over the whole map:
a.erl:
-module(a).
-compile(export_all).
find(String, Map) ->
MyMatchFun = fun (RegexKey, Val, Acc) ->
case re:run(String, RegexKey) of
nomatch -> Acc;
{match, _} -> [Val | Acc]
end
end,
maps:fold(MyMatchFun, _Acc=[], Map).
In the shell:
34> c(a).
a.erl:2:2: Warning: export_all flag enabled - all functions will be exported
% 2| -compile(export_all).
% | ^
{ok,a}
35> Map.
#{"a/.+" => "v1","a/b/.+" => "v3","b/c/.+" => "v2"}
36> a:find("a/d", Map).
["v1"]
36> a:find("b/c/dd", Map).
["v2"]
38> a:find("a/b/cc", Map).
["v3","v1"]
If there is only one key in the map that will match your string, then you can stop iterating over the map as soon as you find a match:
a.erl:
-module(a).
-compile(export_all).
find(String, Map) ->
I = maps:iterator(Map),
match_for(String, I).
match_for(String, I) ->
case maps:next(I) of
none -> %% then you have reached the end of the Map
no_keys_in_map_matched_string;
{RegExKey, Val, NewI} ->
case re:run(String, RegExKey) of
nomatch -> match_for(String, NewI); %% continue iterating over the Map looking for a match
{match, _} -> Val %% found a match, so return the associated value
end
end.
In the shell:
27> c(a).
a.erl:2:2: Warning: export_all flag enabled - all functions will be exported
% 2| -compile(export_all).
% | ^
{ok,a}
28> Map = #{"a/.+" => "v1", "b/c/.+" => "v2"}.
29> a:find("a/d", Map).
"v1"
30> a:find("a/b/cc", Map).
"v1"
31> a:find("b/c/dd", Map).
"v2"
32> a:find("z", Map).
no_keys_in_map_matched_string
How can i replace all keys in the nested map with another key without knowing the path (or replace keys, that match a pattern), for example:
Map = #{foo => #{bar => 1, foo => #{bar => 2}}}.
Map2 = maps:keyreplace(bar, foo, Map).
>>> #{foo => #{foo => 1, foo => #{foo => 2}}}
What is the most efficient way to do this?
Replacing key in a flat map seems to be pretty simple:
keyreplace(K1, K2, M) when is_map(M) ->
case maps:take(K1, M) of
{V, M2} ->
maps:put(K2, V, M2);
error -> M
end.
Or like that, maybe:
keymap(F, Map) ->
maps:fold(fun(K, V, Acc) ->
maps:put(F(K), V, Acc)
end, #{}, Map).
Map = #{foo => 1, bar => 2}.
Fun = fun(K) -> atom_to_list(K) end.
Map2 = keymap(Fun, Map).
>>> #{"bar" => 2,"foo" => 1}
I wrote recursive function which you can use to achieve your goal, it takes 3 arguments:
Is_matched - function in which you check that key match your pattern.
Convert - function which converts key as you want.
Map - map to process.
replace_keys(Is_matched, Convert, Map) when is_map(Map) ->
maps:fold(fun (K, V, AccIn) ->
NewKey =
case Is_matched(K) of
true -> Convert(K);
false -> K
end,
maps:put(NewKey, replace_keys(Is_matched, Convert, V), AccIn)
end, #{}, Map);
replace_keys(_, _, Term) -> Term.
Example:
1> Map = #{a => "1", <<"b">> => #{c => "2", <<"d">> => 3}}.
#{a => "1",<<"b">> => #{c => "2",<<"d">> => 3}}
2> Is_matched = fun(Term) -> is_atom(Term) end.
#Fun<erl_eval.6.99386804>
3> Convert = fun(Term) -> atom_to_list(Term) end.
#Fun<erl_eval.6.99386804>
4> keyreplace:replace_keys(Is_matched, Convert, Map).
#{"a" => "1",<<"b">> => #{"c" => "2",<<"d">> => 3}}
This can be done with some modifications to your maps:fold attempt:
If the third argument is not a map, return it as is.
When folding:
Recursively call the function with the value field to obtain the new value.
If the key matches K1, put the new value into K2.
Your example output is incorrect - it has 2 values for the foo key. I've modified it in the test below.
keyreplace(K1, K2, Map) when is_map(Map) ->
maps:fold(fun(K, V, Acc) ->
Key = if K == K1 -> K2; true -> K end,
Value = keyreplace(K1, K2, V),
maps:put(Key, Value, Acc)
end, #{}, Map);
keyreplace(_K1, _K2, Term) -> Term.
1> c(a).
{ok,a}
2> Map = #{foo => #{bar => 1, baz => #{bar => 2}}}.
#{foo => #{bar => 1,baz => #{bar => 2}}}
3> a:keyreplace(bar, foo, Map).
#{foo => #{baz => #{foo => 2},foo => 1}}
I have the following List structure:
[{A, [{B <--, [A, C]}, {C <--, [B, A]}]}, {B, [{C <--, [A, C]}]}]
For example, B = 1, C = 2.
What would be the correct way to do so?
UPDATE
I'd like to count the number of <-- (a symbol I added just to show what I'm referring to) pointed item in it.
It can be implemented in many ways. Here is one more.
count(List) ->
count(List, dict:new()).
count([{_, InList} | Rest], Dict) ->
count(Rest, count2(InList, Dict));
count([], Dict) ->
dict:to_list(Dict).
count2([{Symbol, _} | Rest], Dict) ->
count2(Rest, dict:update_counter(Symbol, 1, Dict));
count2([], Dict) ->
Dict.
Example Output:
1> test:count([{one, [{b, [123]}, {c,[123]}, {b,[123]}]}, {two, [{b, [123]}, {c,[123]}, {b,[123]}]}]).
[{b,4},{c,2}]
You can write a simple code using an accumulator and some list functions. Supposing all list elements are of the same format:
count(L) ->
count (L, []).
count ([], Cases) ->
Cases;
count ([{_E1, [{X, [_E2, _E3]}]} | Rest], Cases) ->
NewCases =
case lists:keysearch(X, 1, Cases) of
false ->
[ {X, 1} | Cases ];
{value, {X, Val}} ->
lists:keyreplace(X, 1, Cases, {X, 1+Val})
end,
count(Rest, NewCases).
The more formal definition of problem is
Write a function map_search_pred(Map, Pred) that returns the first
element {Key,Value} in the map for which Pred(Key, Value) is true.
My attempt, looks like
map_search_pred(Map, Pred) ->
M = [{Key, Value} || {Key, Value} <- maps:to_list(Map), Pred(Key, Value) =:= true],
case length(M) of
0 -> {};
_ -> lists:nth(1, M)
end.
When I run this, I get correct answer
1> lib_misc:map_search_pred(#{}, fun(X, Y) -> X =:= Y end).
{}
2> lib_misc:map_search_pred(#{1 => 1, 2 => 2}, fun(X, Y) -> X =:= Y end).
{1,1}
3> lib_misc:map_search_pred(#{1 => 1, 2 => 3}, fun(X, Y) -> X =:= Y end).
{1,1}
4> lib_misc:map_search_pred(#{1 => 2, 2 => 2}, fun(X, Y) -> X =:= Y end).
{2,2}
5>
Problem?
The problem is efficiency.
It uses list comprehension and runs for all the elements in the list. The better solution would be to return after the first match.
As a beginner to language, I do not know a better idiomatic way to solve this, so looking for better ideas and suggestions
You could walk the list yourself and return as soon as you find an element for which Pred(Key, Value) is true:
map_search_pred(Map, Pred) when is_map(Map) ->
map_search_pred(maps:to_list(Map), Pred);
map_search_pred([], _) ->
error;
map_search_pred([{Key,Value}=H|T], Pred) ->
case Pred(Key, Value) of
true ->
{ok, H};
false ->
map_search_pred(T, Pred)
end.
The problem I am trying to solve states
Write a function map_search_pred(Map, Pred) that returns the first
element {Key,Value} in the map for which Pred(Key, Value) is true.
My attempt looks like
map_search_pred(#{}, _) -> {};
map_search_pred(Map, Pred) ->
[H|_] = [{Key, Value} || {Key, Value} <- maps:to_list(Map), Pred(Key, Value) =:= true],
H.
When I run this, I see output as
1> lib_misc:map_search_pred(#{1 => 1, 2 => 3}, fun(X, Y) -> X =:= Y end).
{}
2> lib_misc:map_search_pred(#{1 => 1, 2 => 3}, fun(X, Y) -> X =:= Y end).
{}
3> maps:size(#{}).
0
4>
How am I so sure?
I pulled out the first clause so it looks like
map_search_pred(Map, Pred) ->
[H|_] = [{Key, Value} || {Key, Value} <- maps:to_list(Map), Pred(Key, Value) =:= true],
H.
and run again
1> lib_misc:map_search_pred(#{1 => 1, 2 => 3}, fun(X, Y) -> X =:= Y end).
{1,1}
2> lib_misc:map_search_pred(#{}, fun(X, Y) -> X =:= Y end).
** exception error: no match of right hand side value []
in function lib_misc:map_search_pred/2 (/Users/harith/code/IdeaProjects/others/erlang/programmingErlang/src/lib_misc.erl, line 42)
3>
According to map documentation:
Matching an expression against an empty map literal will match its type but no variables will be bound:
#{} = Expr
This expression will match if the expression Expr is of type map, otherwise it will fail with an exception badmatch.
However erlang:map_size can be used instead:
map_search_pred(Map, _) when map_size(Map) == 0 ->
{};
map_search_pred(Map, Pred) ->
[H|_] = [{Key, Value} || {Key, Value} <- maps:to_list(Map), Pred(Key, Value) =:= true],
H.