Of course. The most efficient and idiomatic way to transform a complex term in Erlang is to use a recursive function that traverses the term structure.
The function will:
Check if the current term is a key in the replacement map M. If it is, return the corresponding value.
If not, check if the term is a compound data type (a tuple, list, or map).
If it's a compound type, recursively call the replacement function on each of its elements and reconstruct the data type with the new elements.
If it's an atomic type (like an integer, atom, binary, etc.) and not a key in the map, return it unchanged.
This approach is efficient because it only traverses the term once and takes advantage of Erlang's powerful pattern matching and list/map comprehensions.
The Solution
Here is a complete module that implements this logic.
erlang
-module(term_replace).
-export([replace/2]).
%% @doc
%% Replaces all occurrences of sub-terms in a Term based on a a map of
%% replacements.
%%
%% @param Term The term to transform.
%% @param Replacements A map where keys are the terms to be found and
%% values are their replacements. Example: #{{local_ref, 1} => new_val}.
replace(Term, Replacements) ->
% First, check if the entire Term is a key in the map.
% This is our primary base case for the recursion.
case maps:is_key(Term, Replacements) of
true ->
maps:get(Term, Replacements);
false ->
% If the term itself is not a key, we traverse its contents.
case Term of
T when is_tuple(T) ->
% Convert to list, transform each element, convert back to tuple.
NewElements = [replace(E, Replacements) || E <- tuple_to_list(T)],
list_to_tuple(NewElements);
L when is_list(L) ->
% Transform each element in the list. This handles strings too.
% Note: This implementation assumes a proper list.
[replace(E, Replacements) || E <- L];
M when is_map(M) ->
% Rebuild the map by transforming each key and value.
maps:fold(
fun(Key, Value, Acc) ->
NewKey = replace(Key, Replacements),
NewValue = replace(Value, Replacements),
maps:put(NewKey, NewValue, Acc)
end,
#{},
M
);
_Other ->
% This is the other base case: atomic values that are not in the
% replacement map (integers, atoms, binaries, pids, etc.).
% They are returned as is.
Term
end
end.
How to Use It
Here is how you would use this module with your example data.
As you can see in the output of NewZ, the specific occurrence of {local_ref,-576460752303422591} has been replaced by {node, 5}, while the other {local_ref,-576460752303422559} was left untouched because it was not in the replacement map M.
Why this approach is efficient
Single Pass: The term is traversed only once.
Immutability: Erlang's immutable data structures mean that we are creating a new term, not modifying the old one in place. Sub-terms that are not changed are shared, which can save memory. The Garbage Collector is highly optimized for this pattern of creating many short-lived terms.
Idiomatic: This recursive traversal is the standard, well-understood pattern for this kind of problem in functional languages like Erlang. It's clear, maintainable, and performs well.
Generality: This function works for any term and any key-value replacement map, not just for {local_ref, ...} tuples.