-
Notifications
You must be signed in to change notification settings - Fork 6
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unroll only the first iteration #10
Comments
I'm not sure I understand what you're asking for. Isn't https://discourse.julialang.org/t/skipping-parts-of-a-for-loop-in-the-first-iteration/16252/8 good enough? It's similar to what I would write for |
I tried my hand at it macro unroll_n(n::Int, loop)
@assert @capture(loop, for var_ in iter0_; body__; end)
@gensym iter st
function gen_loop(rem::Int)
if rem == 0
quote
while true
ϕ === nothing && break
$var, $st = ϕ
$(body...)
ϕ = Base.iterate($iter, $st)
end
end
else
quote
if ϕ !== nothing
let ($var, $st) = ϕ
$(body...)
ϕ = Base.iterate($iter, $st)
$(gen_loop(rem-1))
end
end
end
end
end
esc(quote
$iter = $iter0
ϕ = Base.iterate($iter)
$(gen_loop(n))
end)
end There's a weird scoping bug I don't understand, unfortunately julia> @unroll_n 1 for x in 1:10
println(x)
end
ERROR: UndefVarError: ϕ not defined
Stacktrace:
[1] top-level scope at ./REPL[49]:17 But the macroexpansion looks right. |
Could this be related to the observation that https://github.com/mschauer/Trajectories.jl/blob/master/src/unroll1.jl will only run if defined in the same module as the caller? |
For accumulation of iterators it is nice to unroll only the first iteration. For example
is a nice idiom and
would make it type stable.
Would this be easy to add as variation of
@unroll
, say@unrollfirst
? See https://discourse.julialang.org/t/skipping-parts-of-a-for-loop-in-the-first-iteration/16252/19 for related discussionThe text was updated successfully, but these errors were encountered: