gb_trees:map/2
树里的每一个键值对都调用执行一次函数
用法:
map(Function, Tree1) -> Tree2
内部实现:
-spec map(Function, Tree1) -> Tree2 when Function :: fun((K :: term(), V1 :: term()) -> V2 :: term()), Tree1 :: gb_tree(), Tree2 :: gb_tree(). map(F, {Size, Tree}) when is_function(F, 2) -> {Size, map_1(F, Tree)}. map_1(_, nil) -> nil; map_1(F, {K, V, Smaller, Larger}) -> {K, F(K, V), map_1(F, Smaller), map_1(F, Larger)}.
树 Tree1 里的每一个键值对都调用执行一次 Function 函数,最后返回跟树 Tree1 有同键和新值的一个新树 Tree2。
Orddict = orddict:from_list([{pear, 7}, {orange, 5}, {apple, 2}]), Tree = gb_trees:from_orddict(Orddict), Fun = fun(K, V) -> lists:concat([K, "+", V]) end, gb_trees:map(Fun, Tree).