Skip to content

Commit acf5fde

Browse files
committed
added gensyms_to_ids function for replacing gensyms with deterministically generated identifiers
1 parent 8104176 commit acf5fde

File tree

1 file changed

+25
-2
lines changed

1 file changed

+25
-2
lines changed

src/utils.jl

Lines changed: 25 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -131,11 +131,34 @@ end
131131
isgensym(s::Symbol) = contains(string(s), "#")
132132
isgensym(s) = false
133133

134+
"""
135+
gensyms_to_ids(expr)
136+
137+
Replaces gensyms with unique ids (deterministically)
138+
"""
139+
function gensyms_to_ids(ex)
140+
counter = 0
141+
base_id = "sym_id_"
142+
syms = Dict{Symbol, Symbol}()
143+
prewalk(ex) do x
144+
if isgensym(x)
145+
repl_sym = Symbol("$base_id$counter")
146+
#tried the following, but it did not work:
147+
#repl_sym = Symbol(replace(string(x), "#", ""))
148+
counter+=1
149+
Base.@get!(syms, x, repl_sym)
150+
else
151+
x
152+
end
153+
154+
end
155+
end
156+
134157
"""
135158
alias_gensyms(expr)
136159
137-
Replaces gensyms with animal names. This makes gensym'd code far easier to
138-
follow.
160+
Replaces gensyms with animal names
161+
This makes gensym'd code far easier to follow.
139162
"""
140163
function alias_gensyms(ex)
141164
left = copy(animals)

0 commit comments

Comments
 (0)