List of nested Erlang maps

I have a list:

[["asd", "ddsa"], ["ASD", "ASD"], ["FH", "SDFDF]].

How can I apply function F to each element of this list? How can I match this list?

Thank.

+3
source share
2 answers

Assuming the item has the value "asd", your main list will always be two levels deep, as in your example, and your returned result should be in the same format, this should happen.

List1 = [["asd", "ddsa"], ["ASD", "ASD"], ["FH", "SDFDF]]
[ [ F(String) || String <- List2 ] || List2 <- List1 ]

If you mean that your element ["asd", "ddsa"] just uses lists: map / 2 or a list comprehension, as shown below.

List1 = [["asd", "ddsa"], ["ASD", "ASD"], ["FH", "SDFDF]]
[ F(List2) || List2 <- List1 ]
+7
source

You can define a function deepMapby doing something like:

deepMap(Fun, T) ->
  case is_list(T) of
    true -> lists:map(fun(E) -> deepMap(Fun,E) end, T);
    false -> Fun(T)
  end.

, . , deepMap , map, .

+2

Source: https://habr.com/ru/post/1791475/


All Articles