Skip to content

Commit 4bcabfc

Browse files
generated Chain
1 parent e858645 commit 4bcabfc

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

src/rewriters.jl

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,8 +60,8 @@ end
6060

6161
If(f, x) = IfElse(f, x, Empty())
6262

63-
struct Chain
64-
rws
63+
struct Chain{Cs}
64+
rws::Cs
6565
stop_on_match::Bool
6666
end
6767
Chain(rws) = Chain(rws, false)
@@ -78,8 +78,24 @@ function (rw::Chain)(x)
7878
end
7979
end
8080
return x
81+
end
8182

83+
@generated function (rw::Chain{<:NTuple{N, Any}})(x) where {N}
84+
quote
85+
Base.@nexprs $N i -> begin
86+
f = rw.rws[i]
87+
y = f(x)
88+
if rw.stop_on_match && y !== nothing && !isequal(x, y)
89+
return y
90+
end
91+
if y !== nothing
92+
x = y
93+
end
94+
end
95+
return x
96+
end
8297
end
98+
8399
instrument(c::Chain, f) = Chain(map(x->instrument(x,f), c.rws))
84100

85101
struct RestartedChain{Cs}

0 commit comments

Comments
 (0)