Julia 中多个条件的三元运算符
Ternary operator for multiple conditions in Julia
我有如图所示的 Julia 代码
if cxp1_v1 < cxp1_v2
d_min = cxp1_v1
d_max = cxp1_v2
else
d_min = cxp1_v2
d_max = cxp1_v1
end
或
if cxp1_v1 < cxp1_v2 d_min, d_max = cxp1_v1, cxp1_v2 else d_min, d_max = cxp1_v2, cxp1_v1 end
有没有办法在三元运算符中完成同样的事情?我尝试了如图所示的东西
cxp1_v1 < cxp1_v2 ? d_min, d_max = cxp1_v1, cxp1_v2:d_min, d_max = cxp1_v2, cxp1_v1
但我明白了
syntax: colon expected in "?" expression
Stacktrace:
[1] top-level scope
@ In[422]:11
[2] eval
@ ./boot.jl:373 [inlined]
[3] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
@ Base ./loading.jl:1196
cxp1_v1 < cxp1_v2 ? (d_min, d_max) = (cxp1_v1, cxp1_v2) : (d_min, d_max) = (cxp1_v2, cxp1_v1)
但我可能会写成:
d_min, d_max = cxp1_v1 < cxp1_v2 ? (cxp1_v1, cxp1_v2) : (cxp1_v2, cxp1_v1)
因为看起来更容易阅读,甚至:
d_min, d_max = minmax(cxp1_v1, cxp1_v2)
我有如图所示的 Julia 代码
if cxp1_v1 < cxp1_v2
d_min = cxp1_v1
d_max = cxp1_v2
else
d_min = cxp1_v2
d_max = cxp1_v1
end
或
if cxp1_v1 < cxp1_v2 d_min, d_max = cxp1_v1, cxp1_v2 else d_min, d_max = cxp1_v2, cxp1_v1 end
有没有办法在三元运算符中完成同样的事情?我尝试了如图所示的东西
cxp1_v1 < cxp1_v2 ? d_min, d_max = cxp1_v1, cxp1_v2:d_min, d_max = cxp1_v2, cxp1_v1
但我明白了
syntax: colon expected in "?" expression
Stacktrace:
[1] top-level scope
@ In[422]:11
[2] eval
@ ./boot.jl:373 [inlined]
[3] include_string(mapexpr::typeof(REPL.softscope), mod::Module, code::String, filename::String)
@ Base ./loading.jl:1196
cxp1_v1 < cxp1_v2 ? (d_min, d_max) = (cxp1_v1, cxp1_v2) : (d_min, d_max) = (cxp1_v2, cxp1_v1)
但我可能会写成:
d_min, d_max = cxp1_v1 < cxp1_v2 ? (cxp1_v1, cxp1_v2) : (cxp1_v2, cxp1_v1)
因为看起来更容易阅读,甚至:
d_min, d_max = minmax(cxp1_v1, cxp1_v2)