gpt4 book ai didi

metaprogramming - 循环展开?在 Julia 中使用元编程

转载 作者:行者123 更新时间:2023-12-04 12:50:33 26 4
gpt4 key购买 nike

有没有办法“以元编程方式”获得具有以下结构的代码块:

if r1 < R1
s = 1

elseif r1 < R2
s = 2

... etc until N

end
end

谢谢!

最佳答案

查看 Base.Cartesian.@nif

我认为这应该适合你......

julia> macroexpand(:(@nif 10 d->(r1 < R_d) d->begin s=d; break end))
:(if r1 < R_1 # REPL[9], line 1:
s = 1 # REPL[9], line 1:
break
else
if r1 < R_2 # REPL[9], line 1:
s = 2 # REPL[9], line 1:
break
else
if r1 < R_3 # REPL[9], line 1:
s = 3 # REPL[9], line 1:
break
else
if r1 < R_4 # REPL[9], line 1:
s = 4 # REPL[9], line 1:
break
else
if r1 < R_5 # REPL[9], line 1:
s = 5 # REPL[9], line 1:
break
else
if r1 < R_6 # REPL[9], line 1:
s = 6 # REPL[9], line 1:
break
else
if r1 < R_7 # REPL[9], line 1:
s = 7 # REPL[9], line 1:
break
else
if r1 < R_8 # REPL[9], line 1:
s = 8 # REPL[9], line 1:
break
else
if r1 < R_9 # REPL[9], line 1:
s = 9 # REPL[9], line 1:
break
else # REPL[9], line 1:
s = 10 # REPL[9], line 1:
break
end
end
end
end
end
end
end
end
end)

关于metaprogramming - 循环展开?在 Julia 中使用元编程,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39619204/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com