Erlang map nested list
I have i list:
[["asd", "ddsa"], ["ASD", "ASD"], ["FH", "SDFDF]].
How can 开发者_JAVA技巧i applicate function F to each element of this list? How can i map this list?
Thank you.
If assuming by element you mean "asd", your main List will always be two levels deep as in your example and your returned result shall be in the same format, this will due.
List1 = [["asd", "ddsa"], ["ASD", "ASD"], ["FH", "SDFDF]]
[ [ F(String) || String <- List2 ] || List2 <- List1 ]
If you mean that your element is ["asd","ddsa"] just use lists:map/2 or list comprehension as below.
List1 = [["asd", "ddsa"], ["ASD", "ASD"], ["FH", "SDFDF]]
[ F(List2) || List2 <- List1 ]
You could define a deepMap
function, performing something like:
deepMap(Fun, T) ->
case is_list(T) of
true -> lists:map(fun(E) -> deepMap(Fun,E) end, T);
false -> Fun(T)
end.
If the input is not a list, evaluate your function. If it is a list, apply deepMap
to all elements using map
, using your function.
精彩评论